Add new endpoint and macro for service upgrade
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / validators / UpgradePreWorkflowValidatorTest.java
1 package org.onap.so.bpmn.infrastructure.workflow.tasks.validators;
2
3 import static org.assertj.core.api.Assertions.assertThat;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.ArgumentMatchers.anyString;
7 import static org.mockito.Mockito.when;
8 import com.fasterxml.jackson.core.JsonProcessingException;
9 import com.fasterxml.jackson.databind.ObjectMapper;
10 import java.util.Arrays;
11 import java.util.List;
12 import java.util.Optional;
13 import java.util.UUID;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.MockitoAnnotations;
19 import org.onap.so.bpmn.common.BBConstants;
20 import org.onap.so.bpmn.common.BuildingBlockExecution;
21 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
22 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
23 import org.onap.so.db.catalog.beans.Service;
24 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
25 import org.onap.so.db.catalog.client.CatalogDbClient;
26 import org.onap.so.serviceinstancebeans.ModelInfo;
27 import org.onap.so.serviceinstancebeans.RequestDetails;
28 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
29
30 public class UpgradePreWorkflowValidatorTest {
31
32     @Mock
33     private CatalogDbClient catalogDbClient;
34
35     private UpgradePreWorkflowValidator validator;
36
37     private ObjectMapper objectMapper;
38
39     @Before
40     public void setup() {
41         MockitoAnnotations.initMocks(this);
42         validator = new UpgradePreWorkflowValidator(catalogDbClient);
43         objectMapper = new ObjectMapper();
44     }
45
46     @Test
47     public void shouldRunFor() {
48         assertTrue(validator.shouldRunFor("upgradeInstance"));
49         assertFalse(validator.shouldRunFor("createInstance"));
50     }
51
52     private BuildingBlockExecution createExecution(ServiceInstancesRequest sir, List<Resource> resourceList)
53             throws JsonProcessingException {
54         BuildingBlockExecution mock = Mockito.mock(BuildingBlockExecution.class);
55         String jsonSir = objectMapper.writer().writeValueAsString(sir);
56         when(mock.getVariable(BBConstants.G_BPMN_REQUEST)).thenReturn(jsonSir);
57         when(mock.getVariable("resources")).thenReturn(resourceList);
58         return mock;
59     }
60
61     @Test
62     public void validateModelInvariantMismatch() throws JsonProcessingException {
63         ServiceInstancesRequest sir = new ServiceInstancesRequest();
64         sir.setRequestDetails(new RequestDetails());
65         sir.getRequestDetails().setModelInfo(new ModelInfo());
66         sir.getRequestDetails().getModelInfo().setModelInvariantId(UUID.randomUUID().toString());
67
68         Resource serviceResource = new Resource(WorkflowType.SERVICE, "", false, null);
69         String aaiModelInvariantId = UUID.randomUUID().toString();
70         serviceResource.setModelInvariantId(aaiModelInvariantId);
71
72         BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource));
73
74         Optional<String> message = validator.validate(execution);
75
76         assertTrue(message.isPresent());
77         assertTrue(message.get().startsWith("Request service modelInvariantId"));
78     }
79
80     @Test
81     public void validateNoVnfsInAAI() throws JsonProcessingException {
82         ServiceInstancesRequest sir = new ServiceInstancesRequest();
83         sir.setRequestDetails(new RequestDetails());
84         sir.getRequestDetails().setModelInfo(new ModelInfo());
85         String modelInvariantId = UUID.randomUUID().toString();
86         sir.getRequestDetails().getModelInfo().setModelInvariantId(modelInvariantId);
87
88         Resource serviceResource = new Resource(WorkflowType.SERVICE, "", false, null);
89         serviceResource.setModelInvariantId(modelInvariantId);
90
91         BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource));
92
93         Optional<String> message = validator.validate(execution);
94
95         assertThat(message).isEmpty();
96     }
97
98     @Test
99     public void validateAAIVnfsNotSupported() throws JsonProcessingException {
100         ServiceInstancesRequest sir = new ServiceInstancesRequest();
101         sir.setRequestDetails(new RequestDetails());
102         sir.getRequestDetails().setModelInfo(new ModelInfo());
103         sir.getRequestDetails().getModelInfo().setModelUuid(UUID.randomUUID().toString());
104         String modelInvariantId = UUID.randomUUID().toString();
105         sir.getRequestDetails().getModelInfo().setModelInvariantId(modelInvariantId);
106
107         Resource serviceResource = new Resource(WorkflowType.SERVICE, "", false, null);
108         serviceResource.setModelInvariantId(modelInvariantId);
109         Resource vnfResource = new Resource(WorkflowType.VNF, "", false, serviceResource);
110         vnfResource.setVnfCustomizationId(UUID.randomUUID().toString());
111
112         Service service = new Service();
113         VnfResourceCustomization vnfCustomization = new VnfResourceCustomization();
114         vnfCustomization.setModelCustomizationUUID(UUID.randomUUID().toString());
115         service.setVnfCustomizations(Arrays.asList(vnfCustomization));
116
117         when(catalogDbClient.getServiceByModelUUID(anyString())).thenReturn(service);
118
119         BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource, vnfResource));
120
121         Optional<String> message = validator.validate(execution);
122
123         assertTrue(message.isPresent());
124         assertTrue(message.get().startsWith("Existing vnfs in AAI are not supported by service model"));
125     }
126
127     @Test
128     public void validateHappyCase() throws JsonProcessingException {
129         ServiceInstancesRequest sir = new ServiceInstancesRequest();
130         sir.setRequestDetails(new RequestDetails());
131         sir.getRequestDetails().setModelInfo(new ModelInfo());
132         sir.getRequestDetails().getModelInfo().setModelUuid(UUID.randomUUID().toString());
133         String modelInvariantId = UUID.randomUUID().toString();
134         sir.getRequestDetails().getModelInfo().setModelInvariantId(modelInvariantId);
135
136         Resource serviceResource = new Resource(WorkflowType.SERVICE, "", false, null);
137         serviceResource.setModelInvariantId(modelInvariantId);
138         Resource vnfResource = new Resource(WorkflowType.VNF, "", false, serviceResource);
139         String vnfCustomiationId = UUID.randomUUID().toString();
140         vnfResource.setVnfCustomizationId(vnfCustomiationId);
141
142         Service service = new Service();
143         VnfResourceCustomization vnfCustomization = new VnfResourceCustomization();
144         vnfCustomization.setModelCustomizationUUID(vnfCustomiationId);
145         service.setVnfCustomizations(Arrays.asList(vnfCustomization));
146
147         when(catalogDbClient.getServiceByModelUUID(anyString())).thenReturn(service);
148
149         BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource, vnfResource));
150
151         Optional<String> message = validator.validate(execution);
152
153         assertFalse(message.isPresent());
154     }
155
156 }