2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.provider.lcm.service;
27 import org.junit.Assert;
28 import junit.framework.TestCase;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.mockito.internal.util.reflection.Whitebox;
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.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
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;
48 import static org.powermock.api.mockito.PowerMockito.whenNew;
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest({QuiesceTrafficService.class, RequestExecutor.class})
52 public class QuiesceTrafficServiceTest {
53 private final Action myAction = Action.QuiesceTraffic;
54 private final String PAYLOAD_STRING = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
55 private QuiesceTrafficInput mockInput = mock(QuiesceTrafficInput.class);
56 private CommonHeader mockCommonHeader = mock(CommonHeader.class);
57 private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
58 private Payload mockPayload = mock(Payload.class);
60 private QuiesceTrafficService quiesceServiceAction;
62 public void setUp() throws Exception {
63 quiesceServiceAction = spy(new QuiesceTrafficService());
68 public void testConstructor() throws Exception {
69 Action expectedAction = Action.QuiesceTraffic;
70 Assert.assertEquals("Should have proper ACTION", expectedAction,
71 (Action) org.powermock.reflect.Whitebox.getInternalState(quiesceServiceAction, "expectedAction"));
72 Assert.assertEquals("Should have quiesce-traffic RPC name", "quiesce-traffic",
73 (org.powermock.reflect.Whitebox.getInternalState(quiesceServiceAction, "rpcName")).toString());
77 // public void testProcess() throws Exception {
78 // // test error occurs in validation
79 // QuiesceTrafficOutputBuilder outputBuilder = quiesceServiceAction.process(mockInput);
80 // Mockito.verify(quiesceServiceAction, times(0)).proceedAction(any(),any(),any());
81 // Assert.assertTrue("Should not have commonHeader as we did not mock it",outputBuilder.getCommonHeader() == null);
82 // Assert.assertEquals("should return missing parameter status",
83 // Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
84 // outputBuilder.getStatus().getCode());
86 // // make validation pass
87 // Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
88 // Mockito.doReturn(mockPayload).when(mockInput).getPayload();
89 // Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
90 // // to make validation pass
91 // ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
92 // Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
93 // Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
94 // Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
95 // Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
97 // Mockito.doReturn(myAction).when(mockInput).getAction();
98 // Mockito.doReturn(mockAI).when(mockInput).getActionIdentifiers();
99 // Mockito.doReturn("vnfId").when(mockAI).getVnfId();
101 // // test processAction return with error
102 // outputBuilder = quiesceServiceAction.process(mockInput);
103 // Mockito.verify(quiesceServiceAction, times(1)).proceedAction(any(),any(),any());
104 // Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() != null);
105 // Assert.assertEquals("should return rejected status",
106 // Integer.valueOf(LCMCommandStatus.REJECTED.getResponseCode()),
107 // outputBuilder.getStatus().getCode());
109 // // test processAction return without error
110 // RequestExecutor mockExecutor = mock(RequestExecutor.class);
111 // whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
113 // RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
114 // Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
116 // ResponseContext mockResponseContext = mock(ResponseContext.class);
117 // Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
119 // org.onap.appc.domainmodel.lcm.Status mockStatus = mock(org.onap.appc.domainmodel.lcm.Status.class);
120 // Integer successCode = Integer.valueOf(LCMCommandStatus.SUCCESS.getResponseCode());
121 // Mockito.doReturn(successCode).when(mockStatus).getCode();
122 // Mockito.doReturn(mockStatus).when(mockResponseContext).getStatus();
124 // outputBuilder = quiesceServiceAction.process(mockInput);
125 // Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() != null);
126 // Assert.assertEquals("should return success status", successCode, outputBuilder.getStatus().getCode());
130 public void testValidate() throws Exception {
131 quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
132 Status status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
133 Assert.assertEquals("should return missing parameter",
134 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
135 Mockito.verify(quiesceServiceAction, times(0)).buildStatusForParamName(any(), any());
136 Mockito.verify(quiesceServiceAction, times(0)).buildStatusForErrorMsg(any(), any());
138 ZULU mockTimeStamp = mock(ZULU.class);
139 Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
140 Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
141 Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
142 Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
145 quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
146 status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
147 Assert.assertEquals("Should return missing parameter for action",
148 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
150 // test empty ActionIdentifier
151 quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
152 status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
153 Assert.assertEquals("should return missing parameter",
154 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
156 // test Invalid VNF_ID
157 Mockito.doReturn("").when(mockAI).getVnfId();
158 quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
159 status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
160 Assert.assertEquals("should return invalid parameter",
161 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
164 Mockito.doReturn("vnfId").when(mockAI).getVnfId();
165 quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI, null);
166 Mockito.verify(quiesceServiceAction, times(1)).validateExcludedActIds(any(), any());
167 status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
168 Assert.assertEquals("should return missing parameter",
169 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
171 // test empty payload
173 Mockito.doReturn("").when(mockPayload).getValue();
174 quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI, mockPayload);
175 status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
176 Assert.assertEquals("should return invalid parameter",
177 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
179 // test space payload
180 Mockito.doReturn(" ").when(mockPayload).getValue();
181 quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI, mockPayload);
182 status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
183 Assert.assertEquals("should return invalid parameter",
184 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());