Changed the object mapper to ignore unknown fields
[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 import org.camunda.bpm.engine.ProcessEngineServices
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.aaiclient.client.aai.AAIObjectType
39 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
40 import org.onap.aaiclient.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     @Captor
54     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
55
56     @Before
57     void init() throws IOException {
58         super.init("UpdateAAIVfModule")
59         when(updateAAIVfModule.getAAIClient()).thenReturn(client)
60         mockExecution = setupMock("UpdateAAIVfModule")
61     }
62
63     @Test
64     void testGetVfModule() {
65         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
66         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
67         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
68         VfModule vfModule = new VfModule()
69         vfModule.setVfModuleId("supercool")
70         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "skask", "supercool");
71         when(client.get(VfModule.class,resourceUri)).thenReturn(Optional.of(vfModule))
72         updateAAIVfModule.getVfModule(mockExecution)
73         verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 200)
74         verify(mockExecution).setVariable(prefix + "getVfModuleResponse", vfModule)
75     }
76
77     @Test
78     void testGetVfModuleNotFound() {
79         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
80         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
81         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
82         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "skask", "supercool");
83         when(client.get(VfModule.class,resourceUri)).thenReturn(Optional.empty())
84         updateAAIVfModule.getVfModule(mockExecution)
85         verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 404)
86         verify(mockExecution).setVariable(prefix + "getVfModuleResponse", "VF Module not found in AAI")
87     }
88
89     @Test
90     void testGetVfModuleException() {
91         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
92         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
93         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
94         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "skask", "supercool");
95         when(client.get(VfModule.class,resourceUri)).thenThrow(new NullPointerException("Error from AAI client"))
96         updateAAIVfModule.getVfModule(mockExecution)
97         verify(mockExecution).setVariable(prefix + "getVfModuleResponseCode", 500)
98         verify(mockExecution).setVariable(prefix + "getVfModuleResponse", "AAI GET Failed:"+"Error from AAI client")
99     }
100
101
102     @Test
103     void testUpdateVfModule() {
104         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
105         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
106         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
107         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
108         VfModule vfModule = new VfModule()
109         vfModule.setVfModuleId("supercool")
110         vfModule.setResourceVersion("12345")
111         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(vfModule)
112         doNothing().when(client).update(isA(AAIResourceUri.class) as AAIResourceUri, anyObject())
113         updateAAIVfModule.updateVfModule(mockExecution)
114         verify(mockExecution).setVariable("UAAIVfMod_updateVfModuleResponseCode", 200)
115     }
116
117     @Test
118     void testUpdateVfModuleNotFound() throws BpmnError {
119         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
120         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
121         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
122         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
123         VfModule vfModule = new VfModule()
124         vfModule.setVfModuleId("supercool")
125         vfModule.setResourceVersion("12345")
126         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(vfModule)
127         doThrow(new NotFoundException("Vf Module not found")).when(client).update(isA(AAIResourceUri.class) as AAIResourceUri, anyObject())
128         thrown.expect(BpmnError.class)
129         updateAAIVfModule.updateVfModule(mockExecution)
130         verify(mockExecution).setVariable("UAAIVfMod_updateVfModuleResponseCode", 404)
131     }
132
133
134     @Test
135     void testUpdateVfModuleException() {
136         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
137         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
138         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
139         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
140         VfModule vfModule = new VfModule()
141         vfModule.setVfModuleId("supercool")
142         vfModule.setResourceVersion("12345")
143         when(mockExecution.getVariable(prefix + "getVfModuleResponse")).thenReturn(vfModule)
144         doThrow(new IllegalStateException("Error in AAI client")).when(client).update(isA(AAIResourceUri.class) as AAIResourceUri, anyObject())
145         thrown.expect(BpmnError.class)
146         updateAAIVfModule.updateVfModule(mockExecution)
147         verify(mockExecution).setVariable("UAAIVfMod_updateVfModuleResponseCode", 500)
148     }
149 }