6cdca796b6565798780b37d6f823603a25470466
[appc.git] / appc-inbound / appc-interfaces-service / bundle / src / test / java / org / onap / appc / interfaces / service / executorImpl / ServiceExecutorImplTest.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.interfaces.service.executorImpl;
23
24 import static org.junit.Assert.assertEquals;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.mockito.Mockito;
31 import org.onap.appc.aai.client.aai.AaiService;
32 import org.onap.appc.interfaces.service.data.Request;
33 import org.onap.appc.interfaces.service.data.ScopeOverlap;
34 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 public class ServiceExecutorImplTest {
39
40     private ServiceExecutorImpl executor;
41
42     @Rule
43     public ExpectedException expectedEx = ExpectedException.none();
44
45     @Test
46     public void testScopeOverlapEmpty() throws Exception {
47         executor = Mockito.spy(new ServiceExecutorImpl());
48         ScopeOverlap scopeOverlap = Mockito.spy(new ScopeOverlap());
49         List<Request> emptyList = new ArrayList<Request>();
50         Mockito.doReturn(emptyList).when(scopeOverlap).getInProgressRequest();
51         ObjectMapper objectMapper = Mockito.spy(new ObjectMapper());
52         Mockito.doReturn(objectMapper).when(executor).getObjectMapper();
53         Mockito.doReturn(scopeOverlap).when(objectMapper).readValue("{}", ScopeOverlap.class);
54         assertEquals("\"requestOverlap\"  : false", executor.isRequestOverLap("{}"));
55     }
56
57     @Test
58     public void testScopeOverlapWithVnfIdInCurrentRequest() throws Exception {
59         executor = Mockito.spy(new ServiceExecutorImpl());
60         String requestData = "{\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
61                 + " \"service-instance-id\",\"vnf-id\" : \"vnf-id\",\"vnfc-name\" : \"vnfc-name\",\"vf-module-id\" : \"vf-module-id\","
62                 + "\"vserver-id\": \"vserver-id\"}},\"in-progress-requests\" :[{\"action\" : \"HealthCheck\",\"action-identifiers\" :"
63                 + " {\"service-instance-id\" : \"service-instance-id1\",\"vnf-id\" : \"vnf-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vf-module-id\" :"
64                 + " \"vf-module-id\",\"vserver-id\": \"vserver-id1\"}},{\"action\" : \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" :"
65                 + " \"service-instance-id2\",\"vnf-id\" : \"vnf-id2\",\"vnfc-name\" : \"vnfc-name2\",\"vf-module-id\" : \"vf-module-id2\",\"vserver-id\":"
66                 + " \"vserver-id2\"}}]}";
67         assertEquals("\"requestOverlap\"  : true", executor.isRequestOverLap(requestData));
68     }
69
70     @Test
71     public void testScopeOverlapWithoutVnfIdButWithVfModuleIdCurrentRequest() throws Exception {
72         executor = Mockito.spy(new ServiceExecutorImpl());
73         String requestData = "{\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
74                 + " \"service-instance-id\",\"vnfc-name\" : \"vnfc-name\",\"vf-module-id\" : \"vf-module-id\","
75                 + "\"vserver-id\": \"vserver-id\"}},\"in-progress-requests\" :[{\"action\" : \"HealthCheck\",\"action-identifiers\" :"
76                 + " {\"service-instance-id\" : \"service-instance-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vf-module-id\" :"
77                 + " \"vf-module-id\",\"vserver-id\": \"vserver-id1\"}},{\"action\" : \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" :"
78                 + " \"service-instance-id2\",\"vnfc-name\" : \"vnfc-name2\",\"vf-module-id\" : \"vf-module-id2\",\"vserver-id\":"
79                 + " \"vserver-id2\"}}]}";
80         assertEquals("\"requestOverlap\"  : true", executor.isRequestOverLap(requestData));
81     }
82
83     @Test
84     public void testScopeOverlapWithVnfIdAndInProgressRequest() throws Exception {
85         executor = Mockito.spy(new ServiceExecutorImpl());
86         String requestData = "{\"vnf-id\":\"vnf-id1\",\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
87                 + " \"service-instance-id\",\"vnfc-name\" : \"vnfc-name\",\"vf-module-id\" : \"vf-module-id\","
88                 + "\"vserver-id\": \"vserver-id\"}},\"in-progress-requests\" : [{\"target-id\": \"vnf-id1\",\"action\" : \"HealthCheck\",\"action-identifiers\" :"
89                 + " {\"service-instance-id\" : \"service-instance-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vf-module-id\" :"
90                 + " \"vf-module-id\",\"vserver-id\": \"vserver-id1\"}},{\"action\" : \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" :"
91                 + " \"service-instance-id2\",\"vnfc-name\" : \"vnfc-name2\",\"vf-module-id\" : \"vf-module-id2\",\"vserver-id\":"
92                 + " \"vserver-id2\"}}]}";
93         assertEquals("\"requestOverlap\"  : true", executor.isRequestOverLap(requestData));
94     }
95
96     @Test
97     public void testScopeOverlapWithVserverIdAndInProgressRequest() throws Exception {
98         executor = Mockito.spy(new ServiceExecutorImpl());
99         String requestData = "{\"vnf-id\":\"\",\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
100                 + " \"service-instance-id\",\"vnfc-name\" : \"vnfc-name\",\"vserver-id\": \"vserver-id\"}},\"in-progress-requests\" :"
101                 + " [{\"target-id\": \"vnf-id1\",\"action\" : \"HealthCheck\",\"action-identifiers\" : {\"service-instance-id\" :"
102                 + " \"service-instance-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vserver-id\": \"vserver-id1\"}},{\"action\" :"
103                 + " \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" : \"service-instance-id2\",\"vnfc-name\" :"
104                 + " \"vnfc-name2\",\"vserver-id\": \"vserver-id2\"}}]}";
105         AaiService aaiServiceMock = Mockito.mock(AaiService.class);
106         Mockito.doReturn(aaiServiceMock).when(executor).getAaiService(Mockito.any(AAIClient.class));
107         SvcLogicContext ctx = new SvcLogicContext();
108         ctx.setAttribute("vm-count", "1");
109         Mockito.doReturn(ctx).when(executor).getSvcLogicContext();
110         assertEquals("\"requestOverlap\"  : false", executor.isRequestOverLap(requestData));
111     }
112
113     @Test
114     public void testScopeOverlapWithNoVnfcNameInCurrentRequest() throws Exception {
115         executor = Mockito.spy(new ServiceExecutorImpl());
116         String requestData = "{\"vnf-id\":\"\",\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
117                 + " \"service-instance-id\",\"vserver-id\": \"vserver-id\"}},\"in-progress-requests\" :"
118                 + " [{\"target-id\": \"vnf-id1\",\"action\" : \"HealthCheck\",\"action-identifiers\" : {\"service-instance-id\" :"
119                 + " \"service-instance-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vserver-id\": \"vserver-id1\"}},{\"action\" :"
120                 + " \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" : \"service-instance-id2\",\"vnfc-name\" :"
121                 + " \"vnfc-name2\",\"vserver-id\": \"vserver-id2\"}}]}";
122         AaiService aaiServiceMock = Mockito.mock(AaiService.class);
123         Mockito.doReturn(aaiServiceMock).when(executor).getAaiService(Mockito.any(AAIClient.class));
124         SvcLogicContext ctx = new SvcLogicContext();
125         ctx.setAttribute("vm-count", "1");
126         Mockito.doReturn(ctx).when(executor).getSvcLogicContext();
127         assertEquals("\"requestOverlap\"  : false", executor.isRequestOverLap(requestData));
128     }
129
130     @Test
131     public void testScopeOverlapWithVnfcNameAndInProgressRequest() throws Exception {
132         executor = Mockito.spy(new ServiceExecutorImpl());
133         String requestData = "{\"vnf-id\":\"\",\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
134                 + " \"service-instance-id\",\"vnfc-name\" : \"vnfc-name\"}},\"in-progress-requests\" :"
135                 + " [{\"target-id\": \"vnf-id1\",\"action\" : \"HealthCheck\",\"action-identifiers\" : {\"service-instance-id\" :"
136                 + " \"service-instance-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vserver-id\": \"vserver-id1\"}},{\"action\" :"
137                 + " \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" : \"service-instance-id2\",\"vnfc-name\" :"
138                 + " \"vnfc-name2\",\"vserver-id\": \"vserver-id2\"}}]}";
139         AaiService aaiServiceMock = Mockito.mock(AaiService.class);
140         Mockito.doReturn(aaiServiceMock).when(executor).getAaiService(Mockito.any(AAIClient.class));
141         SvcLogicContext ctx = new SvcLogicContext();
142         ctx.setAttribute("vm-count", "1");
143         Mockito.doReturn(ctx).when(executor).getSvcLogicContext();
144         assertEquals("\"requestOverlap\"  : false", executor.isRequestOverLap(requestData));
145     }
146
147     @Test
148     public void testScopeOverlapExceptionFlow() throws Exception {
149         executor = Mockito.spy(new ServiceExecutorImpl());
150         String requestData = "{\"vnf-id\":\"\",\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
151                 + " \"service-instance-id\"}},\"in-progress-requests\" :"
152                 + " [{\"target-id\": \"vnf-id1\",\"action\" : \"HealthCheck\",\"action-identifiers\" : {\"service-instance-id\" :"
153                 + " \"service-instance-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vserver-id\": \"vserver-id1\"}},{\"action\" :"
154                 + " \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" : \"service-instance-id2\",\"vnfc-name\" :"
155                 + " \"vnfc-name2\",\"vserver-id\": \"vserver-id2\"}}]}";
156         AaiService aaiServiceMock = Mockito.mock(AaiService.class);
157         Mockito.doReturn(aaiServiceMock).when(executor).getAaiService(Mockito.any(AAIClient.class));
158         SvcLogicContext ctx = new SvcLogicContext();
159         ctx.setAttribute("vm-count", "1");
160         Mockito.doReturn(ctx).when(executor).getSvcLogicContext();
161         expectedEx.expect(Exception.class);
162         expectedEx.expectMessage(" Action Identifier doesn't have VnfId, VfModuleId, VServerId, VnfcName ");
163         executor.isRequestOverLap(requestData);
164     }
165 }