e822f650720e679cb7fc44c0a38bb6eb12e1eb94
[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.tasks.inventory;
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.adapters.tasks.inventory.CreateAAIInventory;
42 import org.onap.so.audit.beans.AuditInventory;
43 import org.onap.so.client.aai.AAIObjectType;
44 import org.onap.so.client.aai.AAIResourcesClient;
45 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
46 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
47 import org.onap.so.objects.audit.AAIObjectAuditList;
48 import com.fasterxml.jackson.core.JsonParseException;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51
52 public class CreateAAIInventoryTest extends CreateAAIInventory {
53
54     @InjectMocks
55     private CreateAAIInventory createAAIInventory = new CreateAAIInventory();
56
57     @Mock
58     private ExternalTask mockExternalTask;
59
60     @Mock
61     private AAIResourcesClient mockClient;
62
63     private ObjectMapper objectMapper = new ObjectMapper();
64
65     private AuditInventory auditInventory = new AuditInventory();
66
67     AAIObjectAuditList auditListSuccess;
68
69     AAIObjectAuditList auditListFailure;
70
71     AAIObjectAuditList missingSubInterfaces;
72
73     @Before
74     public void setup() throws JsonParseException, JsonMappingException, IOException {
75         auditInventory.setCloudOwner("cloudOwner");
76         auditInventory.setCloudRegion("cloudRegion");
77         auditInventory.setTenantId("tenantId");
78         auditInventory.setHeatStackName("stackName");
79         MockitoAnnotations.initMocks(this);
80         auditListSuccess = objectMapper.readValue(new File("src/test/resources/ExpectedVServerFound.json"),
81                 AAIObjectAuditList.class);
82         auditListFailure = objectMapper.readValue(new File("src/test/resources/Vserver2_Found_VServer1_Not_Found.json"),
83                 AAIObjectAuditList.class);
84         missingSubInterfaces = objectMapper.readValue(new File("src/test/resources/AuditResultsMissSub.json"),
85                 AAIObjectAuditList.class);
86         doReturn(auditInventory).when(mockExternalTask).getVariable("auditInventory");
87     }
88
89     @Test
90     public void determineAuditResult_Test() throws Exception {
91         boolean actual = createAAIInventory.didAuditFailVserverLInterfaces(auditListSuccess);
92         assertEquals(false, actual);
93     }
94
95     @Test
96     public void determineAuditResult_Failure_Test() throws Exception {
97         boolean actual = createAAIInventory.didAuditFailVserverLInterfaces(auditListFailure);
98         assertEquals(true, actual);
99     }
100
101     @Test
102     public void missing_Sub_Interfaces_Test() throws Exception {
103         AAIResourceUri aaiURI2 = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, "cloudOwner",
104                 "regionOne", "0422ffb57ba042c0800a29dc85ca70f8", "92272b67-d23f-42ca-87fa-7b06a9ec81f3",
105                 "tsbc0005v_tsbc0005vm002_svc1_port_0", "tsbc0005v_tsbc0005vm002_subint_untrusted_svc1_81");
106         AAIResourceUri aaiURI1 = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, "cloudOwner",
107                 "regionOne", "0422ffb57ba042c0800a29dc85ca70f8", "92272b67-d23f-42ca-87fa-7b06a9ec81f3",
108                 "tsbc0005v_tsbc0005vm002_svc2_port_0", "tsbc0005v_tsbc0005vm002_subint_untrusted_svc2_103");
109         ArgumentCaptor<Optional> captor = ArgumentCaptor.forClass(Optional.class);
110         ArgumentCaptor<AAIResourceUri> uriCaptor = ArgumentCaptor.forClass(AAIResourceUri.class);
111
112         createAAIInventory.setAaiClient(mockClient);
113         createAAIInventory.createInventory(missingSubInterfaces);
114         Mockito.verify(mockClient, times(2)).createIfNotExists(uriCaptor.capture(), captor.capture());
115
116         List<AAIResourceUri> capturedURI = uriCaptor.getAllValues();
117         assertTrue(capturedURI.stream().anyMatch(item -> aaiURI1.build().toString().equals(item.build().toString())));
118         assertTrue(capturedURI.stream().anyMatch(item -> aaiURI2.build().toString().equals(item.build().toString())));
119
120
121
122     }
123 }