2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.sdnc.vnftools;
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;
31 import java.util.HashMap;
33 import java.util.Properties;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.times;
38 public class VnfToolsTest {
39 private SvcLogicContext mockSvcLogicContext = mock(SvcLogicContext.class);
41 private VnfTools vnfTools;
44 public void setUp() throws Exception {
45 vnfTools = new VnfTools(null);
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);
56 @Test(expected = SvcLogicException.class)
57 public void testCheckIfActivateReadyFailure() throws Exception {
58 vnfTools.checkIfActivateReady(null, mockSvcLogicContext);
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);
70 @Test(expected = SvcLogicException.class)
71 public void testStringContainsFailure() throws Exception {
72 vnfTools.stringContains(null, mockSvcLogicContext);
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);
85 vnfTools.stringContains(parameters, mockSvcLogicContext);
86 Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(
87 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
89 stringToFindValue = "1234";
90 vnfTools.stringContains(parameters, mockSvcLogicContext);
91 Mockito.verify(mockSvcLogicContext, times(2)).setAttribute(
92 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
96 public void testGenerateNameFailure() throws Exception {
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"));
107 public void testGenerateNameFailWithShortBaseParam() throws Exception {
108 String value = "return path";
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);
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"));
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);
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);
142 public void testPrintContextInParamNullFailure() throws Exception {
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());
152 public void testPrintContextFileNameFailure() throws Exception {
153 String expectedEmessage = "printContext requires 'filename' parameter";
154 Map<String, String> parameters = new HashMap<>();
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());
162 parameters.put(VnfTools.FILENAME, "");
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());
172 public void testPrintContext() throws Exception {
173 Map<String, String> parameters = new HashMap<>();
174 parameters.put(VnfTools.FILENAME, "abc");
175 vnfTools.printContext(parameters, mockSvcLogicContext);
179 public void testGetArrayLengthInvalidInt() throws Exception {
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);
187 public void testGetArrayLength() throws Exception {
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);
196 public void testGetArrayLengthWithDebugInvalidInt() throws Exception {
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);
204 public void testGetArrayLengthWithDebug() throws Exception {
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);