8a80d79cf9d5899bdb662fdba1c4bc37ee9d583d
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright 2018 Nokia
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  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.common.scripts
24
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake
28 import org.junit.Before
29 import org.junit.Test
30 import org.mockito.Mock
31 import org.mockito.MockitoAnnotations
32 import org.onap.aai.domain.yang.RelationshipList
33 import org.onap.aai.domain.yang.VolumeGroup
34 import org.onap.so.bpmn.common.scripts.ConfirmVolumeGroupName
35 import org.onap.so.bpmn.common.scripts.ConfirmVolumeGroupNameFactory
36 import org.onap.so.bpmn.common.scripts.ExceptionUtil
37 import org.onap.aaiclient.client.aai.AAIObjectType
38 import org.onap.aaiclient.client.aai.AAIResourcesClient
39 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
40 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
41 import org.onap.so.constants.Defaults
42 import org.springframework.http.HttpStatus
43
44 import javax.ws.rs.core.UriBuilder
45
46 import static org.assertj.core.api.Assertions.catchThrowableOfType
47 import static org.junit.Assert.assertEquals
48 import static org.junit.Assert.assertFalse
49 import static org.junit.Assert.assertTrue
50 import static org.mockito.Mockito.spy
51 import static org.mockito.Mockito.when
52
53 class ConfirmVolumeGroupNameTest {
54
55     private static final AAIResourceUri RESOURCE_URI = AAIUriFactory.createResourceFromExistingURI(
56             AAIObjectType.VOLUME_GROUP, UriBuilder.fromPath('/aai/test/volume-groups/volume-group/testVolumeGroup').build())
57
58     private ConfirmVolumeGroupName confirmVolumeGroupName
59     @Mock
60     private VolumeGroup volumeGroup
61     @Mock
62     private AAIResourcesClient client
63     private ExceptionUtilFake exceptionUtilFake
64
65     private DelegateExecution delegateExecution
66
67     @Before
68     public void init() throws IOException {
69         exceptionUtilFake = new ExceptionUtilFake()
70         confirmVolumeGroupName = spy(new ConfirmVolumeGroupName(exceptionUtilFake))
71         MockitoAnnotations.initMocks(this)
72         delegateExecution = new DelegateExecutionFake()
73         volumeGroup = createVolumeGroup()
74         when(confirmVolumeGroupName.getAAIClient()).thenReturn(client)
75     }
76
77     @Test
78     public void preProcessRequest_shouldSetUpVariables() {
79         String volumeGroupId = "volume-group-id-1"
80         String volumeGroupName = "volume-group-name-1"
81         String aicCloudRegion = "aic-cloud-region-1"
82         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER, aicCloudRegion, volumeGroupId)
83
84         delegateExecution.setVariable("ConfirmVolumeGroupName_volumeGroupId", volumeGroupId)
85         delegateExecution.setVariable("ConfirmVolumeGroupName_volumeGroupName", volumeGroupName)
86         delegateExecution.setVariable("ConfirmVolumeGroupName_aicCloudRegion", aicCloudRegion)
87         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", uri)
88
89         confirmVolumeGroupName.preProcessRequest(delegateExecution)
90
91         assertEquals(ConfirmVolumeGroupName.Prefix, delegateExecution.getVariable("prefix"))
92
93         assertEquals(volumeGroupId, delegateExecution.getVariable("CVGN_volumeGroupId"))
94         assertEquals(volumeGroupName, delegateExecution.getVariable("CVGN_volumeGroupName"))
95         assertEquals(aicCloudRegion, delegateExecution.getVariable("CVGN_aicCloudRegion"))
96     }
97
98     @Test
99     public void queryAAIForVolumeGroupId_shouldSucceed_whenVolumeGroupExists() {
100         delegateExecution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.OK)
101         delegateExecution.setVariable("CVGN_queryVolumeGroupResponse", volumeGroup)
102         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
103         when(client.get(VolumeGroup.class, RESOURCE_URI)).thenReturn(Optional.of(volumeGroup))
104
105         confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution)
106
107         assertEquals(HttpStatus.OK.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
108         assertEquals(volumeGroup, delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
109     }
110
111     @Test
112     public void queryAAIForVolumeGroupId_shouldFailWith404_whenVolumeGroupDoesNotExist() {
113         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
114         when(client.get(VolumeGroup.class, RESOURCE_URI)).thenReturn(Optional.empty())
115
116         confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution)
117
118         assertEquals(HttpStatus.NOT_FOUND.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
119         assertEquals("Volume Group not Found!", delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
120     }
121
122     @Test
123     public void queryAAIForVolumeGroupId_shouldThrowWorkflowException_whenRuntimeExceptionIsThrown() throws BpmnError {
124         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
125         delegateExecution.setVariable("testProcessKey", "process-key1")
126
127         def errorMsg = "my runtime exception"
128         when(client.get(VolumeGroup.class, RESOURCE_URI)).thenThrow(new RuntimeException(errorMsg))
129
130         def exceptionMsg = "AAI GET Failed"
131
132         BpmnError error = catchThrowableOfType(
133                 { -> confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution) }, BpmnError.class)
134
135         assertEquals(String.format("MSOWorkflowException: %s", exceptionMsg), error.getMessage())
136         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value().toString(), error.getErrorCode())
137
138         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
139         assertEquals(String.format("AAI GET Failed:%s", errorMsg), delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
140         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), exceptionUtilFake.getErrorCode())
141         assertEquals(exceptionMsg, exceptionUtilFake.getErrorMessage())
142         assertEquals(delegateExecution, exceptionUtilFake.getDelegateExecution())
143     }
144
145     @Test
146     public void checkAAIQueryResult_shouldSetVolumeGroupNameMatchesToFalse_whenResponseCodeIs404() {
147         delegateExecution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.NOT_FOUND)
148         delegateExecution.setVariable("CVGN_volumeGroupName", "")
149
150         confirmVolumeGroupName.checkAAIQueryResult(delegateExecution)
151
152         assertFalse(delegateExecution.getVariable("CVGN_volumeGroupNameMatches"))
153     }
154
155     @Test
156     public void checkAAIQueryResult_shouldSetVolumeGroupNameMatchesToTrue_whenResponseCodeIs200AndVolumeGroupNameExists() {
157         delegateExecution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.OK.value())
158         delegateExecution.setVariable("CVGN_queryVolumeGroupResponse", volumeGroup)
159         delegateExecution.setVariable("CVGN_volumeGroupName", volumeGroup.getVolumeGroupName())
160
161         confirmVolumeGroupName.checkAAIQueryResult(delegateExecution)
162
163         assertTrue(delegateExecution.getVariable("CVGN_volumeGroupNameMatches"))
164     }
165
166     @Test
167     public void handleVolumeGroupNameNoMatch_shouldThrowBpmnErrorException() {
168         def volumeGroupId = "volume-group-id"
169         def volumeGroupName = "volume-group-name"
170
171         delegateExecution.setVariable("CVGN_volumeGroupId", volumeGroupId)
172         delegateExecution.setVariable("CVGN_volumeGroupName", volumeGroupName)
173
174         def errorMessage = String.format("Error occurred - volume group id %s is not associated with %s",
175                 delegateExecution.getVariable('CVGN_volumeGroupId'), delegateExecution.getVariable('CVGN_volumeGroupName'))
176
177         BpmnError error = catchThrowableOfType(
178                 { -> confirmVolumeGroupName.handleVolumeGroupNameNoMatch(delegateExecution) }, BpmnError.class)
179
180         assertEquals(String.format("MSOWorkflowException: %s", errorMessage), error.getMessage())
181         assertEquals("1002", error.getErrorCode())
182
183         assertEquals(1002, exceptionUtilFake.getErrorCode())
184         assertEquals(errorMessage, exceptionUtilFake.getErrorMessage())
185         assertEquals(delegateExecution, exceptionUtilFake.getDelegateExecution())
186     }
187
188     @Test
189     public void reportSuccess_shouldSetWorkflowResponseToEmptyString() {
190         confirmVolumeGroupName.reportSuccess(delegateExecution)
191         assertEquals("", delegateExecution.getVariable("WorkflowResponse"))
192     }
193
194     private VolumeGroup createVolumeGroup() {
195         VolumeGroup volumeGroup = new VolumeGroup()
196
197         volumeGroup.setVolumeGroupId("volume-group-id")
198         volumeGroup.setVolumeGroupName("volume-group-name")
199         volumeGroup.setHeatStackId("heat-stack-id")
200         volumeGroup.setVnfType("vnf-type")
201         volumeGroup.setOrchestrationStatus("orchestration-status")
202         volumeGroup.setModelCustomizationId("model-customization-id")
203         volumeGroup.setVfModuleModelCustomizationId("vf-module-model-customization-id")
204         volumeGroup.setResourceVersion("resource-version")
205         volumeGroup.setRelationshipList(new RelationshipList())
206
207         return volumeGroup
208     }
209
210     private static class ExceptionUtilFake extends ExceptionUtil {
211
212         private int errorCode
213         private String errorMessage
214         private DelegateExecution execution
215
216         @Override
217         public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
218             this.errorCode = errorCode
219             this.errorMessage = errorMessage
220             this.execution = execution
221             throw new BpmnError(errorCode.toString(), "MSOWorkflowException: ${errorMessage}")
222         }
223
224         public int getErrorCode() {
225             return errorCode
226         }
227
228         public String getErrorMessage() {
229             return errorMessage
230         }
231
232         public DelegateExecution getDelegateExecution() {
233             return execution
234         }
235     }
236
237 }