Test coverage in EncryptionToolDGWrapper
[appc.git] / appc-config / appc-encryption-tool / provider / src / test / java / org / onap / appc / encryptiontool / wrapper / EncryptionToolDGWrapperTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.encryptiontool.wrapper;
23
24 import static org.junit.Assert.assertEquals;
25 import java.io.IOException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.onap.appc.encryptiontool.fqdn.ParseAdminArtifcat;
34 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.powermock.reflect.Whitebox;
42 import com.fasterxml.jackson.core.JsonProcessingException;
43
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest(WrapperEncryptionTool.class)
46 public class EncryptionToolDGWrapperTest {
47
48     @Rule
49     public ExpectedException expectedEx = ExpectedException.none();
50
51     @Test
52     public void testRunEncryption() throws SvcLogicException {
53         EncryptionToolDGWrapper wrapper = EncryptionToolDGWrapper.initialise();
54         Map<String, String> inParams = new HashMap<>();
55         inParams.put("userName", "userName");
56         inParams.put("password", "password");
57         inParams.put("vnf_type", "vnf_type");
58         SvcLogicContext ctx = new SvcLogicContext();
59         PowerMockito.mockStatic(WrapperEncryptionTool.class);
60         wrapper.runEncryption(inParams, ctx);
61         PowerMockito.verifyStatic();
62     }
63
64     @Test
65     public void testRunEncryptionNullVnfType() throws SvcLogicException {
66         EncryptionToolDGWrapper wrapper = EncryptionToolDGWrapper.initialise();
67         Map<String, String> inParams = new HashMap<>();
68         inParams.put("userName", "userName");
69         inParams.put("password", "password");
70         expectedEx.expect(SvcLogicException.class);
71         expectedEx.expectMessage("username or Password is missing");
72         wrapper.runEncryption(inParams, new SvcLogicContext());
73     }
74
75     @Test
76     public void testGetPropertyAnsibleWithPayload() throws SvcLogicException {
77         SqlResource sqlResource = Mockito.mock(SqlResource.class);
78         EncryptionToolDGWrapper wrapper = new EncryptionToolDGWrapper(sqlResource);
79         Map<String, String> inParams = new HashMap<>();
80         populateParams(inParams);
81         inParams.put("payloadFqdn", "payloadFqdn");
82         SvcLogicContext ctx = new SvcLogicContext();
83         ctx.setAttribute("vnf-type", "vnf-type");
84         ctx.setAttribute("input.action", "input.action");
85         ctx.setAttribute("APPC.protocol.PROTOCOL", "ansible");
86         wrapper.getProperty(inParams, ctx);
87         Mockito.verify(sqlResource).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(),
88                 Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
89     }
90
91     @Test
92     public void testGetPropertyAnsibleWithoutPayload() throws SvcLogicException, JsonProcessingException, RuntimeException, IOException {
93         SqlResource sqlResource = Mockito.mock(SqlResource.class);
94         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
95                 Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
96         EncryptionToolDGWrapper wrapper = Mockito.spy(new EncryptionToolDGWrapper(sqlResource));
97         Map<String, String> inParams = new HashMap<>();
98         populateParams(inParams);
99         SvcLogicContext ctx = new SvcLogicContext();
100         ctx.setAttribute("vnf-type", "vnf-type");
101         ctx.setAttribute("input.action", "input.action");
102         ctx.setAttribute("APPC.protocol.PROTOCOL", "ansible");
103         ctx.setAttribute("MULTIPLE", "2");
104         ParseAdminArtifcat artifact = Mockito.mock(ParseAdminArtifcat.class);
105         Mockito.when(artifact.retrieveFqdn(Mockito.any(SvcLogicContext.class))).thenReturn(":::");
106         Whitebox.setInternalState(wrapper, "artifact", artifact);
107         expectedEx.expect(SvcLogicException.class);
108         expectedEx.expectMessage("Error retrieving credentials");
109         wrapper.getProperty(inParams, ctx);
110     }
111
112     @Test
113     public void testGetPropertyAnsibleWithoutPayloadAndInvalidFQDN() throws SvcLogicException, JsonProcessingException, RuntimeException, IOException {
114         SqlResource sqlResource = Mockito.mock(SqlResource.class);
115         EncryptionToolDGWrapper wrapper = Mockito.spy(new EncryptionToolDGWrapper(sqlResource));
116         Map<String, String> inParams = new HashMap<>();
117         populateParams(inParams);
118         SvcLogicContext ctx = new SvcLogicContext();
119         ctx.setAttribute("vnf-type", "vnf-type");
120         ctx.setAttribute("input.action", "input.action");
121         ctx.setAttribute("APPC.protocol.PROTOCOL", "ansible");
122         ctx.setAttribute("MULTIPLE", "2");
123         ParseAdminArtifcat artifact = Mockito.mock(ParseAdminArtifcat.class);
124         Mockito.when(artifact.retrieveFqdn(Mockito.any(SvcLogicContext.class))).thenReturn("TEST");
125         Whitebox.setInternalState(wrapper, "artifact", artifact);
126         expectedEx.expect(SvcLogicException.class);
127         expectedEx.expectMessage(": NOT_FOUND! No FQDN  match found in admin artifact  for ");
128         wrapper.getProperty(inParams, ctx);
129     }
130
131     @Test
132     public void testGetPropertyCountEqualsOne() throws SvcLogicException, JsonProcessingException, RuntimeException, IOException {
133         SqlResource sqlResource = Mockito.mock(SqlResource.class);
134         EncryptionToolDGWrapper wrapper = Mockito.spy(new EncryptionToolDGWrapper(sqlResource));
135         Map<String, String> inParams = new HashMap<>();
136         populateParams(inParams);
137         SvcLogicContext ctx = new SvcLogicContext();
138         ctx.setAttribute("vnf-type", "vnf-type");
139         ctx.setAttribute("input.action", "input.action");
140         ctx.setAttribute("APPC.protocol.PROTOCOL", "ansible");
141         ctx.setAttribute("MULTIPLE", "1");
142         ctx.setAttribute("USER-NAME", "USER-NAME");
143         ctx.setAttribute("PASSWORD", "PASSWORD");
144         ctx.setAttribute("PORT-NUMBER", "PORT-NUMBER");
145         ctx.setAttribute("URL", "URL");
146         ParseAdminArtifcat artifact = Mockito.mock(ParseAdminArtifcat.class);
147         Mockito.when(artifact.retrieveFqdn(Mockito.any(SvcLogicContext.class))).thenReturn("TEST");
148         Whitebox.setInternalState(wrapper, "artifact", artifact);
149         wrapper.getProperty(inParams, ctx);
150         assertEquals("PORT-NUMBER", ctx.getAttribute("prefix.port"));
151     }
152
153     @Test
154     public void testGetPropertyCountZeroFailure() throws SvcLogicException, JsonProcessingException, RuntimeException, IOException {
155         SqlResource sqlResource = Mockito.mock(SqlResource.class);
156         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
157                 Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
158         EncryptionToolDGWrapper wrapper = Mockito.spy(new EncryptionToolDGWrapper(sqlResource));
159         Map<String, String> inParams = new HashMap<>();
160         populateParams(inParams);
161         SvcLogicContext ctx = new SvcLogicContext();
162         ctx.setAttribute("vnf-type", "vnf-type");
163         ctx.setAttribute("input.action", "input.action");
164         ctx.setAttribute("APPC.protocol.PROTOCOL", "ansible");
165         ctx.setAttribute("MULTIPLE", "0");
166         ParseAdminArtifcat artifact = Mockito.mock(ParseAdminArtifcat.class);
167         Mockito.when(artifact.retrieveFqdn(Mockito.any(SvcLogicContext.class))).thenReturn("TEST");
168         Whitebox.setInternalState(wrapper, "artifact", artifact);
169         expectedEx.expect(SvcLogicException.class);
170         expectedEx.expectMessage("Error retrieving credentials");
171         wrapper.getProperty(inParams, ctx);
172     }
173
174     @Test
175     public void testGetPropertyCountZeroNotFound() throws SvcLogicException, JsonProcessingException, RuntimeException, IOException {
176         SqlResource sqlResource = Mockito.mock(SqlResource.class);
177         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
178                 Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.NOT_FOUND);
179         EncryptionToolDGWrapper wrapper = Mockito.spy(new EncryptionToolDGWrapper(sqlResource));
180         Map<String, String> inParams = new HashMap<>();
181         populateParams(inParams);
182         SvcLogicContext ctx = new SvcLogicContext();
183         ctx.setAttribute("vnf-type", "vnf-type");
184         ctx.setAttribute("input.action", "input.action");
185         ctx.setAttribute("APPC.protocol.PROTOCOL", "ansible");
186         ctx.setAttribute("MULTIPLE", "0");
187         ParseAdminArtifcat artifact = Mockito.mock(ParseAdminArtifcat.class);
188         Mockito.when(artifact.retrieveFqdn(Mockito.any(SvcLogicContext.class))).thenReturn("TEST");
189         Whitebox.setInternalState(wrapper, "artifact", artifact);
190         expectedEx.expect(SvcLogicException.class);
191         expectedEx.expectMessage(":: NOT_FOUND! No data found in device_authentication table for");
192         wrapper.getProperty(inParams, ctx);
193     }
194
195     @Test
196     public void testGetPropertyNonAnsibleFailure() throws SvcLogicException, JsonProcessingException, RuntimeException, IOException {
197         SqlResource sqlResource = Mockito.mock(SqlResource.class);
198         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
199                 Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
200         EncryptionToolDGWrapper wrapper = Mockito.spy(new EncryptionToolDGWrapper(sqlResource));
201         Map<String, String> inParams = new HashMap<>();
202         populateParams(inParams);
203         SvcLogicContext ctx = new SvcLogicContext();
204         ctx.setAttribute("vnf-type", "vnf-type");
205         ctx.setAttribute("input.action", "input.action");
206         ctx.setAttribute("APPC.protocol.PROTOCOL", "TEST");
207         ctx.setAttribute("MULTIPLE", "0");
208         ParseAdminArtifcat artifact = Mockito.mock(ParseAdminArtifcat.class);
209         Mockito.when(artifact.retrieveFqdn(Mockito.any(SvcLogicContext.class))).thenReturn("TEST");
210         Whitebox.setInternalState(wrapper, "artifact", artifact);
211         expectedEx.expect(SvcLogicException.class);
212         expectedEx.expectMessage("Error retrieving credentials");
213         wrapper.getProperty(inParams, ctx);
214     }
215
216     @Test
217     public void testGetPropertyNonAnsibleNotFound() throws SvcLogicException, JsonProcessingException, RuntimeException, IOException {
218         SqlResource sqlResource = Mockito.mock(SqlResource.class);
219         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
220                 Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.NOT_FOUND);
221         EncryptionToolDGWrapper wrapper = Mockito.spy(new EncryptionToolDGWrapper(sqlResource));
222         Map<String, String> inParams = new HashMap<>();
223         populateParams(inParams);
224         SvcLogicContext ctx = new SvcLogicContext();
225         ctx.setAttribute("vnf-type", "vnf-type");
226         ctx.setAttribute("input.action", "input.action");
227         ctx.setAttribute("APPC.protocol.PROTOCOL", "TEST");
228         ctx.setAttribute("MULTIPLE", "0");
229         ParseAdminArtifcat artifact = Mockito.mock(ParseAdminArtifcat.class);
230         Mockito.when(artifact.retrieveFqdn(Mockito.any(SvcLogicContext.class))).thenReturn("TEST");
231         Whitebox.setInternalState(wrapper, "artifact", artifact);
232         expectedEx.expect(SvcLogicException.class);
233         expectedEx.expectMessage(":: NOT_FOUND! No data found in device_authentication table for");
234         wrapper.getProperty(inParams, ctx);
235     }
236
237     private void populateParams(Map<String, String> inParams) {
238         inParams.put("prefix", "prefix");
239         inParams.put("tenantAai", "tenantAai");
240         inParams.put("cldOwnerAai", "cldOwnerAai");
241         inParams.put("cldRegionAai", "cldRegionAai");
242         inParams.put("payloadFqdn", "");
243         inParams.put("payloadTenant", "payloadTenant");
244         inParams.put("payloadCloudOwner", "payloadCloudOwner");
245         inParams.put("payloadCloudRegion", "payloadCloudRegion");
246     }
247 }