8226f4fed85087943f83464446039f2221f208d0
[so.git] /
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 import java.io.File;
28 import java.io.IOException;
29 import java.util.List;
30 import java.util.Map;
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;
50
51 public class CreateAAIInventoryTest extends CreateAAIInventory {
52
53     @InjectMocks
54     private CreateAAIInventory createAAIInventory = new CreateAAIInventory();
55
56     @Mock
57     private ExternalTask mockExternalTask;
58
59     @Mock
60     private AAIResourcesClient mockClient;
61
62     private ObjectMapper objectMapper = new ObjectMapper();
63
64     private AuditInventory auditInventory = new AuditInventory();
65
66     AAIObjectAuditList auditListSuccess;
67
68     AAIObjectAuditList auditListFailure;
69
70     AAIObjectAuditList missingSubInterfaces;
71
72     @Before
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");
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, "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);
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 }