aboutsummaryrefslogtreecommitdiffstats
path: root/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java
blob: 55f59f6407df836b7f9411405680b70122698529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.base;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.junit.Test;
import org.onap.policy.models.base.testconcepts.DummyAuthorativeConcept;
import org.onap.policy.models.base.testconcepts.DummyBadPfConceptContainer;
import org.onap.policy.models.base.testconcepts.DummyPfConcept;
import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;

/**
 * Test the PfCOnceptCOntainer class.
 *
 * @author Liam Fallon (liam.fallon@est.tech)
 */
public class PfConceptContainerTest {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Test
    public void testConceptContainer() {
        DummyPfConceptContainer container = new DummyPfConceptContainer();
        assertNotNull(container);

        container = new DummyPfConceptContainer();
        assertNotNull(container);

        container = new DummyPfConceptContainer(new PfConceptKey());
        assertNotNull(container);

        container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
        assertNotNull(container);

        try {
            new PfConceptContainer((PfConceptKey) null, null);
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("key is marked @NonNull but is null", exc.getMessage());
        }

        try {
            container = new DummyPfConceptContainer((PfConceptKey) null, null);
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("key is marked @NonNull but is null", exc.getMessage());
        }

        try {
            container = new DummyPfConceptContainer(new PfConceptKey(), null);
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage());
        }

        try {
            container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>());
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("key is marked @NonNull but is null", exc.getMessage());
        }

        container.getKey().setName("Dummy");
        DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
        assertNotNull(clonedContainer);
        assertEquals("Dummy", clonedContainer.getKey().getName());

        try {
            DummyPfConceptContainer conceptContainter = null;
            container = new DummyPfConceptContainer(conceptContainter);
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
        }

        List<PfKey> keyList = container.getKeys();
        assertEquals(1, keyList.size());

        PfConceptKey conceptKey = new PfConceptKey("Key", "0.0.1");
        Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
        conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));

        container.setConceptMap(conceptMap);
        keyList = container.getKeys();
        assertEquals(2, keyList.size());

        clonedContainer = new DummyPfConceptContainer(container);
        assertNotNull(clonedContainer);
        assertEquals("Dummy", clonedContainer.getKey().getName());
        assertEquals(2, clonedContainer.getKeys().size());

        assertEquals(clonedContainer, container);
        container.clean();
        assertEquals(clonedContainer, container);

        PfValidationResult result = new PfValidationResult();
        result = container.validate(result);
        assertTrue(result.isOk());

        assertEquals(0, container.compareTo(clonedContainer));

        try {
            container.copyTo(null);
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("target is marked @NonNull but is null", exc.getMessage());
        }

        assertFalse(container.compareTo(null) == 0);
        assertEquals(0, container.compareTo(container));
        assertFalse(container.compareTo(conceptKey) == 0);

        DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
        testContainer.getKey().setVersion("0.0.2");
        assertFalse(container.compareTo(testContainer) == 0);
        testContainer.getKey().setVersion(container.getKey().getVersion());
        assertEquals(0, container.compareTo(testContainer));

        PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1");
        testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
        assertFalse(container.compareTo(testContainer) == 0);

        try {
            container.validate(null);
            fail("test should throw an exception here");
        } catch (Exception exc) {
            assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
        }

        DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
        validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1"));
        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());

        validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());

        validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
        validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());

        validateContainer.getConceptMap().put(testConceptKey, null);
        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
        validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());

        validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
        validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());

        assertEquals(conceptKey, container.get(conceptKey).getKey());
        assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
        assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());

        Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
        assertEquals(conceptKey, returnSet.iterator().next().getKey());

        returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
        assertEquals(conceptKey, returnSet.iterator().next().getKey());

        container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
    }

    @Test
    public void testAuthorative() {
        Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
        dacMap.put("name0", new DummyAuthorativeConcept("name0", "1.2.3", "Hello"));
        dacMap.put("name1", new DummyAuthorativeConcept(null, null, "Hi"));
        dacMap.put("name2", new DummyAuthorativeConcept("name2", "1.2.3", "Howdy"));

        List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
        authorativeList.add(dacMap);

        DummyPfConceptContainer container = new DummyPfConceptContainer();
        container.fromAuthorative(authorativeList);

        assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
        assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:0.0.0")).getDescription());
        assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());

        List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();

        assertEquals(dacMap.get("name0"), outMapList.get(0).get("name0"));
        assertEquals(dacMap.get("name1").getDescription(), outMapList.get(0).get("name1").getDescription());
        assertEquals(dacMap.get("name2"), outMapList.get(0).get("name2"));

        DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
        assertThatThrownBy(() -> {
            badContainer.fromAuthorative(authorativeList);
        }).hasMessage("failed to instantiate instance of container concept class");

        authorativeList.clear();
        assertThatThrownBy(() -> {
            container.fromAuthorative(authorativeList);
        }).hasMessage("An incoming list of concepts must have at least one entry");
    }

    @Test(expected = NullPointerException.class)
    public void testnullKey() {
        PfConceptKey nullKey = null;
        new DummyPfConceptContainer(nullKey);
    }
}