Fix vnftools load issues
[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();
46     }
47
48     @Test
49     public void testConstructor() throws Exception {
50         VnfTools vTools = new VnfTools();
51         Assert.assertTrue("Should have created", vTools != null);
52     }
53
54     @Test(expected = SvcLogicException.class)
55     public void testCheckIfActivateReadyFailure() throws Exception {
56         vnfTools.checkIfActivateReady(null, mockSvcLogicContext);
57     }
58
59     @Test
60     public void testCheckIfActivateReady() throws Exception {
61         String value = "testing";
62         Map<String, String> parameters = new HashMap<>();
63         parameters.put(VnfTools.RETURN_KEY, value);
64         vnfTools.checkIfActivateReady(parameters, mockSvcLogicContext);
65         Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(value, VnfTools.TRUE_STRING);
66     }
67
68     @Test(expected = SvcLogicException.class)
69     public void testStringContainsFailure() throws Exception {
70         vnfTools.stringContains(null, mockSvcLogicContext);
71     }
72
73     @Test
74     public void testStringContains() throws Exception {
75         String value = "result ctx string";
76         String stringToFindValue = "testing";
77         String stringToSearchValue = "testing 1234";
78         Map<String, String> parameters = new HashMap<>();
79         parameters.put(VnfTools.RESULT_CTX_STRING, value);
80         parameters.put(VnfTools.STRING_TO_FIND, stringToFindValue);
81         parameters.put(VnfTools.STRING_TO_SEARCH, stringToSearchValue);
82
83         vnfTools.stringContains(parameters, mockSvcLogicContext);
84         Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(
85                 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
86
87         stringToFindValue = "1234";
88         vnfTools.stringContains(parameters, mockSvcLogicContext);
89         Mockito.verify(mockSvcLogicContext, times(2)).setAttribute(
90                 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
91     }
92
93     @Test
94     public void testGenerateNameFailure() throws Exception {
95         try {
96             vnfTools.generateName(null, mockSvcLogicContext);
97             Assert.fail("should have throw SvcLogicException");
98         } catch (SvcLogicException e) {
99             Assert.assertFalse("Should be validation error",
100                     e.getMessage().contains("needs at least length 4 but only have"));
101         }
102     }
103
104     @Test
105     public void testGenerateNameFailWithShortBaseParam() throws Exception {
106         String value = "return path";
107         String base = "123";
108         String suffix = "suffix";
109         Map<String, String> parameters = new HashMap<>();
110         parameters.put(VnfTools.RETURN_PATH, value);
111         parameters.put(VnfTools.BASE, base);
112         parameters.put(VnfTools.SUFFIX, suffix);
113
114         try {
115             vnfTools.generateName(parameters, mockSvcLogicContext);
116             Assert.fail("should have throw SvcLogicException");
117         } catch (SvcLogicException e) {
118             Assert.assertTrue("Should be length error",
119                     e.getMessage().contains("needs at least length 4 but only have"));
120         }
121     }
122
123     @Test
124     public void testGenerateName() throws Exception {
125         String value = "return path";
126         String base = "1234567890";
127         String suffix = "suffix";
128         Map<String, String> parameters = new HashMap<>();
129         parameters.put(VnfTools.RETURN_PATH, value);
130         parameters.put(VnfTools.BASE, base);
131         parameters.put(VnfTools.SUFFIX, suffix);
132
133         vnfTools.generateName(parameters, mockSvcLogicContext);
134         String expectedValue = String.format("%s%s%s",
135                 base.substring(0, base.length() - 4), suffix, base.substring(base.length() - 2));
136         Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(value, expectedValue);
137     }
138
139     @Test
140     public void testPrintContextInParamNullFailure() throws Exception {
141         try {
142             vnfTools.printContext(null, mockSvcLogicContext);
143             Assert.fail("should have throw SvcLogicException");
144         } catch(SvcLogicException e) {
145             Assert.assertEquals("Should be no param error", "no parameters passed", e.getMessage());
146         }
147     }
148
149     @Test
150     public void testPrintContextFileNameFailure() throws Exception {
151         String expectedEmessage = "printContext requires 'filename' parameter";
152         Map<String, String> parameters = new HashMap<>();
153         try {
154             vnfTools.printContext(parameters, mockSvcLogicContext);
155             Assert.fail("should have throw SvcLogicException");
156         } catch(SvcLogicException e) {
157             Assert.assertEquals("Should be missing filename error", expectedEmessage, e.getMessage());
158         }
159
160         parameters.put(VnfTools.FILENAME, "");
161         try {
162             vnfTools.printContext(parameters, mockSvcLogicContext);
163             Assert.fail("should have throw SvcLogicException");
164         } catch(SvcLogicException e) {
165             Assert.assertEquals("Should still be missing filename error", expectedEmessage, e.getMessage());
166         }
167     }
168
169     @Test
170     public void testPrintContext() throws Exception {
171         Map<String, String> parameters = new HashMap<>();
172         parameters.put(VnfTools.FILENAME, "target/testPrintContext.out");
173         vnfTools.printContext(parameters, mockSvcLogicContext);
174     }
175
176     @Test
177     public void testGetArrayLengthInvalidInt() throws Exception {
178         String key = "abc";
179         Mockito.doReturn("efg").when(mockSvcLogicContext).getAttribute(key);
180         int result = VnfTools.getArrayLength(mockSvcLogicContext, key);
181         Assert.assertEquals("Should return 0 for string value", 0, result);
182     }
183
184     @Test
185     public void testGetArrayLength() throws Exception {
186         String key = "abc";
187         String value = "234";
188         Mockito.doReturn(value).when(mockSvcLogicContext).getAttribute(key);
189         int result = VnfTools.getArrayLength(mockSvcLogicContext, key);
190         Assert.assertEquals("Should return the value int", Integer.parseInt(value), result);
191     }
192
193     @Test
194     public void testGetArrayLengthWithDebugInvalidInt() throws Exception {
195         String key = "abc";
196         Mockito.doReturn("efg").when(mockSvcLogicContext).getAttribute(key);
197         int result = VnfTools.getArrayLength(mockSvcLogicContext, key, "debug");
198         Assert.assertEquals("Should return 0 for string value", 0, result);
199     }
200
201     @Test
202     public void testGetArrayLengthWithDebug() throws Exception {
203         String key = "abc";
204         String value = "234";
205         Mockito.doReturn(value).when(mockSvcLogicContext).getAttribute(key);
206         int result = VnfTools.getArrayLength(mockSvcLogicContext, key, "debug");
207         Assert.assertEquals("Should return the value int", Integer.parseInt(value), result);
208     }
209
210 }