1783fc00fe34a59ff494bd05f5a9e90687e5085a
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2022 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.handling;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.util.UUID;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
37
38 public class SupportConceptGetterTest {
39
40     private static final AxKeyInfo intKI01 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey01", "0.0.1"),
41             UUID.randomUUID(), "IntegerKIKey01 description");
42     private static final AxKeyInfo intKI11 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey11", "0.0.1"),
43             UUID.randomUUID(), "IntegerKIKey11 description");
44     private static final AxKeyInfo intKI21 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey21", "0.0.1"),
45             UUID.randomUUID(), "IntegerKIKey21 description");
46     private static final AxKeyInfo intKI22 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey22", "0.0.2"),
47             UUID.randomUUID(), "IntegerKIKey22 description");
48     private static final AxKeyInfo intKI23 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey23", "0.0.3"),
49             UUID.randomUUID(), "IntegerKIKey23 description");
50     private static final AxKeyInfo intKI24 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey24", "0.0.4"),
51             UUID.randomUUID(), "IntegerKIKey24 description");
52     private static final AxKeyInfo intKI25 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey25", "0.0.5"),
53             UUID.randomUUID(), "IntegerKIKey25 description");
54     private static final AxKeyInfo intKI26 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey26", "0.0.6"),
55             UUID.randomUUID(), "IntegerKIKey26 description");
56     private static final AxKeyInfo intKI31 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey31", "0.0.1"),
57             UUID.randomUUID(), "IntegerKIKey31 description");
58     private static final AxKeyInfo intKI41 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey41", "0.0.1"),
59             UUID.randomUUID(), "IntegerKIKey41 description");
60     private static final AxKeyInfo intKI51 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey51", "0.0.1"),
61             UUID.randomUUID(), "IntegerKIKey51 description");
62     private static final AxKeyInfo intKI52 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey52", "0.0.2"),
63             UUID.randomUUID(), "IntegerKIKey52 description");
64     private static final AxKeyInfo intKI53 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey53", "0.0.3"),
65             UUID.randomUUID(), "IntegerKIKey53 description");
66     private static final AxKeyInfo intKI54 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey54", "0.0.4"),
67             UUID.randomUUID(), "IntegerKIKey54 description");
68     private static final AxKeyInfo intKI61 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey61", "0.0.1"),
69             UUID.randomUUID(), "IntegerKIKey61 description");
70     private static final AxKeyInfo intKI62 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey62", "0.0.2"),
71             UUID.randomUUID(), "IntegerKIKey62 description");
72     private static final AxKeyInfo intKI63 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey63", "0.0.3"),
73             UUID.randomUUID(), "IntegerKIKey63 description");
74     private static final AxKeyInfo intKI64 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey64", "0.0.4"),
75             UUID.randomUUID(), "IntegerKIKey64 description");
76     private static final AxKeyInfo intKI71 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey71", "0.0.1"),
77             UUID.randomUUID(), "IntegerKIKey71 description");
78     private static final AxKeyInfo intKI81 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey81", "0.0.1"),
79             UUID.randomUUID(), "IntegerKIKey81 description");
80     private static final AxKeyInfo intKI91 = new AxKeyInfo(new AxArtifactKey("IntegerKIKey91", "0.0.1"),
81             UUID.randomUUID(), "IntegerKIKey91 description");
82     private static final AxKeyInfo floatKI01 = new AxKeyInfo(new AxArtifactKey("FloatKIKey01", "0.0.1"),
83             UUID.randomUUID(), "IntegerKIKey01 description");
84     private static final AxKeyInfo floatKI11 = new AxKeyInfo(new AxArtifactKey("FloatKIKey11", "0.0.1"),
85             UUID.randomUUID(), "IntegerKIKey11 description");
86     private static final AxKeyInfo floatKI21 = new AxKeyInfo(new AxArtifactKey("FloatKIKey21", "0.0.1"),
87             UUID.randomUUID(), "IntegerKIKey21 description");
88     private static final AxKeyInfo floatKI31 = new AxKeyInfo(new AxArtifactKey("FloatKIKey31", "0.0.1"),
89             UUID.randomUUID(), "IntegerKIKey31 description");
90     private static final AxKeyInfo floatKI41 = new AxKeyInfo(new AxArtifactKey("FloatKIKey41", "0.0.1"),
91             UUID.randomUUID(), "IntegerKIKey41 description");
92     private static final AxKeyInfo floatKI51 = new AxKeyInfo(new AxArtifactKey("FloatKIKey51", "0.0.1"),
93             UUID.randomUUID(), "IntegerKIKey51 description");
94     private static final AxKeyInfo floatKI61 = new AxKeyInfo(new AxArtifactKey("FloatKIKey61", "0.0.1"),
95             UUID.randomUUID(), "IntegerKIKey61 description");
96     private static final AxKeyInfo floatKI71 = new AxKeyInfo(new AxArtifactKey("FloatKIKey71", "0.0.1"),
97             UUID.randomUUID(), "IntegerKIKey71 description");
98     private static final AxKeyInfo floatKI81 = new AxKeyInfo(new AxArtifactKey("FloatKIKey81", "0.0.1"),
99             UUID.randomUUID(), "IntegerKIKey81 description");
100     private static final AxKeyInfo floatKI82 = new AxKeyInfo(new AxArtifactKey("FloatKIKey82", "0.0.2"),
101             UUID.randomUUID(), "IntegerKIKey82 description");
102     private static final AxKeyInfo floatKI83 = new AxKeyInfo(new AxArtifactKey("FloatKIKey83", "0.0.3"),
103             UUID.randomUUID(), "IntegerKIKey83 description");
104     private static final AxKeyInfo floatKI91 = new AxKeyInfo(new AxArtifactKey("FloatKIKey91", "0.0.1"),
105             UUID.randomUUID(), "IntegerKIKey91 description");
106     private static final AxKeyInfo floatKI92 = new AxKeyInfo(new AxArtifactKey("FloatKIKey92", "0.0.2"),
107             UUID.randomUUID(), "IntegerKIKey92 description");
108     private static final AxKeyInfo floatKI93 = new AxKeyInfo(new AxArtifactKey("FloatKIKey93", "0.0.3"),
109             UUID.randomUUID(), "IntegerKIKey93 description");
110
111     @Test
112     public void testConceptGetterInteger() {
113         AxModel basicModel = setTestBasicModel();
114
115         assertNull(basicModel.getKeyInformation().get("NonExistantKey", "0.0.6"));
116         assertEquals(intKI26, basicModel.getKeyInformation().get("IntegerKIKey26", "0.0.6"));
117         assertEquals(intKI62, basicModel.getKeyInformation().get("IntegerKIKey62", "0.0.2"));
118         assertEquals(intKI21, basicModel.getKeyInformation().get("IntegerKIKey21", "0.0.1"));
119         assertEquals(intKI61, basicModel.getKeyInformation().get("IntegerKIKey61", "0.0.1"));
120
121         assertNull(basicModel.getKeyInformation().get("NonExistantKey"));
122
123         assertEquals(intKI01, basicModel.getKeyInformation().get("IntegerKIKey01"));
124         assertEquals(intKI11, basicModel.getKeyInformation().get("IntegerKIKey11"));
125         assertEquals(intKI26, basicModel.getKeyInformation().get("IntegerKIKey26"));
126         assertEquals(intKI31, basicModel.getKeyInformation().get("IntegerKIKey31"));
127         assertEquals(intKI41, basicModel.getKeyInformation().get("IntegerKIKey41"));
128         assertEquals(intKI54, basicModel.getKeyInformation().get("IntegerKIKey54"));
129         assertEquals(intKI64, basicModel.getKeyInformation().get("IntegerKIKey64"));
130         assertEquals(intKI71, basicModel.getKeyInformation().get("IntegerKIKey71"));
131         assertEquals(intKI81, basicModel.getKeyInformation().get("IntegerKIKey81"));
132         assertEquals(intKI91, basicModel.getKeyInformation().get("IntegerKIKey91"));
133     }
134
135     @Test
136     public void testConceptGetterFloat() {
137         AxModel basicModel = setTestBasicModel();
138
139         assertEquals(floatKI01, basicModel.getKeyInformation().get("FloatKIKey01"));
140         assertEquals(floatKI11, basicModel.getKeyInformation().get("FloatKIKey11"));
141         assertEquals(floatKI21, basicModel.getKeyInformation().get("FloatKIKey21"));
142         assertEquals(floatKI31, basicModel.getKeyInformation().get("FloatKIKey31"));
143         assertEquals(floatKI41, basicModel.getKeyInformation().get("FloatKIKey41"));
144         assertEquals(floatKI51, basicModel.getKeyInformation().get("FloatKIKey51"));
145         assertEquals(floatKI61, basicModel.getKeyInformation().get("FloatKIKey61"));
146         assertEquals(floatKI71, basicModel.getKeyInformation().get("FloatKIKey71"));
147         assertEquals(floatKI83, basicModel.getKeyInformation().get("FloatKIKey83"));
148         assertEquals(floatKI93, basicModel.getKeyInformation().get("FloatKIKey93"));
149     }
150
151     @Test
152     public void testMarshalling() throws IOException, ApexException {
153         AxModel basicModel = setTestBasicModel();
154
155         // Ensure marshalling and unmarshalling is OK
156         ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class);
157         ApexModelFileWriter<AxModel> modelWriter = new ApexModelFileWriter<AxModel>(true);
158
159         modelReader.setValidate(false);
160         modelWriter.setValidate(false);
161
162         File tempJsonFile = File.createTempFile("ApexModel", "json");
163         modelWriter.apexModelWriteJsonFile(basicModel, AxModel.class, tempJsonFile.getCanonicalPath());
164
165         FileInputStream jsonFileInputStream = new FileInputStream(tempJsonFile);
166         AxModel readJsonModel = modelReader.read(jsonFileInputStream);
167         jsonFileInputStream.close();
168         assertEquals(basicModel, readJsonModel);
169         assertEquals(intKI91, readJsonModel.getKeyInformation().get("IntegerKIKey91"));
170         assertNotNull(readJsonModel.getKeyInformation().get("FloatKIKey"));
171         tempJsonFile.delete();
172     }
173
174     private AxModel setTestBasicModel() {
175         AxModel basicModel = new DummyApexBasicModelCreator().getModel();
176         assertNotNull(basicModel);
177
178         basicModel.getKeyInformation().getKeyInfoMap().put(intKI31.getKey(), intKI31);
179         basicModel.getKeyInformation().getKeyInfoMap().put(intKI24.getKey(), intKI24);
180         basicModel.getKeyInformation().getKeyInfoMap().put(intKI11.getKey(), intKI11);
181         basicModel.getKeyInformation().getKeyInfoMap().put(intKI64.getKey(), intKI64);
182         basicModel.getKeyInformation().getKeyInfoMap().put(intKI41.getKey(), intKI41);
183         basicModel.getKeyInformation().getKeyInfoMap().put(intKI51.getKey(), intKI51);
184         basicModel.getKeyInformation().getKeyInfoMap().put(intKI23.getKey(), intKI23);
185         basicModel.getKeyInformation().getKeyInfoMap().put(intKI81.getKey(), intKI81);
186         basicModel.getKeyInformation().getKeyInfoMap().put(intKI71.getKey(), intKI71);
187         basicModel.getKeyInformation().getKeyInfoMap().put(intKI01.getKey(), intKI01);
188         basicModel.getKeyInformation().getKeyInfoMap().put(intKI91.getKey(), intKI91);
189         basicModel.getKeyInformation().getKeyInfoMap().put(intKI52.getKey(), intKI52);
190         basicModel.getKeyInformation().getKeyInfoMap().put(intKI53.getKey(), intKI53);
191         basicModel.getKeyInformation().getKeyInfoMap().put(intKI62.getKey(), intKI62);
192         basicModel.getKeyInformation().getKeyInfoMap().put(intKI54.getKey(), intKI54);
193         basicModel.getKeyInformation().getKeyInfoMap().put(intKI26.getKey(), intKI26);
194         basicModel.getKeyInformation().getKeyInfoMap().put(intKI22.getKey(), intKI22);
195         basicModel.getKeyInformation().getKeyInfoMap().put(intKI25.getKey(), intKI25);
196         basicModel.getKeyInformation().getKeyInfoMap().put(intKI21.getKey(), intKI21);
197         basicModel.getKeyInformation().getKeyInfoMap().put(intKI61.getKey(), intKI61);
198         basicModel.getKeyInformation().getKeyInfoMap().put(intKI63.getKey(), intKI63);
199
200         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI11.getKey(), floatKI11);
201         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI83.getKey(), floatKI83);
202         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI51.getKey(), floatKI51);
203         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI71.getKey(), floatKI71);
204         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI21.getKey(), floatKI21);
205         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI81.getKey(), floatKI81);
206         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI92.getKey(), floatKI92);
207         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI91.getKey(), floatKI91);
208         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI01.getKey(), floatKI01);
209         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI82.getKey(), floatKI82);
210         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI61.getKey(), floatKI61);
211         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI41.getKey(), floatKI41);
212         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI31.getKey(), floatKI31);
213         basicModel.getKeyInformation().getKeyInfoMap().put(floatKI93.getKey(), floatKI93);
214
215         return basicModel;
216     }
217 }