1 package org.onap.so.bpmn.infrastructure.workflow.tasks.validators;
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;
30 public class UpgradePreWorkflowValidatorTest {
33 private CatalogDbClient catalogDbClient;
35 private UpgradePreWorkflowValidator validator;
37 private ObjectMapper objectMapper;
41 MockitoAnnotations.initMocks(this);
42 validator = new UpgradePreWorkflowValidator(catalogDbClient);
43 objectMapper = new ObjectMapper();
47 public void shouldRunFor() {
48 assertTrue(validator.shouldRunFor("upgradeInstance"));
49 assertFalse(validator.shouldRunFor("createInstance"));
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);
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());
68 Resource serviceResource = new Resource(WorkflowType.SERVICE, "", false, null);
69 String aaiModelInvariantId = UUID.randomUUID().toString();
70 serviceResource.setModelInvariantId(aaiModelInvariantId);
72 BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource));
74 Optional<String> message = validator.validate(execution);
76 assertTrue(message.isPresent());
77 assertTrue(message.get().startsWith("Request service modelInvariantId"));
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);
88 Resource serviceResource = new Resource(WorkflowType.SERVICE, "", false, null);
89 serviceResource.setModelInvariantId(modelInvariantId);
91 BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource));
93 Optional<String> message = validator.validate(execution);
95 assertThat(message).isEmpty();
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);
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());
112 Service service = new Service();
113 VnfResourceCustomization vnfCustomization = new VnfResourceCustomization();
114 vnfCustomization.setModelCustomizationUUID(UUID.randomUUID().toString());
115 service.setVnfCustomizations(Arrays.asList(vnfCustomization));
117 when(catalogDbClient.getServiceByModelUUID(anyString())).thenReturn(service);
119 BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource, vnfResource));
121 Optional<String> message = validator.validate(execution);
123 assertTrue(message.isPresent());
124 assertTrue(message.get().startsWith("Existing vnfs in AAI are not supported by service model"));
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);
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);
142 Service service = new Service();
143 VnfResourceCustomization vnfCustomization = new VnfResourceCustomization();
144 vnfCustomization.setModelCustomizationUUID(vnfCustomiationId);
145 service.setVnfCustomizations(Arrays.asList(vnfCustomization));
147 when(catalogDbClient.getServiceByModelUUID(anyString())).thenReturn(service);
149 BuildingBlockExecution execution = createExecution(sir, Arrays.asList(serviceResource, vnfResource));
151 Optional<String> message = validator.validate(execution);
153 assertFalse(message.isPresent());