e065ccd14fe10ed33e46e48017f106c14170ed66
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / ConfirmVolumeGroupNameTest.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.common.scripts
22
23 import com.github.tomakehurst.wiremock.junit.WireMockRule
24
25 import static org.mockito.Mockito.*
26 import org.camunda.bpm.engine.ProcessEngineServices
27 import org.camunda.bpm.engine.RepositoryService
28 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
29 import org.camunda.bpm.engine.repository.ProcessDefinition
30 import org.junit.Assert
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.ArgumentCaptor
35 import org.mockito.Captor
36 import org.mockito.Mockito
37 import org.mockito.runners.MockitoJUnitRunner
38 import org.onap.so.bpmn.core.WorkflowException
39
40 import static com.github.tomakehurst.wiremock.client.WireMock.*
41
42 @RunWith(MockitoJUnitRunner.class)
43 class ConfirmVolumeGroupNameTest extends MsoGroovyTest {
44
45         private static final def AAA_URI = "uri_test"
46         private static final def AIC_CLOUD_REGION = "AicClReg_test"
47         private static final def VOLUME_GROUP_NAME = "volumeTestGName"
48         private static final def VOLUME_GROUP_ID = "vol_gr_id_test"
49
50         @Captor
51         ArgumentCaptor<ExecutionEntity> captor=  ArgumentCaptor.forClass(ExecutionEntity.class);
52
53         @Rule
54         public WireMockRule wireMockRule = new WireMockRule(8090);
55
56         @Test
57         void preProcessRequestSuccessful() {
58                 ExecutionEntity mockExecution = setupMock()
59                 when(mockExecution.getVariable("ConfirmVolumeGroupName_volumeGroupId")).thenReturn(VOLUME_GROUP_ID)
60                 when(mockExecution.getVariable("ConfirmVolumeGroupName_volumeGroupName")).thenReturn(VOLUME_GROUP_NAME)
61                 when(mockExecution.getVariable("ConfirmVolumeGroupName_aicCloudRegion")).thenReturn(AIC_CLOUD_REGION)
62
63                 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn('namespace_test')
64                 when(mockExecution.getVariable("mso.workflow.ConfirmVolumeGroupName.aai.cloud-region.uri")).thenReturn(AAA_URI)
65                 new ConfirmVolumeGroupName().preProcessRequest(mockExecution)
66
67                 verifyInitProcessVariables(mockExecution)
68                 verify(mockExecution).setVariable("CVGN_volumeGroupId", VOLUME_GROUP_ID)
69                 verify(mockExecution).setVariable("CVGN_volumeGroupName", "volumeTestGName")
70                 verify(mockExecution).setVariable("CVGN_aicCloudRegion", AIC_CLOUD_REGION)
71                 verify(mockExecution).setVariable("CVGN_volumeGroupGetEndpoint",
72                                 "${AAA_URI}/${AIC_CLOUD_REGION}/volume-groups/volume-group/" + VOLUME_GROUP_ID)
73         }
74
75         private void verifyInitProcessVariables(ExecutionEntity mockExecution) {
76                 verify(mockExecution).setVariable("prefix", "CVGN_")
77                 verify(mockExecution).setVariable("CVGN_volumeGroupId", null)
78                 verify(mockExecution).setVariable("CVGN_volumeGroupName", null)
79                 verify(mockExecution).setVariable("CVGN_aicCloudRegion", null)
80                 verify(mockExecution).setVariable("CVGN_volumeGroupGetEndpoint", null)
81                 verify(mockExecution).setVariable("CVGN_volumeGroupNameMatches", false)
82                 verify(mockExecution).setVariable("CVGN_queryVolumeGroupResponseCode", null)
83                 verify(mockExecution).setVariable("CVGN_queryVolumeGroupResponse", "")
84                 verify(mockExecution).setVariable("CVGN_ResponseCode", null)
85                 verify(mockExecution).setVariable("RollbackData", null)
86         }
87
88         @Test
89         void checkAAIQueryResult_volumeGroupNamesMatch() {
90                 ExecutionEntity mockExecution = setupMock()
91                 commonPartOfCheckAAIQueryTest(mockExecution, VOLUME_GROUP_NAME)
92                 verify(mockExecution).setVariable("CVGN_volumeGroupNameMatches", true)
93         }
94
95         @Test
96         void checkAAIQueryResult_volumeGroupNamesDoNotMatch() {
97                 ExecutionEntity mockExecution = setupMock()
98                 commonPartOfCheckAAIQueryTest(mockExecution, "grName2")
99                 verify(mockExecution, Mockito.times(0)).setVariable("CVGN_volumeGroupNameMatches", true)
100         }
101
102         private void commonPartOfCheckAAIQueryTest(ExecutionEntity mockExecution, def volumeGroupName) {
103                 when(mockExecution.getVariable("CVGN_volumeGroupName")).thenReturn(VOLUME_GROUP_NAME)
104                 def xml = "<volume-group-name>" + volumeGroupName + "</volume-group-name>"
105                 when(mockExecution.getVariable("CVGN_queryVolumeGroupResponse")).thenReturn(xml)
106                 new ConfirmVolumeGroupName().checkAAIQueryResult(mockExecution)
107                 verify(mockExecution).setVariable("CVGN_volumeGroupNameMatches", false)
108         }
109
110         @Test
111         public void testQueryAAIForVolumeGroupId() {
112                 ExecutionEntity mockExecution = setupMock()
113                 when(mockExecution.getVariable("aai.endpoint")).thenReturn('http://localhost:8090')
114                 when(mockExecution.getVariable("CVGN_volumeGroupGetEndpoint")).thenReturn('/aai/test/volume-groups/volume-group/testVolumeGroup')
115                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn('true')
116
117                 mockData()
118
119                 ConfirmVolumeGroupName confirmVolumeGroupName = new ConfirmVolumeGroupName()
120                 confirmVolumeGroupName.queryAAIForVolumeGroupId(mockExecution)
121                 verify(mockExecution).setVariable("CVGN_queryVolumeGroupResponseCode",200)
122                 verify(mockExecution).setVariable("CVGN_queryVolumeGroupResponse","")
123         }
124
125         @Test
126         public void testQueryAAIForVolumeGroupId_404() {
127
128                 ExecutionEntity mockExecution = setupMock()
129                 try {
130                         when(mockExecution.getVariable("CVGN_volumeGroupGetEndpoint")).thenReturn('/aai/test/volume-groups/volume-group/testVolumeGroup')
131                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn('true')
132
133                         mockData()
134
135                         ConfirmVolumeGroupName confirmVolumeGroupName = new ConfirmVolumeGroupName()
136                         confirmVolumeGroupName.queryAAIForVolumeGroupId(mockExecution)
137                 }
138                 catch(Exception ex){
139
140                 }
141                 Mockito.verify(mockExecution,times(4)).setVariable(captor.capture(),captor.capture())
142                 WorkflowException workflowException = captor.getValue()
143                 Assert.assertEquals("AAI GET Failed",workflowException.getErrorMessage())
144                 Assert.assertEquals(500,workflowException.getErrorCode())
145         }
146         private void  mockData() {
147                 stubFor(get(urlMatching("/aai/test/volume-groups/volume-group/testVolumeGroup"))
148                                 .willReturn(aResponse()
149                                 .withStatus(200)))
150         }
151
152         private ExecutionEntity setupMock() {
153
154                 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
155                 when(mockProcessDefinition.getKey()).thenReturn("ConfirmVolumeGroupName")
156                 RepositoryService mockRepositoryService = mock(RepositoryService.class)
157                 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
158                 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("ConfirmVolumeGroupName")
159                 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
160                 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
161                 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
162                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
163                 when(mockExecution.getId()).thenReturn("100")
164                 when(mockExecution.getProcessDefinitionId()).thenReturn("ConfirmVolumeGroupName")
165                 when(mockExecution.getProcessInstanceId()).thenReturn("ConfirmVolumeGroupName")
166                 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
167                 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
168                 return mockExecution
169         }
170 }