edd214f85f2e98d53b613a1bfb898a4e33f8f93a
[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.bpmn.servicedecomposition.tasks;
22
23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.ArgumentMatchers.isA;
28 import static org.mockito.Mockito.doReturn;
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Optional;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.Spy;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
45 import org.onap.aaiclient.client.aai.AAICommonObjectMapperProvider;
46 import org.onap.aaiclient.client.aai.AAIObjectType;
47 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
48 import org.onap.aaiclient.client.aai.entities.Relationships;
49 import com.fasterxml.jackson.core.JsonParseException;
50 import com.fasterxml.jackson.core.JsonProcessingException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class CloudInfoFromAAITest {
56
57     private static final String RESOURCE_PATH = "src/test/resources/__files/ExecuteBuildingBlock/";
58
59     @Spy
60     private CloudInfoFromAAI SPY_CloudInfoFromAAI = new CloudInfoFromAAI();
61
62     protected ObjectMapper mapper = new ObjectMapper();
63
64     @Mock
65     private BBInputSetupUtils SPY_bbInputSetupUtils;
66
67     @Before
68     public void setup() {
69         SPY_CloudInfoFromAAI.setBbInputSetupUtils(SPY_bbInputSetupUtils);
70     }
71
72     @Test
73     public void testGetCloudInfoFromAAI() throws JsonParseException, JsonMappingException, IOException {
74         // Test vnfs
75         ServiceInstance serviceInstance =
76                 mapper.readValue(new File(RESOURCE_PATH + "ServiceInstance_getServiceInstanceNOAAIExpected.json"),
77                         ServiceInstance.class);
78         CloudRegion expected = new CloudRegion();
79         GenericVnf vnf = new GenericVnf();
80         String vnfId = "vnfId";
81         vnf.setVnfId(vnfId);
82         serviceInstance.getVnfs().add(vnf);
83         org.onap.aai.domain.yang.GenericVnf aaiVnf = new org.onap.aai.domain.yang.GenericVnf();
84         aaiVnf.setVnfId(vnfId);
85         Relationships relationships = Mockito.mock(Relationships.class);
86         Optional<Relationships> relationshipsOp = Optional.of(relationships);
87         doReturn(aaiVnf).when(SPY_bbInputSetupUtils).getAAIGenericVnf(vnf.getVnfId());
88         doReturn(relationshipsOp).when(SPY_CloudInfoFromAAI).getRelationshipsFromWrapper(isA(AAIResultWrapper.class));
89         doReturn(Optional.of(expected)).when(SPY_CloudInfoFromAAI).getRelatedCloudRegionAndTenant(relationships);
90         Optional<CloudRegion> actual = SPY_CloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
91         assertThat(actual.get(), sameBeanAs(expected));
92
93         // Test networks
94         serviceInstance =
95                 mapper.readValue(new File(RESOURCE_PATH + "ServiceInstance_getServiceInstanceNOAAIExpected.json"),
96                         ServiceInstance.class);
97         L3Network l3Network = new L3Network();
98         String networkId = "networkId";
99         l3Network.setNetworkId(networkId);
100         serviceInstance.getNetworks().add(l3Network);
101         org.onap.aai.domain.yang.L3Network aaiL3Network = new org.onap.aai.domain.yang.L3Network();
102         aaiL3Network.setNetworkId(networkId);
103         doReturn(aaiL3Network).when(SPY_bbInputSetupUtils).getAAIL3Network(l3Network.getNetworkId());
104         actual = SPY_CloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
105         assertThat(actual.get(), sameBeanAs(expected));
106
107         // Test no relationships
108
109         doReturn(Optional.empty()).when(SPY_CloudInfoFromAAI).getRelationshipsFromWrapper(isA(AAIResultWrapper.class));
110         actual = SPY_CloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
111         assertEquals(actual, Optional.empty());
112
113         // Test null
114         serviceInstance =
115                 mapper.readValue(new File(RESOURCE_PATH + "ServiceInstance_getServiceInstanceNOAAIExpected.json"),
116                         ServiceInstance.class);
117         actual = SPY_CloudInfoFromAAI.getCloudInfoFromAAI(serviceInstance);
118         assertEquals(actual, Optional.empty());
119     }
120
121     @Test
122     public void testGetRelatedCloudRegionAndTenant() throws JsonProcessingException {
123         String cloudOwner = "cloudOwner";
124         String cloudRegionId = "cloudRegionId";
125         String cloudRegionVersion = "cloudRegionVersion";
126         String cloudRegionComplexName = "cloudRegionComplexName";
127         String tenantId = "tenantId";
128         CloudRegion expected = new CloudRegion();
129         expected.setCloudOwner(cloudOwner);
130         expected.setCloudRegionVersion(cloudRegionVersion);
131         expected.setComplex(cloudRegionComplexName);
132         expected.setLcpCloudRegionId(cloudRegionId);
133         expected.setTenantId(tenantId);
134
135         Relationships relationships = Mockito.mock(Relationships.class);
136         List<AAIResultWrapper> cloudRegions = new ArrayList<>();
137         org.onap.aai.domain.yang.CloudRegion cloudRegion = new org.onap.aai.domain.yang.CloudRegion();
138         cloudRegion.setCloudOwner(cloudOwner);
139         cloudRegion.setCloudRegionId(cloudRegionId);
140         cloudRegion.setCloudRegionVersion(cloudRegionVersion);
141         cloudRegion.setComplexName(cloudRegionComplexName);
142         AAIResultWrapper cloudRegionWrapper =
143                 new AAIResultWrapper(new AAICommonObjectMapperProvider().getMapper().writeValueAsString(cloudRegion));
144         cloudRegions.add(cloudRegionWrapper);
145
146         doReturn(cloudRegions).when(relationships).getByType(AAIObjectType.CLOUD_REGION);
147         List<AAIResultWrapper> tenants = new ArrayList<>();
148         org.onap.aai.domain.yang.Tenant tenant = new org.onap.aai.domain.yang.Tenant();
149         tenant.setTenantId(tenantId);
150         AAIResultWrapper tenantWrapper =
151                 new AAIResultWrapper(new AAICommonObjectMapperProvider().getMapper().writeValueAsString(tenant));
152         tenants.add(tenantWrapper);
153         doReturn(tenants).when(relationships).getByType(AAIObjectType.TENANT);
154
155         Optional<CloudRegion> actual = SPY_CloudInfoFromAAI.getRelatedCloudRegionAndTenant(relationships);
156
157         assertThat(actual.get(), sameBeanAs(expected));
158     }
159 }