if audit fails write sub interface data to a ai
[so.git] / adapters / mso-openstack-adapters / src / test / java / org / onap / so / adapters / inventory / create / CreateAAIInventoryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.adapters.inventory.create;
22
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;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Optional;
33
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;
49
50 import com.fasterxml.jackson.core.JsonParseException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 public class CreateAAIInventoryTest extends CreateAAIInventory {
55
56         @InjectMocks
57         private CreateAAIInventory createAAIInventory = new CreateAAIInventory();
58
59         @Mock
60         private ExternalTask mockExternalTask;
61         
62         @Mock
63         private AAIResourcesClient mockClient;
64
65         private ObjectMapper objectMapper = new ObjectMapper();
66         
67         private AuditInventory auditInventory = new AuditInventory();
68
69         AAIObjectAuditList auditListSuccess;
70         
71         AAIObjectAuditList auditListFailure;
72         
73         AAIObjectAuditList missingSubInterfaces;
74         
75         @Before
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");
86         }
87
88         @Test
89         public void determineAuditResult_Test() throws Exception{
90                 boolean actual = createAAIInventory.didAuditFailVserverLInterfaces(auditListSuccess);
91                 assertEquals(false, actual);
92         }
93         
94         @Test
95         public void determineAuditResult_Failure_Test() throws Exception{
96                 boolean actual = createAAIInventory.didAuditFailVserverLInterfaces(auditListFailure);
97                 assertEquals(true, actual);
98         }
99         
100         @Test
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);
110
111                 createAAIInventory.setAaiClient(mockClient);
112                 createAAIInventory.createInventory(missingSubInterfaces);               
113                 Mockito.verify(mockClient,times(2)).createIfNotExists(uriCaptor.capture(),captor.capture());
114                 
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())));
118
119
120                 
121         }
122 }