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