|
| 1 | +/* |
| 2 | + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). |
| 3 | + * |
| 4 | + * The MIT License |
| 5 | + * Copyright © 2014-2022 Ilkka Seppälä |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package com.iluwatar.polling; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.*; |
| 31 | + |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +class DataRepositoryTest { |
| 35 | + |
| 36 | + private DataRepository repository; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setUp() { |
| 40 | + repository = new DataRepository(); // Initialize before each test |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testSaveAndFindById() { |
| 45 | + repository.save(1, "Test Data"); |
| 46 | + |
| 47 | + String result = repository.findById(1); |
| 48 | + |
| 49 | + assertEquals("Test Data", result, "The retrieved data should match the stored value."); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testFindById_NotFound() { |
| 54 | + String result = repository.findById(99); |
| 55 | + |
| 56 | + assertEquals("Data not found", result, "Should return 'Data not found' for missing entries."); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testDelete() { |
| 61 | + repository.save(2, "To be deleted"); |
| 62 | + repository.delete(2); |
| 63 | + |
| 64 | + String result = repository.findById(2); |
| 65 | + |
| 66 | + assertEquals("Data not found", result, "Deleted data should not be retrievable."); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testFindAll() { |
| 71 | + repository.save(1, "First"); |
| 72 | + repository.save(2, "Second"); |
| 73 | + |
| 74 | + Map<Integer, String> allData = repository.findAll(); |
| 75 | + |
| 76 | + assertEquals(2, allData.size(), "The repository should contain two items."); |
| 77 | + assertTrue(allData.containsKey(1) && allData.containsKey(2), "Both keys should exist."); |
| 78 | + } |
| 79 | +} |
0 commit comments