2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.inventory.create;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.times;
28 import java.io.IOException;
29 import java.util.List;
31 import java.util.Optional;
32 import org.camunda.bpm.client.task.ExternalTask;
33 import org.hamcrest.Matchers;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.so.audit.beans.AuditInventory;
42 import org.onap.so.client.aai.AAIObjectType;
43 import org.onap.so.client.aai.AAIResourcesClient;
44 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
45 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
46 import org.onap.so.objects.audit.AAIObjectAuditList;
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.databind.JsonMappingException;
49 import com.fasterxml.jackson.databind.ObjectMapper;
51 public class CreateAAIInventoryTest extends CreateAAIInventory {
54 private CreateAAIInventory createAAIInventory = new CreateAAIInventory();
57 private ExternalTask mockExternalTask;
60 private AAIResourcesClient mockClient;
62 private ObjectMapper objectMapper = new ObjectMapper();
64 private AuditInventory auditInventory = new AuditInventory();
66 AAIObjectAuditList auditListSuccess;
68 AAIObjectAuditList auditListFailure;
70 AAIObjectAuditList missingSubInterfaces;
73 public void setup() throws JsonParseException, JsonMappingException, IOException {
74 auditInventory.setCloudOwner("cloudOwner");
75 auditInventory.setCloudRegion("cloudRegion");
76 auditInventory.setTenantId("tenantId");
77 auditInventory.setHeatStackName("stackName");
78 MockitoAnnotations.initMocks(this);
79 auditListSuccess = objectMapper.readValue(new File("src/test/resources/ExpectedVServerFound.json"),
80 AAIObjectAuditList.class);
81 auditListFailure = objectMapper.readValue(new File("src/test/resources/Vserver2_Found_VServer1_Not_Found.json"),
82 AAIObjectAuditList.class);
83 missingSubInterfaces = objectMapper.readValue(new File("src/test/resources/AuditResultsMissSub.json"),
84 AAIObjectAuditList.class);
85 doReturn(auditInventory).when(mockExternalTask).getVariable("auditInventory");
89 public void determineAuditResult_Test() throws Exception {
90 boolean actual = createAAIInventory.didAuditFailVserverLInterfaces(auditListSuccess);
91 assertEquals(false, actual);
95 public void determineAuditResult_Failure_Test() throws Exception {
96 boolean actual = createAAIInventory.didAuditFailVserverLInterfaces(auditListFailure);
97 assertEquals(true, actual);
101 public void missing_Sub_Interfaces_Test() throws Exception {
102 AAIResourceUri aaiURI2 = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, "cloudOwner",
103 "regionOne", "0422ffb57ba042c0800a29dc85ca70f8", "92272b67-d23f-42ca-87fa-7b06a9ec81f3",
104 "tsbc0005v_tsbc0005vm002_svc1_port_0", "tsbc0005v_tsbc0005vm002_subint_untrusted_svc1_81");
105 AAIResourceUri aaiURI1 = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, "cloudOwner",
106 "regionOne", "0422ffb57ba042c0800a29dc85ca70f8", "92272b67-d23f-42ca-87fa-7b06a9ec81f3",
107 "tsbc0005v_tsbc0005vm002_svc2_port_0", "tsbc0005v_tsbc0005vm002_subint_untrusted_svc2_103");
108 ArgumentCaptor<Optional> captor = ArgumentCaptor.forClass(Optional.class);
109 ArgumentCaptor<AAIResourceUri> uriCaptor = ArgumentCaptor.forClass(AAIResourceUri.class);
111 createAAIInventory.setAaiClient(mockClient);
112 createAAIInventory.createInventory(missingSubInterfaces);
113 Mockito.verify(mockClient, times(2)).createIfNotExists(uriCaptor.capture(), captor.capture());
115 List<AAIResourceUri> capturedURI = uriCaptor.getAllValues();
116 assertTrue(capturedURI.stream().anyMatch(item -> aaiURI1.build().toString().equals(item.build().toString())));
117 assertTrue(capturedURI.stream().anyMatch(item -> aaiURI2.build().toString().equals(item.build().toString())));