Merge "fix oauth code"
[ccsdk/features.git] / sdnr / wt / mountpoint-registrar / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / mountpointregistrar / test / consumer / TestStrimziKafkaCMVESMsgConsumer.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. 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 distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.consumer;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.fail;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.URISyntaxException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Iterator;
33 import java.util.Map;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.InvalidMessageException;
38 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.config.GeneralConfigForTest;
39 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.config.TestStrimziKafkaConfig;
40 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.vesdomain.cm.StrimziKafkaCMVESMsgConsumer;
41
42 public class TestStrimziKafkaCMVESMsgConsumer {
43
44     private static final String CONFIGURATION_FILE = "cm_test.properties";
45     private StrimziKafkaCMVESMsgConsumer sKafkaCMVESMsgConsumer;
46     private GeneralConfigForTest generalConfigForTest;
47     private TestStrimziKafkaConfig strimziKafkaConfigForTest;
48
49     @Before
50     public void setUp() throws Exception {
51         generalConfigForTest = new GeneralConfigForTest(CONFIGURATION_FILE);
52         strimziKafkaConfigForTest = new TestStrimziKafkaConfig(CONFIGURATION_FILE);
53         sKafkaCMVESMsgConsumer = new StrimziKafkaCMVESMsgConsumer(generalConfigForTest.getCfg(), null);
54     }
55
56     @Test
57     public void processValidMsg() throws URISyntaxException, IOException {
58         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_valid.json").toURI());
59         String cmEvent = readFileToString(cmFileValid);
60         try {
61             sKafkaCMVESMsgConsumer.processMsg(cmEvent);
62         } catch (Exception e) {
63             fail("Test fail with message: " + e.getMessage());
64         }
65     }
66
67     @Test(expected = InvalidMessageException.class)
68     public void processMsgThatMissesField() throws URISyntaxException, IOException, InvalidMessageException {
69         File cmFileInvalid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_invalid.json").toURI());
70         String cmEvent = readFileToString(cmFileInvalid);
71         sKafkaCMVESMsgConsumer.processMsg(cmEvent);
72     }
73
74     @Test(expected = InvalidMessageException.class)
75     public void processMsgThatHasInvalidNotificationType()
76         throws URISyntaxException, IOException, InvalidMessageException {
77         File cmFileInvalid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_invalid_type.json").toURI());
78         String cmEvent = readFileToString(cmFileInvalid);
79         sKafkaCMVESMsgConsumer.processMsg(cmEvent);
80     }
81
82     @Test(expected = JsonProcessingException.class)
83     public void processMsgThatIsNotValidJson() throws URISyntaxException, IOException, InvalidMessageException {
84         File cmFileInvalid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/not_a_json.json").toURI());
85         String cmEvent = readFileToString(cmFileInvalid);
86         sKafkaCMVESMsgConsumer.processMsg(cmEvent);
87     }
88
89     @Test
90     public void processMsgWithOneElementMoiChangesArray() throws URISyntaxException, IOException {
91         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_valid.json").toURI());
92         String cmEvent = readFileToString(cmFileValid);
93         try {
94             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
95             Iterator<JsonNode> nodes = rootNode
96                 .at("/event/stndDefinedFields/data/moiChanges")
97                 .elements();
98             Map<String, String> payloadMap =
99                 sKafkaCMVESMsgConsumer.preparePayloadMapFromMoiChangesArray(rootNode, nodes);
100
101             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
102             assertEquals("0", payloadMap.get("@counter@"));
103             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
104             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
105             assertEquals("notifyMOIChanges", payloadMap.get("@notification-type@"));
106             assertEquals("123", payloadMap.get("@notification-id@"));
107             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
108             assertEquals("https://samsung.com/3GPP/simulation/network-function/ves=1", payloadMap.get("@path@"));
109             assertEquals("REPLACE", payloadMap.get("@operation@"));
110             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
111
112         } catch (Exception e) {
113             fail("Test fail with message: " + e.getMessage());
114         }
115     }
116
117     @Test
118     public void processMsgWithTwoElementMoiChangesArray() throws URISyntaxException, IOException {
119         File cmFileValid =
120             new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_valid_two_element_moi_changes_array.json")
121                 .toURI());
122         String cmEvent = readFileToString(cmFileValid);
123         try {
124             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
125             Iterator<JsonNode> nodes = rootNode
126                 .at("/event/stndDefinedFields/data/moiChanges")
127                 .elements();
128             Map<String, String> payloadMap =
129                 sKafkaCMVESMsgConsumer.preparePayloadMapFromMoiChangesArray(rootNode, nodes);
130
131             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
132             assertEquals("0", payloadMap.get("@counter@"));
133             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
134             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
135             assertEquals("notifyMOIChanges", payloadMap.get("@notification-type@"));
136             assertEquals("123", payloadMap.get("@notification-id@"));
137             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
138             assertEquals("https://samsung.com/3GPP/simulation/network-function/ves=1", payloadMap.get("@path@"));
139             assertEquals("REPLACE", payloadMap.get("@operation@"));
140             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
141
142             Map<String, String> payloadMap2 = null;
143             while (nodes.hasNext()) {
144                 payloadMap2 = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoiChangesArray(rootNode, nodes);
145             }
146             assertEquals("samsung-O-DU-1122", payloadMap2.get("@node-id@"));
147             assertEquals("124", payloadMap2.get("@notification-id@"));
148             assertEquals("RESOURCE_OPERATION", payloadMap2.get("@source-indicator@"));
149             assertEquals("https://samsung.com/3GPP/simulation/network-function/ves=2", payloadMap2.get("@path@"));
150             assertEquals("CREATE", payloadMap2.get("@operation@"));
151             assertEquals("{pnf-registration:false,faults-enabled:false}", payloadMap2.get("@value@"));
152
153         } catch (Exception e) {
154             fail("Test fail with message: " + e.getMessage());
155         }
156     }
157
158     @Test
159     public void processMsgNotifyMoiCreationType() throws URISyntaxException, IOException {
160         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_moi_creation.json").toURI());
161         String cmEvent = readFileToString(cmFileValid);
162         try {
163             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
164             Map<String, String> payloadMap = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeList");
165             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
166             assertEquals("0", payloadMap.get("@counter@"));
167             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
168             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
169             assertEquals("notifyMOICreation", payloadMap.get("@notification-type@"));
170             assertNull(payloadMap.get("@notification-id@"));
171             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
172             assertNull(payloadMap.get("@path@"));
173             assertEquals("NULL", payloadMap.get("@operation@"));
174             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
175
176         } catch (Exception e) {
177             fail("Test fail with message: " + e.getMessage());
178         }
179     }
180
181     @Test
182     public void processMsgNotifyMoiDeletionType() throws URISyntaxException, IOException {
183         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_moi_deletion.json").toURI());
184         String cmEvent = readFileToString(cmFileValid);
185         try {
186             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
187             Map<String, String> payloadMap = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeList");
188             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
189             assertEquals("0", payloadMap.get("@counter@"));
190             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
191             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
192             assertEquals("notifyMOIDeletion", payloadMap.get("@notification-type@"));
193             assertNull(payloadMap.get("@notification-id@"));
194             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
195             assertNull(payloadMap.get("@path@"));
196             assertEquals("NULL", payloadMap.get("@operation@"));
197             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
198
199         } catch (Exception e) {
200             fail("Test fail with message: " + e.getMessage());
201         }
202     }
203
204     @Test
205     public void processMsgNotifyMoiAttributeValueChangesType() throws URISyntaxException, IOException {
206         File cmFileValid =
207             new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_moi_attribute_value_changes.json").toURI());
208         String cmEvent = readFileToString(cmFileValid);
209         try {
210             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
211             Map<String, String> payloadMap = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeListValueChanges");
212             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
213             assertEquals("0", payloadMap.get("@counter@"));
214             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
215             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
216             assertEquals("notifyMOIAttributeValueChanges", payloadMap.get("@notification-type@"));
217             assertNull(payloadMap.get("@notification-id@"));
218             assertEquals("UNKNOWN", payloadMap.get("@source-indicator@"));
219             assertNull(payloadMap.get("@path@"));
220             assertEquals("NULL", payloadMap.get("@operation@"));
221             assertEquals("[{attributeNameValuePairSet:{faults-enabled:true}}]", payloadMap.get("@value@"));
222
223         } catch (Exception e) {
224             fail("Test fail with message: " + e.getMessage());
225         }
226     }
227
228     private String readFileToString(File file) throws IOException {
229         StringBuilder fileContent = new StringBuilder();
230         Files.lines(Paths.get(file.toURI())).forEach(fileContent::append);
231         return fileContent.toString();
232     }
233
234     private JsonNode convertMessageToJsonNode(String message) throws JsonProcessingException {
235         return new ObjectMapper().readTree(message);
236     }
237
238     @After
239     public void after() {
240         generalConfigForTest.close();
241     }
242 }