72bcfcf3598acccd9fc2dda1a0a9e7d86839f42f
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / UpdateAAIVfModuleTest.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 org.junit.rules.ExpectedException
24
25 import static org.mockito.Mockito.*
26
27 import javax.ws.rs.NotFoundException
28
29 import org.camunda.bpm.engine.delegate.BpmnError
30 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
31 import org.junit.Before
32 import org.junit.Rule
33 import org.junit.Test
34 import org.mockito.ArgumentCaptor
35 import org.mockito.Captor
36 import org.mockito.MockitoAnnotations
37 import org.mockito.Spy
38 import org.onap.so.client.aai.AAIObjectType
39 import org.onap.so.client.aai.entities.uri.AAIResourceUri
40 import org.onap.so.client.aai.entities.uri.AAIUriFactory
41 import org.onap.aai.domain.yang.VfModule
42
43 import com.github.tomakehurst.wiremock.junit.WireMockRule
44
45 class UpdateAAIVfModuleTest  extends MsoGroovyTest {
46     def prefix = "UAAIVfMod_"
47
48     @Rule
49     public ExpectedException thrown = ExpectedException.none();
50     @Spy
51     UpdateAAIVfModule updateAAIVfModule;
52
53     @Rule
54     public WireMockRule wireMockRule = new WireMockRule(28090)
55
56     @Captor
57     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
58
59     @Before
60     void init() throws IOException {
61         super.init("UpdateAAIVfModule")
62         when(updateAAIVfModule.getAAIClient()).thenReturn(client)
63     }
64
65     @Test
66     void testGetVfModule() {
67         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
68         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
69         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
70         VfModule vfModule = new VfModule()
71         vfModule.setVfModuleId("supercool")
72         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "skask", "supercool");
73         when(client.get(VfModule.class,resourceUri)).thenReturn(Optional.of(vfModule))
74         updateAAIVfModule.getVfModule(mockExecution)
75         verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 200)
76         verify(mockExecution).setVariable(prefix + "getVfModuleResponse", vfModule)
77     }
78
79     @Test
80     void testGetVfModuleNotFound() {
81         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
82         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
83         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
84         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "skask", "supercool");
85         when(client.get(VfModule.class,resourceUri)).thenReturn(Optional.empty())
86         updateAAIVfModule.getVfModule(mockExecution)
87         verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 404)
88         verify(mockExecution).setVariable(prefix + "getVfModuleResponse", "VF Module not found in AAI")
89     }
90
91     @Test
92     void testGetVfModuleException() {
93         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
94         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
95         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
96         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "skask", "supercool");
97         when(client.get(VfModule.class,resourceUri)).thenThrow(new NullPointerException("Error from AAI client"))
98         updateAAIVfModule.getVfModule(mockExecution)
99         verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 500)
100         verify(mockExecution).setVariable(prefix + "getVfModuleResponse", "AAI GET Failed:"+"Error from AAI client")
101     }
102
103
104     @Test
105     void testUpdateVfModule() {
106         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
107         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
108         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
109         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
110         VfModule vfModule = new VfModule()
111         vfModule.setVfModuleId("supercool")
112         vfModule.setResourceVersion("12345")
113         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(vfModule)
114         doNothing().when(client).update(isA(AAIResourceUri.class) as AAIResourceUri, anyObject())
115         updateAAIVfModule.updateVfModule(mockExecution)
116                 verify(mockExecution).setVariable("UAAIVfMod_updateVfModuleResponseCode", 200)
117     }
118
119     @Test
120     void testUpdateVfModuleNotFound() throws BpmnError {
121         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
122         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
123         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
124         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
125         VfModule vfModule = new VfModule()
126         vfModule.setVfModuleId("supercool")
127         vfModule.setResourceVersion("12345")
128         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(vfModule)
129         doThrow(new NotFoundException("Vf Module not found")).when(client).update(isA(AAIResourceUri.class) as AAIResourceUri, anyObject())
130         thrown.expect(BpmnError.class)
131         updateAAIVfModule.updateVfModule(mockExecution)
132                 verify(mockExecution).setVariable("UAAIVfMod_updateVfModuleResponseCode", 404)
133     }
134
135
136     @Test
137     void testUpdateVfModuleException() {
138         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
139         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
140         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
141         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
142         VfModule vfModule = new VfModule()
143         vfModule.setVfModuleId("supercool")
144         vfModule.setResourceVersion("12345")
145         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(vfModule)
146         doThrow(new IllegalStateException("Error in AAI client")).when(client).update(isA(AAIResourceUri.class) as AAIResourceUri, anyObject())
147         thrown.expect(BpmnError.class)
148         updateAAIVfModule.updateVfModule(mockExecution)
149                 verify(mockExecution).setVariable("UAAIVfMod_updateVfModuleResponseCode", 500)
150
151     }
152 }