ConfigScaleOut-Payload parameter to manadatory
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / ConfigScaleOutServiceTest.java
1
2 /*-
3  * ============LICENSE_START=======================================================
4  * ONAP : APPC
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider.lcm.service;
25
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.onap.appc.util.JsonUtil;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.*;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
38 import org.onap.appc.domainmodel.lcm.ResponseContext;
39 import org.onap.appc.executor.objects.LCMCommandStatus;
40 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
41 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
42
43 import java.util.Map;
44
45 import static org.mockito.Matchers.any;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.spy;
48 import static org.mockito.Mockito.times;
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class ConfigScaleOutServiceTest {
52     private final Action myAction = Action.ConfigScaleOut;
53     private final String PAYLOAD_STRING = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
54     private ConfigScaleOutInput mockInput = mock(ConfigScaleOutInput.class);
55     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
56     private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
57     private Payload mockPayload = mock(Payload.class);
58
59     private ConfigScaleOutService configscaleoutServiceAction;
60     @Before
61     public void setUp() throws Exception {
62
63         configscaleoutServiceAction = spy(new ConfigScaleOutService());
64     }
65
66     @Test
67     public void testProcess() throws Exception {
68         // test error occurs in validation
69         ConfigScaleOutOutputBuilder outputBuilder = configscaleoutServiceAction.process(mockInput);
70         Mockito.verify(configscaleoutServiceAction, times(0)).proceedAction(any(),any(),any());
71         Assert.assertTrue("Should not have commonHeader as we did not mock it",outputBuilder.getCommonHeader() == null);
72         Assert.assertEquals("should return missing parameter status",
73                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
74                 outputBuilder.getStatus().getCode());
75
76         // make validation pass
77         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
78         Mockito.doReturn(mockPayload).when(mockInput).getPayload();
79         Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
80         // to make validation pass
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
87         Mockito.doReturn(myAction).when(mockInput).getAction();
88         Mockito.doReturn(mockAI).when(mockInput).getActionIdentifiers();
89         Mockito.doReturn("vnfId").when(mockAI).getVnfId();
90
91         // test processAction return without error
92         RequestExecutor mockExecutor = mock(RequestExecutor.class);
93       //  whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
94
95         RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
96         Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
97
98         ResponseContext mockResponseContext = mock(ResponseContext.class);
99         Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
100
101         org.onap.appc.domainmodel.lcm.Status mockStatus = mock(org.onap.appc.domainmodel.lcm.Status.class);
102         Integer successCode = Integer.valueOf(LCMCommandStatus.SUCCESS.getResponseCode());
103         Mockito.doReturn(successCode).when(mockStatus).getCode();
104         Mockito.doReturn(mockStatus).when(mockResponseContext).getStatus();
105         RequestHandlerInput requestHandlerInputInput = mock(RequestHandlerInput.class);
106         AbstractBaseService abstractBaseService = mock(AbstractBaseService.class);
107         Mockito.when(abstractBaseService.executeAction(requestHandlerInputInput)).thenReturn(mockOutput);
108         try {
109             outputBuilder = configscaleoutServiceAction.process(mockInput);
110         }catch(Exception e){
111             Assert.assertTrue(true);
112         }
113         Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() == null);
114         Assert.assertEquals("should return success status", new Integer(302), outputBuilder.getStatus().getCode());
115     }
116
117     @Test
118     public void testValidate() throws Exception {
119         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
120         Status status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
121         Assert.assertEquals("should return missing parameter",
122                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
123         Mockito.verify(configscaleoutServiceAction, times(0)).buildStatusForParamName(any(), any());
124         Mockito.verify(configscaleoutServiceAction, times(0)).buildStatusForErrorMsg(any(), any());
125
126         ZULU mockTimeStamp = mock(ZULU.class);
127         Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
128         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
129         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
130         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
131
132         // test empty action
133         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
134         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
135         Assert.assertEquals("Should return missing parameter for action",
136                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
137
138         // test empty ActionIdentifier
139         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
140         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
141         Assert.assertEquals("should return missing parameter",
142                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
143
144         // test Invalid VNF_ID
145         Mockito.doReturn("").when(mockAI).getVnfId();
146         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
147         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
148         Assert.assertEquals("should return invalid parameter",
149                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
150
151         // test null payload
152         Mockito.doReturn("vnfId").when(mockAI).getVnfId();
153         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, null);
154         Mockito.verify(configscaleoutServiceAction, times(1)).validateExcludedActIds(any(), any());
155         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
156         Assert.assertEquals("should return missing parameter",
157                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
158
159         // test empty payload
160
161         Mockito.doReturn("").when(mockPayload).getValue();
162         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, mockPayload);
163         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
164         Assert.assertEquals("should return invalid parameter",
165                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
166
167         // test space payload
168         Mockito.doReturn(" ").when(mockPayload).getValue();
169         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, mockPayload);
170         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
171         Assert.assertEquals("should return invalid parameter",
172                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
173     }
174 }
175