0cd7f022821e2851e3f4290bb50c2e130b2177a4
[ccsdk/features.git] /
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;
20
21 import com.fasterxml.jackson.core.JsonProcessingException;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.DMaaPCMVESMsgConsumer;
26 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.InvalidMessageException;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import static org.junit.Assert.fail;
34
35 public class TestDMaaPCMVESMsgConsumer {
36
37     private static final String CONFIGURATION_FILE = "cm_test.properties";
38     private DMaaPCMVESMsgConsumer dMaaPCMVESMsgConsumer;
39     private GeneralConfigForTest generalConfigForTest;
40
41     @Before
42     public void setUp() throws Exception {
43         generalConfigForTest = new GeneralConfigForTest(CONFIGURATION_FILE);
44         dMaaPCMVESMsgConsumer = new DMaaPCMVESMsgConsumer(generalConfigForTest.getCfg());
45     }
46
47     @Test
48     public void processValidMsg() throws URISyntaxException, IOException {
49         File cmFileValid = new File(TestDMaaPCMVESMsgConsumer.class.getResource("/msgs/cm_valid.json").toURI());
50         String cmEvent = readFileToString(cmFileValid);
51         try {
52             dMaaPCMVESMsgConsumer.processMsg(cmEvent);
53         } catch (Exception e) {
54             fail("Test fail with message: " + e.getMessage());
55         }
56     }
57
58     @Test(expected = InvalidMessageException.class)
59     public void processMsgThatMissesField() throws URISyntaxException, IOException, InvalidMessageException {
60         File cmFileInvalid = new File(TestDMaaPCMVESMsgConsumer.class.getResource("/msgs/cm_invalid.json").toURI());
61         String cmEvent = readFileToString(cmFileInvalid);
62         dMaaPCMVESMsgConsumer.processMsg(cmEvent);
63     }
64
65     @Test(expected = JsonProcessingException.class)
66     public void processMsgThatIsNotValidJson() throws URISyntaxException, IOException, InvalidMessageException {
67         File cmFileInvalid = new File(TestDMaaPCMVESMsgConsumer.class.getResource("/msgs/not_a_json.json").toURI());
68         String cmEvent = readFileToString(cmFileInvalid);
69         dMaaPCMVESMsgConsumer.processMsg(cmEvent);
70     }
71
72     private String readFileToString(File file) throws IOException {
73         StringBuilder fileContent = new StringBuilder();
74         Files.lines(Paths.get(file.toURI())).forEach(fileContent::append);
75         return fileContent.toString();
76     }
77
78     @After
79     public void after() {
80         generalConfigForTest.close();
81     }
82 }