2 * ============LICENSE_START=======================================================
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
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.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.common.scripts
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
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
49 class ConfirmVolumeGroupNameTest {
51 private static final AAIResourceUri RESOURCE_URI = AAIUriFactory.createResourceFromExistingURI(
52 Types.VOLUME_GROUP, UriBuilder.fromPath('/aai/test/volume-groups/volume-group/testVolumeGroup').build())
54 private ConfirmVolumeGroupName confirmVolumeGroupName
56 private VolumeGroup volumeGroup
58 private AAIResourcesClient client
59 private ExceptionUtilFake exceptionUtilFake
61 private DelegateExecution delegateExecution
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)
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))
80 delegateExecution.setVariable("ConfirmVolumeGroupName_volumeGroupId", volumeGroupId)
81 delegateExecution.setVariable("ConfirmVolumeGroupName_volumeGroupName", volumeGroupName)
82 delegateExecution.setVariable("ConfirmVolumeGroupName_aicCloudRegion", aicCloudRegion)
83 delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", uri)
85 confirmVolumeGroupName.preProcessRequest(delegateExecution)
87 assertEquals(ConfirmVolumeGroupName.Prefix, delegateExecution.getVariable("prefix"))
89 assertEquals(volumeGroupId, delegateExecution.getVariable("CVGN_volumeGroupId"))
90 assertEquals(volumeGroupName, delegateExecution.getVariable("CVGN_volumeGroupName"))
91 assertEquals(aicCloudRegion, delegateExecution.getVariable("CVGN_aicCloudRegion"))
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))
101 confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution)
103 assertEquals(HttpStatus.OK.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
104 assertEquals(volumeGroup, delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
108 public void queryAAIForVolumeGroupId_shouldFailWith404_whenVolumeGroupDoesNotExist() {
109 delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
110 when(client.get(VolumeGroup.class, RESOURCE_URI)).thenReturn(Optional.empty())
112 confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution)
114 assertEquals(HttpStatus.NOT_FOUND.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
115 assertEquals("Volume Group not Found!", delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
119 public void queryAAIForVolumeGroupId_shouldThrowWorkflowException_whenRuntimeExceptionIsThrown() throws BpmnError {
120 delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
121 delegateExecution.setVariable("testProcessKey", "process-key1")
123 def errorMsg = "my runtime exception"
124 when(client.get(VolumeGroup.class, RESOURCE_URI)).thenThrow(new RuntimeException(errorMsg))
126 def exceptionMsg = "AAI GET Failed"
128 BpmnError error = catchThrowableOfType(
129 { -> confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution) }, BpmnError.class)
131 assertEquals(String.format("MSOWorkflowException: %s", exceptionMsg), error.getMessage())
132 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value().toString(), error.getErrorCode())
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())
142 public void checkAAIQueryResult_shouldSetVolumeGroupNameMatchesToFalse_whenResponseCodeIs404() {
143 delegateExecution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.NOT_FOUND)
144 delegateExecution.setVariable("CVGN_volumeGroupName", "")
146 confirmVolumeGroupName.checkAAIQueryResult(delegateExecution)
148 assertFalse(delegateExecution.getVariable("CVGN_volumeGroupNameMatches"))
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())
157 confirmVolumeGroupName.checkAAIQueryResult(delegateExecution)
159 assertTrue(delegateExecution.getVariable("CVGN_volumeGroupNameMatches"))
163 public void handleVolumeGroupNameNoMatch_shouldThrowBpmnErrorException() {
164 def volumeGroupId = "volume-group-id"
165 def volumeGroupName = "volume-group-name"
167 delegateExecution.setVariable("CVGN_volumeGroupId", volumeGroupId)
168 delegateExecution.setVariable("CVGN_volumeGroupName", volumeGroupName)
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'))
173 BpmnError error = catchThrowableOfType(
174 { -> confirmVolumeGroupName.handleVolumeGroupNameNoMatch(delegateExecution) }, BpmnError.class)
176 assertEquals(String.format("MSOWorkflowException: %s", errorMessage), error.getMessage())
177 assertEquals("1002", error.getErrorCode())
179 assertEquals(1002, exceptionUtilFake.getErrorCode())
180 assertEquals(errorMessage, exceptionUtilFake.getErrorMessage())
181 assertEquals(delegateExecution, exceptionUtilFake.getDelegateExecution())
185 public void reportSuccess_shouldSetWorkflowResponseToEmptyString() {
186 confirmVolumeGroupName.reportSuccess(delegateExecution)
187 assertEquals("", delegateExecution.getVariable("WorkflowResponse"))
190 private VolumeGroup createVolumeGroup() {
191 VolumeGroup volumeGroup = new VolumeGroup()
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())
206 private static class ExceptionUtilFake extends ExceptionUtil {
208 private int errorCode
209 private String errorMessage
210 private DelegateExecution execution
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}")
220 public int getErrorCode() {
224 public String getErrorMessage() {
228 public DelegateExecution getDelegateExecution() {