Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / AbstractBaseServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.provider.lcm.service;
26
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;
40
41 import java.util.EnumSet;
42
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.spy;
45
46 public class AbstractBaseServiceTest {
47     private Action expectedAction = Action.Query;
48     private String rpcName = expectedAction.name().toLowerCase();
49
50     private CommonHeader commonHeader = mock(CommonHeader.class);
51     private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
52     private testAbc testAbstractBaseService;
53
54     class testAbc extends AbstractBaseService {
55         public testAbc() {
56             super(AbstractBaseServiceTest.this.expectedAction);
57         }
58     }
59
60     @Before
61     public void setUp() throws Exception {
62         testAbstractBaseService = spy(new testAbc());
63     }
64
65     @Test
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());
71     }
72
73     @Test
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());
79
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();
85
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());
90
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());
95
96         // test validation passed
97         status = testAbstractBaseService.validateInput(commonHeader, Action.Query, mockAI);
98         Assert.assertTrue("Should have null status", status == null);
99     }
100
101     @Test
102     public void testValidateVnfId() throws Exception {
103         // Skip test input validation, as it is all done in testValidateInput
104
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();
110
111         // test null VNF ID
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());
115
116         // test empty VNF_ID
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());
121
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);
126     }
127
128     @Test
129     public void testValidateVserverId() throws Exception {
130         // Skip test input validation, as it is all done in testValidateInput
131
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();
137
138         // test null VNF ID
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());
142
143         // test empty VNF_ID
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());
148
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);
153     }
154
155     @Test
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);
160
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());
165
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());
170
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());
175
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());
180
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());
190     }
191
192     @Test
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());
199     }
200
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)) {
205                 continue;
206             }
207             msgBuilder.append(aKey.getKeyName()).append(testAbstractBaseService.DELIMITER_COMMA);
208         }
209         String msg = msgBuilder.toString();
210         return new Params().addParam("errorMsg", msg.substring(0, msg.length() -1));
211     }
212 }