2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.provider.lcm.service;
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;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
40 import static org.junit.Assert.assertNull;
41 import static org.mockito.Matchers.any;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.spy;
45 import static org.mockito.Mockito.times;
46 import static org.powermock.api.mockito.PowerMockito.whenNew;
48 @RunWith(MockitoJUnitRunner.class)
49 public class DistributeTrafficServiceTest {
51 private final Action myAction = Action.DistributeTraffic;
52 private final String rpcName = "distribute-traffic";
53 private String PAYLOAD_STRING = "{\"ConfigFileName\":\"test\"}";
55 private final DistributeTrafficInput mockInput = mock(DistributeTrafficInput.class);
56 private final CommonHeader mockCommonHeader = mock(CommonHeader.class);
57 private final ActionIdentifiers mockActionIdentifiers = mock(ActionIdentifiers.class);
58 private final Payload mockPayload = mock(Payload.class);
60 private DistributeTrafficService distributeTrafficService;
63 public void setUp() throws Exception {
64 distributeTrafficService = spy(new DistributeTrafficService());
68 public void testConstructor() throws Exception {
69 assertEquals("Should have proper ACTION", myAction,
70 (Action) Whitebox.getInternalState(distributeTrafficService, "expectedAction"));
71 assertEquals("Should have action-status RPC name", rpcName,
72 Whitebox.getInternalState(distributeTrafficService, "rpcName").toString());
75 private void helpInitializeRequestParameters() {
76 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
77 Mockito.doReturn(mockPayload).when(mockInput).getPayload();
78 Mockito.doReturn(myAction).when(mockInput).getAction();
79 Mockito.doReturn(mockActionIdentifiers).when(mockInput).getActionIdentifiers();
80 Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
81 ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
82 Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
83 Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
84 Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
85 Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
86 Mockito.doReturn("vnfId").when(mockActionIdentifiers).getVnfId();
90 public void testProcess() throws Exception {
92 helpInitializeRequestParameters();
94 // test processAction return without error
95 RequestExecutor mockExecutor = mock(RequestExecutor.class);
96 whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
98 RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
99 Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
101 ResponseContext mockResponseContext = mock(ResponseContext.class);
102 Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
104 Mockito.when(distributeTrafficService.executeAction(any())).thenReturn(mockOutput);
106 DistributeTrafficOutputBuilder outputBuilder = distributeTrafficService.process(mockInput);
108 Mockito.verify(distributeTrafficService, times(1)).proceedAction(mockInput);
109 assertNotNull("Should have commonHeader", outputBuilder.getCommonHeader());
113 public void testValidateMissingParameters() throws Exception {
114 DistributeTrafficOutputBuilder outputBuilder = distributeTrafficService.process(mockInput);
115 Mockito.verify(distributeTrafficService, times(0)).proceedAction(any());
116 assertNull("Should not have commonHeader as we did not mock it", outputBuilder.getCommonHeader());
117 assertEquals("should return missing parameter status",
118 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
119 outputBuilder.getStatus().getCode());
123 public void testValidateForMissingOrInvalidAction() throws Exception {
124 helpInitializeRequestParameters();
126 // check missing Action
127 Mockito.doReturn(null).when(mockInput).getAction();
128 distributeTrafficService.validate(mockInput);
129 Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
130 assertEquals("Should return missing parameter for action",
131 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
133 // check invalid Action
134 Mockito.doReturn(Action.Migrate).when(mockInput).getAction();
136 distributeTrafficService.validate(mockInput);
137 status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
138 assertEquals("should return missing parameter",
139 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
143 public void testValidateForMissingActionIdentifiers() throws Exception {
145 helpInitializeRequestParameters();
146 Mockito.doReturn(null).when(mockInput).getActionIdentifiers();
148 // test missing ActionIdentifiers
149 distributeTrafficService.validate(mockInput);
150 Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
151 assertEquals("should return missing parameter",
152 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
156 public void testValidateEmptyOrMissingPayload() throws Exception {
157 helpInitializeRequestParameters();
159 // validate empty payload
160 Mockito.doReturn("").when(mockPayload).getValue();
161 distributeTrafficService.validate(mockInput);
162 Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
163 assertEquals("should return invalid parameter",
164 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
166 // validate missing payload
167 Mockito.doReturn(null).when(mockInput).getPayload();
168 distributeTrafficService.validate(mockInput);
169 status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
171 assertEquals("should return missing parameter",
172 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
177 public void testValidateMissingConfigFileName() throws Exception {
178 helpInitializeRequestParameters();
179 String wrongPayload = "{\"test\":\"test\"}";
180 Mockito.doReturn(wrongPayload).when(mockPayload).getValue();
181 distributeTrafficService.validate(mockInput);
182 Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
183 assertEquals("should return status null",
184 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());