b00e7db9fbee5628687f75f6b9884469e0028206
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / concepts / AxConceptGetterImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
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.apex.model.basicmodel.concepts;
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 AxConceptGetterImplTest {
38
39     @Test
40     public void testAxConceptGetterImpl() {
41         NavigableMap<AxArtifactKey, AxArtifactKey> keyMap = new TreeMap<>();
42
43         AxConceptGetterImpl<AxArtifactKey> getter = new AxConceptGetterImpl<>(keyMap);
44         assertNotNull(getter);
45         
46         AxArtifactKey keyA = new AxArtifactKey("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         AxArtifactKey keyZ = new AxArtifactKey("Z", "0.0.1");
60         keyMap.put(keyZ, keyZ);
61         assertNull(getter.get("W"));
62
63         AxArtifactKey keyW001 = new AxArtifactKey("W", "0.0.1");
64         keyMap.put(keyW001, keyW001);
65         assertEquals(keyW001, getter.get("W"));
66
67         AxArtifactKey keyW002 = new AxArtifactKey("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<AxArtifactKey>(keyMap.values()), getter.getAll(null));
86         assertEquals(new TreeSet<AxArtifactKey>(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 }