Adding DistributeTrafficCheck LCM API
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / DistributeTrafficCheckServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Orange
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.provider.lcm.service;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.onap.appc.domainmodel.lcm.ResponseContext;
30 import org.onap.appc.executor.objects.LCMCommandStatus;
31 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.*;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
36 import org.powermock.reflect.Whitebox;
37
38 import static org.junit.Assert.*;
39 import static org.mockito.Matchers.any;
40 import static org.mockito.Mockito.*;
41 import static org.powermock.api.mockito.PowerMockito.whenNew;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class DistributeTrafficCheckServiceTest {
45
46     private static final Action myAction = Action.DistributeTrafficCheck;
47     private static final String rpcName = "distribute-traffic-check";
48     private static final String PAYLOAD_STRING = "{\"test\":\"test\"}";
49
50     private final DistributeTrafficCheckInput mockInput = mock(DistributeTrafficCheckInput.class);
51     private final CommonHeader mockCommonHeader = mock(CommonHeader.class);
52     private final ActionIdentifiers mockActionIdentifiers = mock(ActionIdentifiers.class);
53     private final Payload mockPayload = mock(Payload.class);
54
55     private DistributeTrafficCheckService distributeTrafficCheckService;
56
57     @Before
58     public void setUp() throws Exception {
59         distributeTrafficCheckService = spy(new DistributeTrafficCheckService());
60     }
61
62     @Test
63     public void testConstructor() throws Exception {
64         assertEquals("Should have proper ACTION", myAction,
65                 (Action) Whitebox.getInternalState(distributeTrafficCheckService, "expectedAction"));
66         assertEquals("Should have action-status RPC name", rpcName,
67                 Whitebox.getInternalState(distributeTrafficCheckService, "rpcName").toString());
68     }
69
70     private void helpInitializeRequestParameters() {
71         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
72         Mockito.doReturn(mockPayload).when(mockInput).getPayload();
73         Mockito.doReturn(myAction).when(mockInput).getAction();
74         Mockito.doReturn(mockActionIdentifiers).when(mockInput).getActionIdentifiers();
75         Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
76         ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
77         Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
78         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
79         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
80         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
81         Mockito.doReturn("vnfId").when(mockActionIdentifiers).getVnfId();
82     }
83
84     @Test
85     public void testProcess() throws Exception {
86         helpInitializeRequestParameters();
87
88         // test processAction return without error
89         RequestExecutor mockExecutor = mock(RequestExecutor.class);
90         whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
91
92         RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
93         Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
94
95         ResponseContext mockResponseContext = mock(ResponseContext.class);
96         Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
97
98         Mockito.when(distributeTrafficCheckService.executeAction(any())).thenReturn(mockOutput);
99
100         DistributeTrafficCheckOutputBuilder outputBuilder = distributeTrafficCheckService.process(mockInput);
101
102         Mockito.verify(distributeTrafficCheckService, times(1)).proceedAction(mockInput);
103
104         assertNotNull("Should have commonHeader", outputBuilder.getCommonHeader());
105     }
106
107     @Test
108     public void testValidateMissingParameters() throws Exception {
109         DistributeTrafficCheckOutputBuilder outputBuilder = distributeTrafficCheckService.process(mockInput);
110         Mockito.verify(distributeTrafficCheckService, times(0)).proceedAction(any());
111         assertNull("Should not have commonHeader as we did not mock it", outputBuilder.getCommonHeader());
112         assertEquals("should return missing parameter status",
113                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
114                 outputBuilder.getStatus().getCode());
115     }
116
117     @Test
118     public void testValidateForMissingOrInvalidAction() throws Exception {
119         helpInitializeRequestParameters();
120
121         // check missing Action
122         Mockito.doReturn(null).when(mockInput).getAction();
123
124         distributeTrafficCheckService.validate(mockInput);
125         Status status = (Status) Whitebox.getInternalState(distributeTrafficCheckService, "status");
126         assertEquals("Should return missing parameter for action",
127                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
128
129         // check invalid Action
130         Mockito.doReturn(Action.Migrate).when(mockInput).getAction();
131         distributeTrafficCheckService.validate(mockInput);
132         status = (Status) Whitebox.getInternalState(distributeTrafficCheckService, "status");
133         assertEquals("should return missing parameter",
134                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
135     }
136
137     @Test
138     public void testValidateForMissingActionIdentifiers() throws Exception {
139         helpInitializeRequestParameters();
140         Mockito.doReturn(null).when(mockInput).getActionIdentifiers();
141
142         // test missing ActionIdentifiers
143         distributeTrafficCheckService.validate(mockInput);
144         Status status = (Status) Whitebox.getInternalState(distributeTrafficCheckService, "status");
145         assertEquals("should return missing parameter",
146                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
147     }
148
149     @Test
150     public void testValidateEmptyOrMissingPayload() throws Exception {
151         helpInitializeRequestParameters();
152
153         // validate empty payload
154         Mockito.doReturn("").when(mockPayload).getValue();
155         distributeTrafficCheckService.validate(mockInput);
156         Status status = (Status) Whitebox.getInternalState(distributeTrafficCheckService, "status");
157         assertEquals("should return invalid parameter",
158                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
159
160         // validate missing payload
161         Mockito.doReturn(null).when(mockInput).getPayload();
162         distributeTrafficCheckService.validate(mockInput);
163         status = (Status) Whitebox.getInternalState(distributeTrafficCheckService, "status");
164         assertEquals("should return missing parameter",
165                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
166
167     }
168
169 }