Sonar coverage vnftools - VnfTools
[sdnc/northbound.git] / vnftools / provider / src / test / java / org / onap / sdnc / vnftools / VnfToolsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.sdnc.vnftools;
23
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
30
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Properties;
34
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.times;
37
38 public class VnfToolsTest {
39     private SvcLogicContext mockSvcLogicContext = mock(SvcLogicContext.class);
40
41     private VnfTools vnfTools;
42
43     @Before
44     public void setUp() throws Exception {
45         vnfTools = new VnfTools(null);
46     }
47
48     @Test
49     public void testConstructor() throws Exception {
50         VnfTools vTools = new VnfTools(null);
51         Assert.assertTrue("Should have no impact with null property", vTools != null);
52         vTools = new VnfTools(new Properties());
53         Assert.assertTrue("Should have created", vTools != null);
54     }
55
56     @Test(expected = SvcLogicException.class)
57     public void testCheckIfActivateReadyFailure() throws Exception {
58         vnfTools.checkIfActivateReady(null, mockSvcLogicContext);
59     }
60
61     @Test
62     public void testCheckIfActivateReady() throws Exception {
63         String value = "testing";
64         Map<String, String> parameters = new HashMap<>();
65         parameters.put(VnfTools.RETURN_KEY, value);
66         vnfTools.checkIfActivateReady(parameters, mockSvcLogicContext);
67         Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(value, VnfTools.TRUE_STRING);
68     }
69
70     @Test(expected = SvcLogicException.class)
71     public void testStringContainsFailure() throws Exception {
72         vnfTools.stringContains(null, mockSvcLogicContext);
73     }
74
75     @Test
76     public void testStringContains() throws Exception {
77         String value = "result ctx string";
78         String stringToFindValue = "testing";
79         String stringToSearchValue = "testing 1234";
80         Map<String, String> parameters = new HashMap<>();
81         parameters.put(VnfTools.RESULT_CTX_STRING, value);
82         parameters.put(VnfTools.STRING_TO_FIND, stringToFindValue);
83         parameters.put(VnfTools.STRING_TO_SEARCH, stringToSearchValue);
84
85         vnfTools.stringContains(parameters, mockSvcLogicContext);
86         Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(
87                 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
88
89         stringToFindValue = "1234";
90         vnfTools.stringContains(parameters, mockSvcLogicContext);
91         Mockito.verify(mockSvcLogicContext, times(2)).setAttribute(
92                 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
93     }
94
95     @Test
96     public void testGenerateNameFailure() throws Exception {
97         try {
98             vnfTools.generateName(null, mockSvcLogicContext);
99             Assert.fail("should have throw SvcLogicException");
100         } catch (SvcLogicException e) {
101             Assert.assertFalse("Should be validation error",
102                     e.getMessage().contains("needs at least length 4 but only have"));
103         }
104     }
105
106     @Test
107     public void testGenerateNameFailWithShortBaseParam() throws Exception {
108         String value = "return path";
109         String base = "123";
110         String suffix = "suffix";
111         Map<String, String> parameters = new HashMap<>();
112         parameters.put(VnfTools.RETURN_PATH, value);
113         parameters.put(VnfTools.BASE, base);
114         parameters.put(VnfTools.SUFFIX, suffix);
115
116         try {
117             vnfTools.generateName(parameters, mockSvcLogicContext);
118             Assert.fail("should have throw SvcLogicException");
119         } catch (SvcLogicException e) {
120             Assert.assertTrue("Should be length error",
121                     e.getMessage().contains("needs at least length 4 but only have"));
122         }
123     }
124
125     @Test
126     public void testGenerateName() throws Exception {
127         String value = "return path";
128         String base = "1234567890";
129         String suffix = "suffix";
130         Map<String, String> parameters = new HashMap<>();
131         parameters.put(VnfTools.RETURN_PATH, value);
132         parameters.put(VnfTools.BASE, base);
133         parameters.put(VnfTools.SUFFIX, suffix);
134
135         vnfTools.generateName(parameters, mockSvcLogicContext);
136         String expectedValue = String.format("%s%s%s",
137                 base.substring(0, base.length() - 4), suffix, base.substring(base.length() - 2));
138         Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(value, expectedValue);
139     }
140
141     @Test
142     public void testPrintContextInParamNullFailure() throws Exception {
143         try {
144             vnfTools.printContext(null, mockSvcLogicContext);
145             Assert.fail("should have throw SvcLogicException");
146         } catch(SvcLogicException e) {
147             Assert.assertEquals("Should be no param error", "no parameters passed", e.getMessage());
148         }
149     }
150
151     @Test
152     public void testPrintContextFileNameFailure() throws Exception {
153         String expectedEmessage = "printContext requires 'filename' parameter";
154         Map<String, String> parameters = new HashMap<>();
155         try {
156             vnfTools.printContext(parameters, mockSvcLogicContext);
157             Assert.fail("should have throw SvcLogicException");
158         } catch(SvcLogicException e) {
159             Assert.assertEquals("Should be missing filename error", expectedEmessage, e.getMessage());
160         }
161
162         parameters.put(VnfTools.FILENAME, "");
163         try {
164             vnfTools.printContext(parameters, mockSvcLogicContext);
165             Assert.fail("should have throw SvcLogicException");
166         } catch(SvcLogicException e) {
167             Assert.assertEquals("Should still be missing filename error", expectedEmessage, e.getMessage());
168         }
169     }
170
171     @Test
172     public void testPrintContext() throws Exception {
173         Map<String, String> parameters = new HashMap<>();
174         parameters.put(VnfTools.FILENAME, "abc");
175         vnfTools.printContext(parameters, mockSvcLogicContext);
176     }
177
178     @Test
179     public void testGetArrayLengthInvalidInt() throws Exception {
180         String key = "abc";
181         Mockito.doReturn("efg").when(mockSvcLogicContext).getAttribute(key);
182         int result = VnfTools.getArrayLength(mockSvcLogicContext, key);
183         Assert.assertEquals("Should return 0 for string value", 0, result);
184     }
185
186     @Test
187     public void testGetArrayLength() throws Exception {
188         String key = "abc";
189         String value = "234";
190         Mockito.doReturn(value).when(mockSvcLogicContext).getAttribute(key);
191         int result = VnfTools.getArrayLength(mockSvcLogicContext, key);
192         Assert.assertEquals("Should return the value int", Integer.parseInt(value), result);
193     }
194
195     @Test
196     public void testGetArrayLengthWithDebugInvalidInt() throws Exception {
197         String key = "abc";
198         Mockito.doReturn("efg").when(mockSvcLogicContext).getAttribute(key);
199         int result = VnfTools.getArrayLength(mockSvcLogicContext, key, "debug");
200         Assert.assertEquals("Should return 0 for string value", 0, result);
201     }
202
203     @Test
204     public void testGetArrayLengthWithDebug() throws Exception {
205         String key = "abc";
206         String value = "234";
207         Mockito.doReturn(value).when(mockSvcLogicContext).getAttribute(key);
208         int result = VnfTools.getArrayLength(mockSvcLogicContext, key, "debug");
209         Assert.assertEquals("Should return the value int", Integer.parseInt(value), result);
210     }
211
212 }