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();
49 public void testConstructor() throws Exception {
50 VnfTools vTools = new VnfTools();
51 Assert.assertTrue("Should have created", vTools != null);
54 @Test(expected = SvcLogicException.class)
55 public void testCheckIfActivateReadyFailure() throws Exception {
56 vnfTools.checkIfActivateReady(null, mockSvcLogicContext);
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);
68 @Test(expected = SvcLogicException.class)
69 public void testStringContainsFailure() throws Exception {
70 vnfTools.stringContains(null, mockSvcLogicContext);
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);
83 vnfTools.stringContains(parameters, mockSvcLogicContext);
84 Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(
85 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
87 stringToFindValue = "1234";
88 vnfTools.stringContains(parameters, mockSvcLogicContext);
89 Mockito.verify(mockSvcLogicContext, times(2)).setAttribute(
90 value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
94 public void testGenerateNameFailure() throws Exception {
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"));
105 public void testGenerateNameFailWithShortBaseParam() throws Exception {
106 String value = "return path";
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);
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"));
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);
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);
140 public void testPrintContextInParamNullFailure() throws Exception {
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());
150 public void testPrintContextFileNameFailure() throws Exception {
151 String expectedEmessage = "printContext requires 'filename' parameter";
152 Map<String, String> parameters = new HashMap<>();
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());
160 parameters.put(VnfTools.FILENAME, "");
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());
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);
177 public void testGetArrayLengthInvalidInt() throws Exception {
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);
185 public void testGetArrayLength() throws Exception {
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);
194 public void testGetArrayLengthWithDebugInvalidInt() throws Exception {
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);
202 public void testGetArrayLengthWithDebug() throws Exception {
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);