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;
29 import java.io.IOException;
30 import java.util.List;
32 import java.util.Optional;
34 import org.camunda.bpm.client.task.ExternalTask;
35 import org.hamcrest.Matchers;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.so.adapters.audit.AAIObjectAuditList;
44 import org.onap.so.audit.beans.AuditInventory;
45 import org.onap.so.client.aai.AAIObjectType;
46 import org.onap.so.client.aai.AAIResourcesClient;
47 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
48 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
50 import com.fasterxml.jackson.core.JsonParseException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
54 public class CreateAAIInventoryTest extends CreateAAIInventory {
57 private CreateAAIInventory createAAIInventory = new CreateAAIInventory();
60 private ExternalTask mockExternalTask;
63 private AAIResourcesClient mockClient;
65 private ObjectMapper objectMapper = new ObjectMapper();
67 private AuditInventory auditInventory = new AuditInventory();
69 AAIObjectAuditList auditListSuccess;
71 AAIObjectAuditList auditListFailure;
73 AAIObjectAuditList missingSubInterfaces;
76 public void setup() throws JsonParseException, JsonMappingException, IOException {
77 auditInventory.setCloudOwner("cloudOwner");
78 auditInventory.setCloudRegion("cloudRegion");
79 auditInventory.setTenantId("tenantId");
80 auditInventory.setHeatStackName("stackName");
81 MockitoAnnotations.initMocks(this);
82 auditListSuccess = objectMapper.readValue(new File("src/test/resources/ExpectedVServerFound.json"), AAIObjectAuditList.class);
83 auditListFailure = objectMapper.readValue(new File("src/test/resources/Vserver2_Found_VServer1_Not_Found.json"), AAIObjectAuditList.class);
84 missingSubInterfaces = objectMapper.readValue(new File("src/test/resources/AuditResultsMissSub.json"), 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,
103 "cloudOwner", "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,
106 "cloudOwner", "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())));