Test coverage in DBService
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / dbervices / FlowControlDBServiceTest.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.flow.controller.dbervices;
23
24 import static org.junit.Assert.assertNull;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.mockito.Mockito;
29 import org.onap.appc.flow.controller.data.Transaction;
30 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
31 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
35
36 public class FlowControlDBServiceTest {
37
38     private SqlResource sqlResource = Mockito.mock(SqlResource.class);
39     private FlowControlDBService dbService;
40
41     @Rule
42     public ExpectedException expectedEx = ExpectedException.none();
43
44     @Test
45     public void testGetFlowReferenceData() throws SvcLogicException {
46         dbService = new FlowControlDBService(sqlResource);
47         SvcLogicContext ctx = new SvcLogicContext();
48         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
49         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
50                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
51                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
52         expectedEx.expect(SvcLogicException.class);
53         expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR);
54         dbService.getFlowReferenceData(ctx, null, new SvcLogicContext());
55     }
56
57     @Test
58     public void testGetEndpointByAction() {
59         dbService = new FlowControlDBService(sqlResource);
60         assertNull(dbService.getEndPointByAction(null));
61     }
62
63     @Test
64     public void testGetDesignTimeFlowModelFirstQueryException() throws SvcLogicException {
65         dbService = new FlowControlDBService(sqlResource);
66         SvcLogicContext ctx = new SvcLogicContext();
67         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
68         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
69                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
70                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
71         expectedEx.expect(SvcLogicException.class);
72         expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR);
73         dbService.getDesignTimeFlowModel(ctx);
74     }
75
76     @Test
77     public void testGetDesignTimeFlowModelSecondQueryException() throws SvcLogicException {
78         dbService = new FlowControlDBService(sqlResource);
79         SvcLogicContext ctx = new SvcLogicContext();
80         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
81         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
82                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
83                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE);
84         expectedEx.expect(SvcLogicException.class);
85         expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR);
86         dbService.getDesignTimeFlowModel(ctx);
87     }
88
89     @Test
90     public void testGetDesignTimeFlowModelNullLocalContext() throws SvcLogicException {
91         dbService = new FlowControlDBService(sqlResource);
92         assertNull(dbService.getDesignTimeFlowModel(null));
93     }
94
95     @Test
96     public void testLoadSequenceIntoDb() throws SvcLogicException {
97         dbService = new FlowControlDBService(sqlResource);
98         SvcLogicContext ctx = new SvcLogicContext();
99         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
100         Mockito.when(sqlResource.save(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyString(),
101                 Mockito.any(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
102                 .thenReturn(QueryStatus.FAILURE);
103         expectedEx.expect(SvcLogicException.class);
104         expectedEx.expectMessage("Error While processing storing Artifact: ");
105         dbService.loadSequenceIntoDB(ctx);
106     }
107
108     @Test
109     public void testGetProtocolTypeFirstException() throws SvcLogicException {
110         dbService = new FlowControlDBService(sqlResource);
111         SvcLogicContext ctx = new SvcLogicContext();
112         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
113         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
114                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
115                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
116         expectedEx.expect(SvcLogicException.class);
117         expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR);
118         dbService.populateModuleAndRPC(new Transaction(), "vnf_type");
119     }
120
121     @Test
122     public void testGetProtocolTypeSecondException() throws SvcLogicException {
123         dbService = Mockito.spy(new FlowControlDBService(sqlResource));
124         SvcLogicContext ctx = new SvcLogicContext();
125         ctx.setAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM, "1");
126         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
127                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
128                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE);
129         Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx);
130         expectedEx.expect(SvcLogicException.class);
131         expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR);
132         dbService.populateModuleAndRPC(new Transaction(), "vnf_type");
133     }
134
135     @Test
136     public void testHasSingleProtocolFirstException() throws SvcLogicException {
137         dbService = Mockito.spy(new FlowControlDBService(sqlResource));
138         SvcLogicContext ctx = new SvcLogicContext();
139         ctx.setAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM, "2");
140         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
141                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
142                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE);
143         Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx);
144         expectedEx.expect(SvcLogicException.class);
145         expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR);
146         dbService.populateModuleAndRPC(new Transaction(), "vnf_type");
147         Mockito.verify(dbService).getSvcLogicContext();
148     }
149
150     @Test
151     public void testHasSingleProtocolSecondException() throws SvcLogicException {
152         dbService = Mockito.spy(new FlowControlDBService(sqlResource));
153         SvcLogicContext ctx = new SvcLogicContext();
154         ctx.setAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM, "2");
155         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
156                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
157                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.SUCCESS)
158                 .thenReturn(QueryStatus.FAILURE);
159         Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx);
160         expectedEx.expect(SvcLogicException.class);
161         expectedEx.expectMessage("Got more than 2 values..");
162         dbService.populateModuleAndRPC(new Transaction(), "vnf_type");
163         Mockito.verify(dbService).getSvcLogicContext();
164     }
165
166     @Test
167     public void testHasSingleProtocolThirdException() throws SvcLogicException {
168         dbService = Mockito.spy(new FlowControlDBService(sqlResource));
169         SvcLogicContext ctx = Mockito.spy(new SvcLogicContext());
170         Mockito.when(ctx.getAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM)).thenReturn("2").thenReturn("1");
171         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
172                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
173                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.SUCCESS)
174                 .thenReturn(QueryStatus.FAILURE);
175         Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx);
176         expectedEx.expect(SvcLogicException.class);
177         expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR);
178         dbService.populateModuleAndRPC(new Transaction(), "vnf_type");
179         Mockito.verify(dbService).getSvcLogicContext();
180     }
181
182     @Test
183     public void testHasSingleProtocolSuccessFlow() throws SvcLogicException {
184         dbService = Mockito.spy(new FlowControlDBService(sqlResource));
185         SvcLogicContext ctx = Mockito.spy(new SvcLogicContext());
186         Mockito.when(ctx.getAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM)).thenReturn("2").thenReturn("1");
187         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
188                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
189                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.SUCCESS)
190                 .thenReturn(QueryStatus.SUCCESS);
191         Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx);
192         Transaction transaction = Mockito.spy(new Transaction());
193         dbService.populateModuleAndRPC(transaction, "vnf_type");
194         Mockito.verify(transaction).setExecutionRPC(null);
195     }
196
197     @Test
198     public void testGetDependencyInfoFirstException() throws SvcLogicException {
199         dbService = new FlowControlDBService(sqlResource);
200         SvcLogicContext ctx = new SvcLogicContext();
201         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
202         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
203                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
204                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
205         expectedEx.expect(SvcLogicException.class);
206         expectedEx.expectMessage("Error - while getting dependencydata ");
207         dbService.getDependencyInfo(ctx);
208     }
209
210     @Test
211     public void testGetDependencyInfoSecondException() throws SvcLogicException {
212         dbService = new FlowControlDBService(sqlResource);
213         SvcLogicContext ctx = new SvcLogicContext();
214         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
215         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
216                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
217                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE);
218         expectedEx.expect(SvcLogicException.class);
219         expectedEx.expectMessage("Error - while getting dependencyData ");
220         dbService.getDependencyInfo(ctx);
221     }
222
223     @Test
224     public void testGetCapabilitiesDataFirstException() throws SvcLogicException {
225         dbService = new FlowControlDBService(sqlResource);
226         SvcLogicContext ctx = new SvcLogicContext();
227         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
228         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
229                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
230                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE);
231         expectedEx.expect(SvcLogicException.class);
232         expectedEx.expectMessage("Error - while getting capabilitiesData ");
233         dbService.getCapabilitiesData(ctx);
234     }
235
236     @Test
237     public void testGetCapabilitiesDataSecondException() throws SvcLogicException {
238         dbService = new FlowControlDBService(sqlResource);
239         SvcLogicContext ctx = new SvcLogicContext();
240         ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level");
241         Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
242                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
243                 Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE);
244         expectedEx.expect(SvcLogicException.class);
245         expectedEx.expectMessage("Error - while getting capabilitiesData ");
246         dbService.getCapabilitiesData(ctx);
247     }
248 }