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