3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.service;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mockito;
31 import org.mockito.internal.util.reflection.Whitebox;
32 import org.mockito.runners.MockitoJUnitRunner;
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;
42 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;
47 @RunWith(MockitoJUnitRunner.class)
48 public class ConfigScaleOutServiceTest {
49 private final Action myAction = Action.ConfigScaleOut;
50 private final String PAYLOAD_STRING = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
51 private ConfigScaleOutInput mockInput = mock(ConfigScaleOutInput.class);
52 private CommonHeader mockCommonHeader = mock(CommonHeader.class);
53 private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
54 private Payload mockPayload = mock(Payload.class);
56 private ConfigScaleOutService configscaleoutServiceAction;
58 public void setUp() throws Exception {
59 configscaleoutServiceAction = spy(new ConfigScaleOutService());
63 public void testProcess() throws Exception {
64 // test error occurs in validation
65 ConfigScaleOutOutputBuilder outputBuilder = configscaleoutServiceAction.process(mockInput);
66 Mockito.verify(configscaleoutServiceAction, times(0)).proceedAction(any(),any(),any());
67 Assert.assertTrue("Should not have commonHeader as we did not mock it",outputBuilder.getCommonHeader() == null);
68 Assert.assertEquals("should return missing parameter status",
69 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
70 outputBuilder.getStatus().getCode());
72 // make validation pass
73 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
74 Mockito.doReturn(mockPayload).when(mockInput).getPayload();
75 Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
76 // to make validation pass
77 ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
78 Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
79 Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
80 Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
81 Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
83 Mockito.doReturn(myAction).when(mockInput).getAction();
84 Mockito.doReturn(mockAI).when(mockInput).getActionIdentifiers();
85 Mockito.doReturn("vnfId").when(mockAI).getVnfId();
87 // test processAction return without error
88 RequestExecutor mockExecutor = mock(RequestExecutor.class);
89 // whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
91 RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
92 Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
94 ResponseContext mockResponseContext = mock(ResponseContext.class);
95 Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
97 org.onap.appc.domainmodel.lcm.Status mockStatus = mock(org.onap.appc.domainmodel.lcm.Status.class);
98 Integer successCode = Integer.valueOf(LCMCommandStatus.SUCCESS.getResponseCode());
99 Mockito.doReturn(successCode).when(mockStatus).getCode();
100 Mockito.doReturn(mockStatus).when(mockResponseContext).getStatus();
101 RequestHandlerInput requestHandlerInputInput = mock(RequestHandlerInput.class);
102 AbstractBaseService abstractBaseService = mock(AbstractBaseService.class);
103 Mockito.when(abstractBaseService.executeAction(requestHandlerInputInput)).thenReturn(mockOutput);
105 outputBuilder = configscaleoutServiceAction.process(mockInput);
107 Assert.assertTrue(true);
109 Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() == null);
110 Assert.assertEquals("should return success status", new Integer(302), outputBuilder.getStatus().getCode());
114 public void testValidate() throws Exception {
115 configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
116 Status status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
117 Assert.assertEquals("should return missing parameter",
118 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
119 Mockito.verify(configscaleoutServiceAction, times(0)).buildStatusForParamName(any(), any());
120 Mockito.verify(configscaleoutServiceAction, times(0)).buildStatusForErrorMsg(any(), any());
122 ZULU mockTimeStamp = mock(ZULU.class);
123 Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
124 Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
125 Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
126 Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
129 configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
130 status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
131 Assert.assertEquals("Should return missing parameter for action",
132 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
134 // test empty ActionIdentifier
135 configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
136 status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
137 Assert.assertEquals("should return missing parameter",
138 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
140 // test Invalid VNF_ID
141 Mockito.doReturn("").when(mockAI).getVnfId();
142 configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
143 status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
144 Assert.assertEquals("should return invalid parameter",
145 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
148 Mockito.doReturn("vnfId").when(mockAI).getVnfId();
149 configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, null);
150 Mockito.verify(configscaleoutServiceAction, times(1)).validateExcludedActIds(any(), any());
151 status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
152 Assert.assertTrue("Should skip Payload",true);
154 // test empty payload
156 Mockito.doReturn("").when(mockPayload).getValue();
157 configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, mockPayload);
158 status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
159 Assert.assertTrue("Should skip Payload",true);
161 // test space payload
162 Mockito.doReturn(" ").when(mockPayload).getValue();
163 configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, mockPayload);
164 status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
165 Assert.assertTrue("Should skip Payload",true);