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 org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ZULU;
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.onap.appc.executor.objects.LCMCommandStatus;
37 import org.onap.appc.executor.objects.Params;
38 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
39 import org.powermock.reflect.Whitebox;
41 import java.util.EnumSet;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.spy;
46 public class AbstractBaseServiceTest {
47 private Action expectedAction = Action.Query;
48 private String rpcName = expectedAction.name().toLowerCase();
50 private CommonHeader commonHeader = mock(CommonHeader.class);
51 private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
52 private testAbc testAbstractBaseService;
54 class testAbc extends AbstractBaseService {
56 super(AbstractBaseServiceTest.this.expectedAction);
61 public void setUp() throws Exception {
62 testAbstractBaseService = spy(new testAbc());
66 public void testConstructor() throws Exception {
67 Assert.assertEquals("Should have proper ACTION", expectedAction,
68 (Action) Whitebox.getInternalState(testAbstractBaseService, "expectedAction"));
69 Assert.assertEquals("Should have action-status RPC name", rpcName,
70 (Whitebox.getInternalState(testAbstractBaseService, "rpcName")).toString());
74 public void testValidateInput() throws Exception {
75 // test commonHeader error
76 Status status = testAbstractBaseService.validateInput(commonHeader, Action.Query, null);
77 Assert.assertEquals("should return missing parameter",
78 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
80 ZULU mockTimeStamp = mock(ZULU.class);
81 Mockito.doReturn(mockTimeStamp).when(commonHeader).getTimestamp();
82 Mockito.doReturn("api ver").when(commonHeader).getApiVer();
83 Mockito.doReturn("originator Id").when(commonHeader).getOriginatorId();
84 Mockito.doReturn("request Id").when(commonHeader).getRequestId();
86 // test invalid action
87 status = testAbstractBaseService.validateInput(commonHeader, Action.AttachVolume, null);
88 Assert.assertEquals("Should return invalid parameter for action",
89 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
91 // test null actionIdentifier
92 status = testAbstractBaseService.validateInput(commonHeader, Action.Query, null);
93 Assert.assertEquals("should return missing parameter",
94 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
96 // test validation passed
97 status = testAbstractBaseService.validateInput(commonHeader, Action.Query, mockAI);
98 Assert.assertTrue("Should have null status", status == null);
102 public void testValidateVnfId() throws Exception {
103 // Skip test input validation, as it is all done in testValidateInput
105 ZULU mockTimeStamp = mock(ZULU.class);
106 Mockito.doReturn(mockTimeStamp).when(commonHeader).getTimestamp();
107 Mockito.doReturn("api ver").when(commonHeader).getApiVer();
108 Mockito.doReturn("originator Id").when(commonHeader).getOriginatorId();
109 Mockito.doReturn("request Id").when(commonHeader).getRequestId();
112 Status status = testAbstractBaseService.validateVnfId(commonHeader, Action.Query, mockAI);
113 Assert.assertEquals("should return invalid parameter",
114 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
117 Mockito.doReturn("").when(mockAI).getVnfId();
118 status = testAbstractBaseService.validateVnfId(commonHeader, Action.Query, mockAI);
119 Assert.assertEquals("should return invalid parameter",
120 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
122 // test calling validateExcludeActId
123 Mockito.doReturn("vnfId").when(mockAI).getVnfId();
124 status = testAbstractBaseService.validateVnfId(commonHeader, Action.Query, mockAI);
125 Assert.assertTrue("Should have null status", status == null);
129 public void testValidateVserverId() throws Exception {
130 // Skip test input validation, as it is all done in testValidateInput
132 ZULU mockTimeStamp = mock(ZULU.class);
133 Mockito.doReturn(mockTimeStamp).when(commonHeader).getTimestamp();
134 Mockito.doReturn("api ver").when(commonHeader).getApiVer();
135 Mockito.doReturn("originator Id").when(commonHeader).getOriginatorId();
136 Mockito.doReturn("request Id").when(commonHeader).getRequestId();
139 Status status = testAbstractBaseService.validateVserverId(commonHeader, Action.Query, mockAI);
140 Assert.assertEquals("should return invalid parameter",
141 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
144 Mockito.doReturn("").when(mockAI).getVserverId();
145 status = testAbstractBaseService.validateVserverId(commonHeader, Action.Query, mockAI);
146 Assert.assertEquals("should return invalid parameter",
147 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
149 // test calling validateExcludeActId
150 Mockito.doReturn("vserverId").when(mockAI).getVserverId();
151 status = testAbstractBaseService.validateVserverId(commonHeader, Action.Query, mockAI);
152 Assert.assertTrue("Should have null status", status == null);
156 public void testValidateExcludedActIds() throws Exception {
157 EnumSet<AbstractBaseService.ACTID_KEYS> exclutionKeys = EnumSet.of(AbstractBaseService.ACTID_KEYS.VNF_ID);
158 Status status = testAbstractBaseService.validateExcludedActIds(mockAI, exclutionKeys);
159 Assert.assertTrue("Should have not error", status == null);
161 Integer expectedErrorCode = Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode());
162 Mockito.doReturn("vnfc name").when(mockAI).getVnfcName();
163 status = testAbstractBaseService.validateExcludedActIds(mockAI, exclutionKeys);
164 Assert.assertEquals("Should have error for vnfc name", expectedErrorCode, status.getCode());
166 Mockito.doReturn(null).when(mockAI).getVnfcName();
167 Mockito.doReturn("vserver Id").when(mockAI).getVserverId();
168 status = testAbstractBaseService.validateExcludedActIds(mockAI, exclutionKeys);
169 Assert.assertEquals("Should have error for vserver Id", expectedErrorCode, status.getCode());
171 Mockito.doReturn(null).when(mockAI).getVserverId();
172 Mockito.doReturn("vf module Id").when(mockAI).getVfModuleId();
173 status = testAbstractBaseService.validateExcludedActIds(mockAI, exclutionKeys);
174 Assert.assertEquals("Should have error for vf module Id", expectedErrorCode, status.getCode());
176 Mockito.doReturn(null).when(mockAI).getServiceInstanceId();
177 Mockito.doReturn("service instance Id").when(mockAI).getServiceInstanceId();
178 status = testAbstractBaseService.validateExcludedActIds(mockAI, exclutionKeys);
179 Assert.assertEquals("Should have error for service instance Id", expectedErrorCode, status.getCode());
181 Mockito.doReturn("vnfc name").when(mockAI).getVnfcName();
182 Mockito.doReturn("vserver Id").when(mockAI).getVserverId();
183 Mockito.doReturn("vf module Id").when(mockAI).getVfModuleId();
184 Mockito.doReturn("vnf Id").when(mockAI).getVnfId();
185 status = testAbstractBaseService.validateExcludedActIds(mockAI, exclutionKeys);
186 Assert.assertEquals("Should have error code", expectedErrorCode, status.getCode());
187 Assert.assertEquals("Should have error message",
188 LCMCommandStatus.INVALID_INPUT_PARAMETER.getFormattedMessage(getMsgParams(exclutionKeys)),
189 status.getMessage());
193 public void testExecuteAction() throws Exception {
194 RequestHandlerOutput output = testAbstractBaseService.executeAction(null);
195 Assert.assertTrue("Should return null RequestHandlerOutput", output == null);
196 Status status = Whitebox.getInternalState(testAbstractBaseService, "status");
197 Assert.assertEquals("Should have error code",
198 Integer.valueOf(LCMCommandStatus.UNEXPECTED_ERROR.getResponseCode()), status.getCode());
201 private Params getMsgParams(EnumSet<AbstractBaseService.ACTID_KEYS> exclutionKeys) {
202 StringBuilder msgBuilder = new StringBuilder();
203 for (QueryService.ACTID_KEYS aKey : AbstractBaseService.ACTID_KEYS.values()) {
204 if (exclutionKeys.contains(aKey)) {
207 msgBuilder.append(aKey.getKeyName()).append(testAbstractBaseService.DELIMITER_COMMA);
209 String msg = msgBuilder.toString();
210 return new Params().addParam("errorMsg", msg.substring(0, msg.length() -1));