Test coverage in appc-interfaces-services package
[appc.git] / appc-inbound / appc-interfaces-service / bundle / src / test / java / org / onap / appc / interfaces / service / InterfacesServiceProviderImplTest.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;
23
24 import static org.junit.Assert.assertEquals;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Future;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.appc.interfaces.service.executor.ServiceExecutor;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.interfaces.service.rev170818.ExecuteServiceInput;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.interfaces.service.rev170818.ExecuteServiceInputBuilder;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.interfaces.service.rev170818.ExecuteServiceOutput;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.interfaces.service.rev170818.request.info.Request;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.interfaces.service.rev170818.request.info.RequestBuilder;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36
37 public class InterfacesServiceProviderImplTest {
38
39     @Test
40     public void testExecuteService() throws InterruptedException, ExecutionException {
41         InterfacesServiceProviderImpl impl = new InterfacesServiceProviderImpl();
42         RequestBuilder requestBuilder = new RequestBuilder();
43         String requestData = "{\"vnf-id\": \"vnf-id\",\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
44                 + " \"service-instance-id\",\"vnf-id\" : \"vnf-id\",\"vnfc-name\" : \"vnfc-name\",\"vf-module-id\" : \"vf-module-id\","
45                 + "\"vserver-id\": \"vserver-id\"}},\"in-progress-requests\" :[{\"action\" : \"HealthCheck\",\"action-identifiers\" :"
46                 + " {\"service-instance-id\" : \"service-instance-id1\",\"vnf-id\" : \"vnf-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vf-module-id\" :"
47                 + " \"vf-module-id\",\"vserver-id\": \"vserver-id1\"}},{\"action\" : \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" :"
48                 + " \"service-instance-id2\",\"vnf-id\" : \"vnf-id2\",\"vnfc-name\" : \"vnfc-name2\",\"vf-module-id\" : \"vf-module-id2\",\"vserver-id\":"
49                 + " \"vserver-id2\"}}]}";
50         requestBuilder.setRequestData(requestData);
51         requestBuilder.setAction("isScopeOverlap");
52         Request request = requestBuilder.build();
53         ExecuteServiceInputBuilder inputBuilder = new ExecuteServiceInputBuilder();
54         inputBuilder.setRequest(request);
55         ExecuteServiceInput input = inputBuilder.build();
56         Future<RpcResult<ExecuteServiceOutput>> future = impl.executeService(input);
57         assertEquals("400", future.get().getResult().getStatus().getCode());
58     }
59
60     @Test
61     public void testExecuteServiceExceptionFlow() throws Exception {
62         InterfacesServiceProviderImpl impl = Mockito.spy(new InterfacesServiceProviderImpl());
63         RequestBuilder requestBuilder = new RequestBuilder();
64         String requestData = "{\"vnf-id\": \"vnf-id\",\"current-request\" :{\"action\" : \"Audit\",\"action-identifiers\" : {\"service-instance-id\" :"
65                 + " \"service-instance-id\",\"vnf-id\" : \"vnf-id\",\"vnfc-name\" : \"vnfc-name\",\"vf-module-id\" : \"vf-module-id\","
66                 + "\"vserver-id\": \"vserver-id\"}},\"in-progress-requests\" :[{\"action\" : \"HealthCheck\",\"action-identifiers\" :"
67                 + " {\"service-instance-id\" : \"service-instance-id1\",\"vnf-id\" : \"vnf-id1\",\"vnfc-name\" : \"vnfc-name1\",\"vf-module-id\" :"
68                 + " \"vf-module-id\",\"vserver-id\": \"vserver-id1\"}},{\"action\" : \"CheckLock\",\"action-identifiers\" : {\"service-instance-id\" :"
69                 + " \"service-instance-id2\",\"vnf-id\" : \"vnf-id2\",\"vnfc-name\" : \"vnfc-name2\",\"vf-module-id\" : \"vf-module-id2\",\"vserver-id\":"
70                 + " \"vserver-id2\"}}]}";
71         requestBuilder.setRequestData(requestData);
72         requestBuilder.setAction("isScopeOverlap");
73         Request request = requestBuilder.build();
74         ExecuteServiceInputBuilder inputBuilder = new ExecuteServiceInputBuilder();
75         inputBuilder.setRequest(request);
76         ExecuteServiceInput input = inputBuilder.build();
77         ServiceExecutor executorMock = Mockito.mock(ServiceExecutor.class);
78         Mockito.doThrow(new Exception()).when(executorMock).execute(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
79         Mockito.doReturn(executorMock).when(impl).getServiceExecutor();
80         Future<RpcResult<ExecuteServiceOutput>> future = impl.executeService(input);
81         assertEquals("401", future.get().getResult().getStatus().getCode());
82     }
83
84 }