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