3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2019 Nokia.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.provider.lcm.service;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mockito;
30 import org.mockito.internal.util.reflection.Whitebox;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.onap.appc.util.JsonUtil;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.*;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
37 import org.onap.appc.domainmodel.lcm.ResponseContext;
38 import org.onap.appc.executor.objects.LCMCommandStatus;
39 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
40 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
44 import static org.mockito.Matchers.any;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.spy;
47 import static org.mockito.Mockito.times;
49 @RunWith(MockitoJUnitRunner.class)
50 public class ConfigScaleInServiceTest {
51 private final Action myAction = Action.ConfigScaleIn;
52 private final String PAYLOAD_STRING = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
53 private ConfigScaleInInput mockInput = mock(ConfigScaleInInput.class);
54 private CommonHeader mockCommonHeader = mock(CommonHeader.class);
55 private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
56 private Payload mockPayload = mock(Payload.class);
58 private ConfigScaleInService configscaleinServiceAction;
60 public void setUp() throws Exception {
62 configscaleinServiceAction = spy(new ConfigScaleInService());
66 public void testProcess() throws Exception {
67 // test error occurs in validation
68 ConfigScaleInOutputBuilder outputBuilder = configscaleinServiceAction.process(mockInput);
69 Mockito.verify(configscaleinServiceAction, times(0)).proceedAction(any(),any(),any());
70 Assert.assertTrue("Should not have commonHeader as we did not mock it",outputBuilder.getCommonHeader() == null);
71 Assert.assertEquals("should return missing parameter status",
72 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
73 outputBuilder.getStatus().getCode());
75 // make validation pass
76 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
77 Mockito.doReturn(mockPayload).when(mockInput).getPayload();
78 Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
79 // to make validation pass
80 ZULU zuluTimeStamp = new ZULU("2019-12-06T21:44:00.35Z");
81 Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
82 Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
83 Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
84 Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
86 Mockito.doReturn(myAction).when(mockInput).getAction();
87 Mockito.doReturn(mockAI).when(mockInput).getActionIdentifiers();
88 Mockito.doReturn("vnfId").when(mockAI).getVnfId();
90 // test processAction return without error
91 RequestExecutor mockExecutor = mock(RequestExecutor.class);
92 // whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
94 RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
95 Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
97 ResponseContext mockResponseContext = mock(ResponseContext.class);
98 Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
100 org.onap.appc.domainmodel.lcm.Status mockStatus = mock(org.onap.appc.domainmodel.lcm.Status.class);
101 Integer successCode = Integer.valueOf(LCMCommandStatus.SUCCESS.getResponseCode());
102 Mockito.doReturn(successCode).when(mockStatus).getCode();
103 Mockito.doReturn(mockStatus).when(mockResponseContext).getStatus();
104 RequestHandlerInput requestHandlerInputInput = mock(RequestHandlerInput.class);
105 AbstractBaseService abstractBaseService = mock(AbstractBaseService.class);
106 Mockito.when(abstractBaseService.executeAction(requestHandlerInputInput)).thenReturn(mockOutput);
108 outputBuilder = configscaleinServiceAction.process(mockInput);
110 Assert.assertTrue(true);
112 Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() == null);
113 Assert.assertEquals("should return success status", new Integer(302), outputBuilder.getStatus().getCode());
117 public void testValidate() throws Exception {
118 configscaleinServiceAction.validate(mockCommonHeader, Action.ConfigScaleIn, mockAI,mockPayload);
119 Status status = (Status) Whitebox.getInternalState(configscaleinServiceAction, "status");
120 Assert.assertEquals("should return missing parameter",
121 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
122 Mockito.verify(configscaleinServiceAction, times(0)).buildStatusForParamName(any(), any());
123 Mockito.verify(configscaleinServiceAction, times(0)).buildStatusForErrorMsg(any(), any());
125 ZULU mockTimeStamp = mock(ZULU.class);
126 Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
127 Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
128 Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
129 Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
132 configscaleinServiceAction.validate(mockCommonHeader, Action.ConfigScaleIn, mockAI,mockPayload);
133 status = (Status) Whitebox.getInternalState(configscaleinServiceAction, "status");
134 Assert.assertEquals("Should return missing parameter for action",
135 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
137 // test empty ActionIdentifier
138 configscaleinServiceAction.validate(mockCommonHeader, Action.ConfigScaleIn, mockAI,mockPayload);
139 status = (Status) Whitebox.getInternalState(configscaleinServiceAction, "status");
140 Assert.assertEquals("should return missing parameter",
141 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
143 // test Invalid VNF_ID
144 Mockito.doReturn("").when(mockAI).getVnfId();
145 configscaleinServiceAction.validate(mockCommonHeader, Action.ConfigScaleIn, mockAI,mockPayload);
146 status = (Status) Whitebox.getInternalState(configscaleinServiceAction, "status");
147 Assert.assertEquals("should return invalid parameter",
148 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
151 Mockito.doReturn("vnfId").when(mockAI).getVnfId();
152 configscaleinServiceAction.validate(mockCommonHeader, Action.ConfigScaleIn, mockAI, null);
153 Mockito.verify(configscaleinServiceAction, times(1)).validateExcludedActIds(any(), any());
154 status = (Status) Whitebox.getInternalState(configscaleinServiceAction, "status");
155 Assert.assertEquals("should return missing parameter",
156 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
158 // test empty payload
160 Mockito.doReturn("").when(mockPayload).getValue();
161 configscaleinServiceAction.validate(mockCommonHeader, Action.ConfigScaleIn, mockAI, mockPayload);
162 status = (Status) Whitebox.getInternalState(configscaleinServiceAction, "status");
163 Assert.assertEquals("should return invalid parameter",
164 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
166 // test space payload
167 Mockito.doReturn(" ").when(mockPayload).getValue();
168 configscaleinServiceAction.validate(mockCommonHeader, Action.ConfigScaleIn, mockAI, mockPayload);
169 status = (Status) Whitebox.getInternalState(configscaleinServiceAction, "status");
170 Assert.assertEquals("should return invalid parameter",
171 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());