aboutsummaryrefslogtreecommitdiffstats
path: root/vnftools/provider/src/test/java/org/onap/sdnc/vnftools/VnfToolsTest.java
blob: b30ff86f7c21e9f16299ed7c40235ef19e67cc87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdnc.vnftools;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

public class VnfToolsTest {
    private SvcLogicContext mockSvcLogicContext = mock(SvcLogicContext.class);

    private VnfTools vnfTools;

    @Before
    public void setUp() throws Exception {
        vnfTools = new VnfTools(null);
    }

    @Test
    public void testConstructor() throws Exception {
        VnfTools vTools = new VnfTools(null);
        Assert.assertTrue("Should have no impact with null property", vTools != null);
        vTools = new VnfTools(new Properties());
        Assert.assertTrue("Should have created", vTools != null);
    }

    @Test(expected = SvcLogicException.class)
    public void testCheckIfActivateReadyFailure() throws Exception {
        vnfTools.checkIfActivateReady(null, mockSvcLogicContext);
    }

    @Test
    public void testCheckIfActivateReady() throws Exception {
        String value = "testing";
        Map<String, String> parameters = new HashMap<>();
        parameters.put(VnfTools.RETURN_KEY, value);
        vnfTools.checkIfActivateReady(parameters, mockSvcLogicContext);
        Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(value, VnfTools.TRUE_STRING);
    }

    @Test(expected = SvcLogicException.class)
    public void testStringContainsFailure() throws Exception {
        vnfTools.stringContains(null, mockSvcLogicContext);
    }

    @Test
    public void testStringContains() throws Exception {
        String value = "result ctx string";
        String stringToFindValue = "testing";
        String stringToSearchValue = "testing 1234";
        Map<String, String> parameters = new HashMap<>();
        parameters.put(VnfTools.RESULT_CTX_STRING, value);
        parameters.put(VnfTools.STRING_TO_FIND, stringToFindValue);
        parameters.put(VnfTools.STRING_TO_SEARCH, stringToSearchValue);

        vnfTools.stringContains(parameters, mockSvcLogicContext);
        Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(
                value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));

        stringToFindValue = "1234";
        vnfTools.stringContains(parameters, mockSvcLogicContext);
        Mockito.verify(mockSvcLogicContext, times(2)).setAttribute(
                value, Boolean.toString(stringToSearchValue.contains(stringToFindValue)));
    }

    @Test
    public void testGenerateNameFailure() throws Exception {
        try {
            vnfTools.generateName(null, mockSvcLogicContext);
            Assert.fail("should have throw SvcLogicException");
        } catch (SvcLogicException e) {
            Assert.assertFalse("Should be validation error",
                    e.getMessage().contains("needs at least length 4 but only have"));
        }
    }

    @Test
    public void testGenerateNameFailWithShortBaseParam() throws Exception {
        String value = "return path";
        String base = "123";
        String suffix = "suffix";
        Map<String, String> parameters = new HashMap<>();
        parameters.put(VnfTools.RETURN_PATH, value);
        parameters.put(VnfTools.BASE, base);
        parameters.put(VnfTools.SUFFIX, suffix);

        try {
            vnfTools.generateName(parameters, mockSvcLogicContext);
            Assert.fail("should have throw SvcLogicException");
        } catch (SvcLogicException e) {
            Assert.assertTrue("Should be length error",
                    e.getMessage().contains("needs at least length 4 but only have"));
        }
    }

    @Test
    public void testGenerateName() throws Exception {
        String value = "return path";
        String base = "1234567890";
        String suffix = "suffix";
        Map<String, String> parameters = new HashMap<>();
        parameters.put(VnfTools.RETURN_PATH, value);
        parameters.put(VnfTools.BASE, base);
        parameters.put(VnfTools.SUFFIX, suffix);

        vnfTools.generateName(parameters, mockSvcLogicContext);
        String expectedValue = String.format("%s%s%s",
                base.substring(0, base.length() - 4), suffix, base.substring(base.length() - 2));
        Mockito.verify(mockSvcLogicContext, times(1)).setAttribute(value, expectedValue);
    }

    @Test
    public void testPrintContextInParamNullFailure() throws Exception {
        try {
            vnfTools.printContext(null, mockSvcLogicContext);
            Assert.fail("should have throw SvcLogicException");
        } catch(SvcLogicException e) {
            Assert.assertEquals("Should be no param error", "no parameters passed", e.getMessage());
        }
    }

    @Test
    public void testPrintContextFileNameFailure() throws Exception {
        String expectedEmessage = "printContext requires 'filename' parameter";
        Map<String, String> parameters = new HashMap<>();
        try {
            vnfTools.printContext(parameters, mockSvcLogicContext);
            Assert.fail("should have throw SvcLogicException");
        } catch(SvcLogicException e) {
            Assert.assertEquals("Should be missing filename error", expectedEmessage, e.getMessage());
        }

        parameters.put(VnfTools.FILENAME, "");
        try {
            vnfTools.printContext(parameters, mockSvcLogicContext);
            Assert.fail("should have throw SvcLogicException");
        } catch(SvcLogicException e) {
            Assert.assertEquals("Should still be missing filename error", expectedEmessage, e.getMessage());
        }
    }

    @Test
    public void testPrintContext() throws Exception {
        Map<String, String> parameters = new HashMap<>();
        parameters.put(VnfTools.FILENAME, "abc");
        vnfTools.printContext(parameters, mockSvcLogicContext);
    }

    @Test
    public void testGetArrayLengthInvalidInt() throws Exception {
        String key = "abc";
        Mockito.doReturn("efg").when(mockSvcLogicContext).getAttribute(key);
        int result = VnfTools.getArrayLength(mockSvcLogicContext, key);
        Assert.assertEquals("Should return 0 for string value", 0, result);
    }

    @Test
    public void testGetArrayLength() throws Exception {
        String key = "abc";
        String value = "234";
        Mockito.doReturn(value).when(mockSvcLogicContext).getAttribute(key);
        int result = VnfTools.getArrayLength(mockSvcLogicContext, key);
        Assert.assertEquals("Should return the value int", Integer.parseInt(value), result);
    }

    @Test
    public void testGetArrayLengthWithDebugInvalidInt() throws Exception {
        String key = "abc";
        Mockito.doReturn("efg").when(mockSvcLogicContext).getAttribute(key);
        int result = VnfTools.getArrayLength(mockSvcLogicContext, key, "debug");
        Assert.assertEquals("Should return 0 for string value", 0, result);
    }

    @Test
    public void testGetArrayLengthWithDebug() throws Exception {
        String key = "abc";
        String value = "234";
        Mockito.doReturn(value).when(mockSvcLogicContext).getAttribute(key);
        int result = VnfTools.getArrayLength(mockSvcLogicContext, key, "debug");
        Assert.assertEquals("Should return the value int", Integer.parseInt(value), result);
    }

}