Add DAO module for Models
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfConceptGetterImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import java.util.NavigableMap;
29 import java.util.TreeMap;
30 import java.util.TreeSet;
31
32 import org.junit.Test;
33
34 /**
35  * Test the AxConceptGetterImpl class.
36  */
37 public class PfConceptGetterImplTest {
38
39     @Test
40     public void testAxConceptGetterImpl() {
41         NavigableMap<PfConceptKey, PfConceptKey> keyMap = new TreeMap<>();
42
43         PfConceptGetterImpl<PfConceptKey> getter = new PfConceptGetterImpl<>(keyMap);
44         assertNotNull(getter);
45
46         PfConceptKey keyA = new PfConceptKey("A", "0.0.1");
47         assertNull(getter.get(keyA));
48
49         try {
50             getter.get((String)null);
51             fail("test should throw an exception here");
52         }
53         catch (Exception getException) {
54             assertEquals("conceptKeyName may not be null", getException.getMessage());
55         }
56
57         assertNull(getter.get("W"));
58
59         PfConceptKey keyZ = new PfConceptKey("Z", "0.0.1");
60         keyMap.put(keyZ, keyZ);
61         assertNull(getter.get("W"));
62
63         PfConceptKey keyW001 = new PfConceptKey("W", "0.0.1");
64         keyMap.put(keyW001, keyW001);
65         assertEquals(keyW001, getter.get("W"));
66
67         PfConceptKey keyW002 = new PfConceptKey("W", "0.0.2");
68         keyMap.put(keyW002, keyW002);
69         assertEquals(keyW002, getter.get("W"));
70
71         keyMap.remove(keyZ);
72         assertEquals(keyW002, getter.get("W"));
73
74         try {
75             getter.get((String)null, "0.0.1");
76             fail("test should throw an exception here");
77         }
78         catch (Exception getException) {
79             assertEquals("conceptKeyName may not be null", getException.getMessage());
80         }
81
82         assertEquals(keyW002, getter.get("W", "0.0.2"));
83         assertEquals(keyW002, getter.get("W", (String)null));
84
85         assertEquals(new TreeSet<PfConceptKey>(keyMap.values()), getter.getAll(null));
86         assertEquals(new TreeSet<PfConceptKey>(keyMap.values()), getter.getAll(null, null));
87
88         assertEquals(keyW001, getter.getAll("W", null).iterator().next());
89         assertEquals(keyW002, getter.getAll("W", "0.0.2").iterator().next());
90         assertEquals(0, getter.getAll("A", null).size());
91         assertEquals(0, getter.getAll("Z", null).size());
92
93         keyMap.put(keyZ, keyZ);
94         assertEquals(keyW002, getter.getAll("W", "0.0.2").iterator().next());
95     }
96 }