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
 
  10  * http://www.apache.org/licenses/LICENSE-2.0
 
  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
 
  16  * ============LICENSE_END==========================================================================
 
  19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.consumer;
 
  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;
 
  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;
 
  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.vesdomain.cm.StrimziKafkaCMVESMsgConsumer;
 
  41 public class TestStrimziKafkaCMVESMsgConsumer {
 
  43     private static final String CONFIGURATION_FILE = "cm_test.properties";
 
  44     private StrimziKafkaCMVESMsgConsumer sKafkaCMVESMsgConsumer;
 
  45     private GeneralConfigForTest generalConfigForTest;
 
  48     public void setUp() throws Exception {
 
  49         generalConfigForTest = new GeneralConfigForTest(CONFIGURATION_FILE);
 
  50         sKafkaCMVESMsgConsumer = new StrimziKafkaCMVESMsgConsumer(generalConfigForTest.getCfg());
 
  54     public void processValidMsg() throws URISyntaxException, IOException {
 
  55         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_valid.json").toURI());
 
  56         String cmEvent = readFileToString(cmFileValid);
 
  58             sKafkaCMVESMsgConsumer.processMsg(cmEvent);
 
  59         } catch (Exception e) {
 
  60             fail("Test fail with message: " + e.getMessage());
 
  64     @Test(expected = InvalidMessageException.class)
 
  65     public void processMsgThatMissesField() throws URISyntaxException, IOException, InvalidMessageException {
 
  66         File cmFileInvalid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_invalid.json").toURI());
 
  67         String cmEvent = readFileToString(cmFileInvalid);
 
  68         sKafkaCMVESMsgConsumer.processMsg(cmEvent);
 
  71     @Test(expected = InvalidMessageException.class)
 
  72     public void processMsgThatHasInvalidNotificationType()
 
  73         throws URISyntaxException, IOException, InvalidMessageException {
 
  74         File cmFileInvalid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_invalid_type.json").toURI());
 
  75         String cmEvent = readFileToString(cmFileInvalid);
 
  76         sKafkaCMVESMsgConsumer.processMsg(cmEvent);
 
  79     @Test(expected = JsonProcessingException.class)
 
  80     public void processMsgThatIsNotValidJson() throws URISyntaxException, IOException, InvalidMessageException {
 
  81         File cmFileInvalid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/not_a_json.json").toURI());
 
  82         String cmEvent = readFileToString(cmFileInvalid);
 
  83         sKafkaCMVESMsgConsumer.processMsg(cmEvent);
 
  87     public void processMsgWithOneElementMoiChangesArray() throws URISyntaxException, IOException {
 
  88         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_valid.json").toURI());
 
  89         String cmEvent = readFileToString(cmFileValid);
 
  91             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
 
  92             Iterator<JsonNode> nodes = rootNode
 
  93                 .at("/event/stndDefinedFields/data/moiChanges")
 
  95             Map<String, String> payloadMap =
 
  96                 sKafkaCMVESMsgConsumer.preparePayloadMapFromMoiChangesArray(rootNode, nodes);
 
  98             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
 
  99             assertEquals("0", payloadMap.get("@counter@"));
 
 100             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
 
 101             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
 
 102             assertEquals("notifyMOIChanges", payloadMap.get("@notification-type@"));
 
 103             assertEquals("123", payloadMap.get("@notification-id@"));
 
 104             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
 
 105             assertEquals("https://samsung.com/3GPP/simulation/network-function/ves=1", payloadMap.get("@path@"));
 
 106             assertEquals("REPLACE", payloadMap.get("@operation@"));
 
 107             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
 
 109         } catch (Exception e) {
 
 110             fail("Test fail with message: " + e.getMessage());
 
 115     public void processMsgWithTwoElementMoiChangesArray() throws URISyntaxException, IOException {
 
 117             new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_valid_two_element_moi_changes_array.json")
 
 119         String cmEvent = readFileToString(cmFileValid);
 
 121             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
 
 122             Iterator<JsonNode> nodes = rootNode
 
 123                 .at("/event/stndDefinedFields/data/moiChanges")
 
 125             Map<String, String> payloadMap =
 
 126                 sKafkaCMVESMsgConsumer.preparePayloadMapFromMoiChangesArray(rootNode, nodes);
 
 128             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
 
 129             assertEquals("0", payloadMap.get("@counter@"));
 
 130             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
 
 131             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
 
 132             assertEquals("notifyMOIChanges", payloadMap.get("@notification-type@"));
 
 133             assertEquals("123", payloadMap.get("@notification-id@"));
 
 134             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
 
 135             assertEquals("https://samsung.com/3GPP/simulation/network-function/ves=1", payloadMap.get("@path@"));
 
 136             assertEquals("REPLACE", payloadMap.get("@operation@"));
 
 137             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
 
 139             Map<String, String> payloadMap2 = null;
 
 140             while (nodes.hasNext()) {
 
 141                 payloadMap2 = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoiChangesArray(rootNode, nodes);
 
 143             assertEquals("samsung-O-DU-1122", payloadMap2.get("@node-id@"));
 
 144             assertEquals("124", payloadMap2.get("@notification-id@"));
 
 145             assertEquals("RESOURCE_OPERATION", payloadMap2.get("@source-indicator@"));
 
 146             assertEquals("https://samsung.com/3GPP/simulation/network-function/ves=2", payloadMap2.get("@path@"));
 
 147             assertEquals("CREATE", payloadMap2.get("@operation@"));
 
 148             assertEquals("{pnf-registration:false,faults-enabled:false}", payloadMap2.get("@value@"));
 
 150         } catch (Exception e) {
 
 151             fail("Test fail with message: " + e.getMessage());
 
 156     public void processMsgNotifyMoiCreationType() throws URISyntaxException, IOException {
 
 157         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_moi_creation.json").toURI());
 
 158         String cmEvent = readFileToString(cmFileValid);
 
 160             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
 
 161             Map<String, String> payloadMap = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeList");
 
 162             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
 
 163             assertEquals("0", payloadMap.get("@counter@"));
 
 164             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
 
 165             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
 
 166             assertEquals("notifyMOICreation", payloadMap.get("@notification-type@"));
 
 167             assertNull(payloadMap.get("@notification-id@"));
 
 168             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
 
 169             assertNull(payloadMap.get("@path@"));
 
 170             assertEquals("NULL", payloadMap.get("@operation@"));
 
 171             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
 
 173         } catch (Exception e) {
 
 174             fail("Test fail with message: " + e.getMessage());
 
 179     public void processMsgNotifyMoiDeletionType() throws URISyntaxException, IOException {
 
 180         File cmFileValid = new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_moi_deletion.json").toURI());
 
 181         String cmEvent = readFileToString(cmFileValid);
 
 183             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
 
 184             Map<String, String> payloadMap = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeList");
 
 185             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
 
 186             assertEquals("0", payloadMap.get("@counter@"));
 
 187             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
 
 188             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
 
 189             assertEquals("notifyMOIDeletion", payloadMap.get("@notification-type@"));
 
 190             assertNull(payloadMap.get("@notification-id@"));
 
 191             assertEquals("MANAGEMENT_OPERATION", payloadMap.get("@source-indicator@"));
 
 192             assertNull(payloadMap.get("@path@"));
 
 193             assertEquals("NULL", payloadMap.get("@operation@"));
 
 194             assertEquals("{pnf-registration:true,faults-enabled:true}", payloadMap.get("@value@"));
 
 196         } catch (Exception e) {
 
 197             fail("Test fail with message: " + e.getMessage());
 
 202     public void processMsgNotifyMoiAttributeValueChangesType() throws URISyntaxException, IOException {
 
 204             new File(TestStrimziKafkaCMVESMsgConsumer.class.getResource("/msgs/cm_moi_attribute_value_changes.json").toURI());
 
 205         String cmEvent = readFileToString(cmFileValid);
 
 207             JsonNode rootNode = convertMessageToJsonNode(cmEvent);
 
 208             Map<String, String> payloadMap = sKafkaCMVESMsgConsumer.preparePayloadMapFromMoi(rootNode,"/event/stndDefinedFields/data/attributeListValueChanges");
 
 209             assertEquals("samsung-O-DU-1122", payloadMap.get("@node-id@"));
 
 210             assertEquals("0", payloadMap.get("@counter@"));
 
 211             assertEquals("2019-01-09T12:30:07.722Z", payloadMap.get("@timestamp@"));
 
 212             assertEquals("src_device_id_1732f1ad-53fd-4fd1-8b73-a677987d4e8f", payloadMap.get("@object-id@"));
 
 213             assertEquals("notifyMOIAttributeValueChanges", payloadMap.get("@notification-type@"));
 
 214             assertNull(payloadMap.get("@notification-id@"));
 
 215             assertEquals("UNKNOWN", payloadMap.get("@source-indicator@"));
 
 216             assertNull(payloadMap.get("@path@"));
 
 217             assertEquals("NULL", payloadMap.get("@operation@"));
 
 218             assertEquals("[{attributeNameValuePairSet:{faults-enabled:true}}]", payloadMap.get("@value@"));
 
 220         } catch (Exception e) {
 
 221             fail("Test fail with message: " + e.getMessage());
 
 225     private String readFileToString(File file) throws IOException {
 
 226         StringBuilder fileContent = new StringBuilder();
 
 227         Files.lines(Paths.get(file.toURI())).forEach(fileContent::append);
 
 228         return fileContent.toString();
 
 231     private JsonNode convertMessageToJsonNode(String message) throws JsonProcessingException {
 
 232         return new ObjectMapper().readTree(message);
 
 236     public void after() {
 
 237         generalConfigForTest.close();