3e1929bf5ff8469af15399639a30e7316bd095b7
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SLI
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.adapter.ansible.model;
24
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import org.json.JSONObject;
29 import org.junit.Test;
30 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleMessageParser;
31 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleResult;
32 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleServerEmulator;
33
34 import static org.junit.Assert.assertNotNull;
35
36 public class TestAnsibleAdapter {
37
38     @Test
39     public void callPrivateConstructorsMethodsForCodeCoverage()
40             throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException,
41             InvocationTargetException {
42
43         /* test constructors */
44         Class<?>[] classesOne = {AnsibleMessageParser.class};
45         for (Class<?> clazz : classesOne) {
46             Constructor<?> constructor = clazz.getDeclaredConstructor();
47             constructor.setAccessible(true);
48             assertNotNull(constructor.newInstance());
49         }
50         Class<?>[] classesTwo = {AnsibleServerEmulator.class};
51         for (Class<?> clazz : classesTwo) {
52             Constructor<?> constructor = clazz.getDeclaredConstructor();
53             constructor.setAccessible(true);
54             assertNotNull(constructor.newInstance());
55         }
56         Class<?>[] classesThree = {AnsibleResult.class};
57         for (Class<?> clazz : classesThree) {
58             Constructor<?> constructor = clazz.getDeclaredConstructor();
59             constructor.setAccessible(true);
60             assertNotNull(constructor.newInstance());
61         }
62
63         /* test methods */
64         AnsibleMessageParser ansibleMessageParser = new AnsibleMessageParser();
65         Class<?>[] parameterTypes = new Class[1];
66         parameterTypes[0] = java.lang.String.class;
67
68         Method m = ansibleMessageParser.getClass().getDeclaredMethod("getFilePayload", parameterTypes);
69         m.setAccessible(true);
70         assertNotNull(m.invoke(ansibleMessageParser, "{\"test\": test}"));
71
72         // test logging-suppression for an invalid host value (Fortify Log Forging fix)
73         String input = "{"
74                        + "  \"Results\": {"
75                        + "    \"192.168.1.10\": {"
76                        + "      \"Id\": \"101\","
77                        + "      \"StatusCode\": 200,"
78                        + "      \"StatusMessage\": \"SUCCESS\""
79                        + "    },"
80                        + "    \"192%168%1%10\": {"
81                        + "      \"Id\": \"102\","
82                        + "      \"StatusCode\": 200,"
83                        + "      \"StatusMessage\": \"SUCCESS\""
84                        + "    },"
85                        + "    \"server-dev.att.com\": {"
86                        + "      \"Id\": \"103\","
87                        + "      \"StatusCode\": 200,"
88                        + "      \"StatusMessage\": \"SUCCESS\""
89                        + "    }"
90                        + "  },"
91                        + "  \"StatusCode\": 200,"
92                        + "  \"StatusMessage\": \"FINISHED\""
93                        + "}";
94         Method m2 = ansibleMessageParser.getClass().getDeclaredMethod("parseGetResponseNested", AnsibleResult.class, JSONObject.class);
95         m2.setAccessible(true);
96         m2.invoke(ansibleMessageParser, new AnsibleResult(), new JSONObject(input));
97     }
98
99 }
100