Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / GenericGetVnfTest.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 import org.camunda.bpm.engine.ProcessEngineServices
25 import org.camunda.bpm.engine.RepositoryService
26 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
27 import org.camunda.bpm.engine.repository.ProcessDefinition
28 import org.junit.Assert
29 import org.junit.Before
30 import org.junit.Ignore
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.MockitoAnnotations
38 import org.mockito.runners.MockitoJUnitRunner
39 import org.onap.so.bpmn.core.WorkflowException
40
41 import static com.github.tomakehurst.wiremock.client.WireMock.*
42 import static org.mockito.Mockito.mock
43 import static org.mockito.Mockito.times
44 import static org.mockito.Mockito.when
45
46 @RunWith(MockitoJUnitRunner.class)
47 @Ignore
48 class GenericGetVnfTest {
49
50     def prefix = "GENGV_"
51
52     @Rule
53     public WireMockRule wireMockRule = new WireMockRule(28090)
54
55     @Captor
56     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
57
58
59     @Before
60     public void init()
61     {
62         MockitoAnnotations.initMocks(this)
63     }
64
65     @Test
66     public void testGetVnfByName() {
67
68         ExecutionEntity mockExecution = setupMock()
69         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
70         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
71         when(mockExecution.getVariable(prefix+"vnfName")).thenReturn("genricVnf_test")
72         when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
73         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
74         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
75
76         mockData()
77         GenericGetVnf obj = new GenericGetVnf()
78         obj.getVnfByName(mockExecution)
79
80         Mockito.verify(mockExecution).setVariable("prefix","GENGV_")
81         Mockito.verify(mockExecution).setVariable(prefix+"getVnfPath","http://localhost:28090/aai/v9/network/generic-vnfs/generic-vnf?vnf-name=genricVnf_test&depth=1")
82     }
83
84     @Test
85     public void testGetVnfByNameEndpointNull() {
86
87         ExecutionEntity mockExecution = setupMock()
88         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
89         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
90         when(mockExecution.getVariable(prefix+"vnfName")).thenReturn("genricVnf_test")
91         when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
92         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
93         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
94
95         mockData()
96         try{
97             GenericGetVnf obj = new GenericGetVnf()
98             obj.getVnfByName(mockExecution)
99         }catch (Exception ex) {
100             println " Test End - Handle catch-throw BpmnError()! "
101         }
102
103         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
104         WorkflowException workflowException = captor.getValue()
105         Assert.assertEquals(9999, workflowException.getErrorCode())
106         Assert.assertEquals("org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
107     }
108
109     @Test
110     public void testGetVnfById() {
111
112         ExecutionEntity mockExecution = setupMock()
113         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
114         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
115         when(mockExecution.getVariable(prefix+"vnfId")).thenReturn("genricVnf_test")
116         when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
117         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
118         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
119
120         mockData()
121         GenericGetVnf obj = new GenericGetVnf()
122         obj.getVnfById(mockExecution)
123
124         Mockito.verify(mockExecution).setVariable("prefix","GENGV_")
125         Mockito.verify(mockExecution).setVariable(prefix+"getVnfPath","http://localhost:28090/aai/v9/network/generic-vnfs/generic-vnf/genricVnf_test?depth=1")
126     }
127
128     @Test
129     public void testGetVnfByIdEndpointNull() {
130
131         ExecutionEntity mockExecution = setupMock()
132         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
133         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
134         when(mockExecution.getVariable(prefix+"vnfId")).thenReturn("genricVnf_test")
135         when(mockExecution.getVariable(prefix+"type")).thenReturn("generic-vnf")
136         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
137         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
138
139         mockData()
140         try{
141             GenericGetVnf obj = new GenericGetVnf()
142             obj.getVnfById(mockExecution)
143         }catch (Exception ex) {
144             println " Test End - Handle catch-throw BpmnError()! "
145         }
146
147         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
148         WorkflowException workflowException = captor.getValue()
149         Assert.assertEquals(9999, workflowException.getErrorCode())
150         Assert.assertEquals("org.apache.http.client.ClientProtocolException", workflowException.getErrorMessage())
151     }
152
153     private static ExecutionEntity setupMock() {
154         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
155         when(mockProcessDefinition.getKey()).thenReturn("DoCreateVfModule")
156         RepositoryService mockRepositoryService = mock(RepositoryService.class)
157         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
158         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateVfModule")
159         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
160         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
161         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
162
163         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
164         // Initialize prerequisite variables
165         when(mockExecution.getId()).thenReturn("100")
166         when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateVfModule")
167         when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateVfModule")
168         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
169         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
170
171         return mockExecution
172     }
173
174     private static void mockData() {
175         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf.*"))
176                 .willReturn(aResponse()
177                 .withStatus(200).withHeader("Content-Type", "text/xml")
178                 .withBodyFile("GenericFlows/getGenericVnfResponse.xml")))
179     }
180 }