2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.audit;
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.mockito.Mockito.doReturn;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Optional;
33 import java.util.stream.Collectors;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.onap.aai.domain.yang.LInterface;
42 import org.onap.aai.domain.yang.LInterfaces;
43 import org.onap.aai.domain.yang.Vserver;
44 import org.onap.so.openstack.utils.MsoHeatUtils;
45 import org.skyscreamer.jsonassert.JSONAssert;
47 import com.fasterxml.jackson.databind.DeserializationFeature;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49 import com.woorea.openstack.heat.model.Resource;
50 import com.woorea.openstack.heat.model.Resources;
51 import com.woorea.openstack.heat.model.Stack;
54 @RunWith(MockitoJUnitRunner.Silent.class)
55 public class HeatStackAuditTest extends HeatStackAudit {
58 private HeatStackAudit heatStackAudit = new HeatStackAudit();
61 private MsoHeatUtils msoHeatUtilsMock;
64 private AuditVServer auditVserver;
66 private static final String cloudRegion = "cloudRegion";
67 private static final String tenantId = "tenantId";
69 private Resources resources = new Resources();
71 private ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
73 private ObjectMapper stackObjectMapper = new ObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
76 public void setup() throws Exception{
77 resources= objectMapper.readValue(new File("src/test/resources/GetResources.json"), Resources.class);
82 public void extract_proper_path_Test(){
83 Optional<String> actualResult = extractStackPathFromHref("https://orchestration.com:8004/v1/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81");
84 assertEquals("/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81", actualResult.get());
88 public void extract_proper_resources_path_Test(){
89 Optional<String> actualResult = extractResourcePathFromHref("https://orchestration.com:8004/v1/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81");
90 assertEquals("/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81/resources", actualResult.get());
94 public void extract_invalid_uri_Test(){
95 Optional<String> actualResult = extractStackPathFromHref("orchestrn.com:8004/v18b44d60a6f94bdcb2738f9e//stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81");
96 assertEquals(false, actualResult.isPresent());
100 public void createVserverSet_Test() throws Exception{
101 List<Resource> novaResources = resources.getList().stream()
102 .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList());
104 List<Resource> resourceGroups = resources.getList().stream()
105 .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType())).collect(Collectors.toList());
107 Set<Vserver> expectedVservers = new HashSet<>();
108 Vserver vServer1= new Vserver();
109 vServer1.setVserverId("92272b67-d23f-42ca-87fa-7b06a9ec81f3");
110 LInterfaces vServer1Linterfaces = new LInterfaces();
111 vServer1.setLInterfaces(vServer1Linterfaces);
113 LInterface ssc_1_trusted_port_0 = new LInterface();
114 ssc_1_trusted_port_0.setInterfaceId("d2f51f82-0ec2-4581-bd1a-d2a82073e52b");
115 vServer1.getLInterfaces().getLInterface().add(ssc_1_trusted_port_0);
119 LInterface ssc_1_mgmt_port_1 = new LInterface();
120 ssc_1_mgmt_port_1.setInterfaceId("07f5b14c-147a-4d14-8c94-a9e94dbc097b");
121 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_1);
123 LInterface ssc_1_mgmt_port_0 = new LInterface();
124 ssc_1_mgmt_port_0.setInterfaceId("8d93f63e-e972-48c7-ad98-b2122da47315");
125 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_0);
127 LInterface ssc_1_service2_port_0 = new LInterface();
128 ssc_1_service2_port_0.setLInterfaces(new LInterfaces());
129 ssc_1_service2_port_0.setInterfaceId("0594a2f2-7ea4-42eb-abc2-48ea49677fca");
130 vServer1.getLInterfaces().getLInterface().add(ssc_1_service2_port_0);
132 LInterface service2_sub_interface_1 = new LInterface();
133 service2_sub_interface_1.setInterfaceId("2bbfa345-33bb-495a-94b2-fb514ee1cffc");
134 ssc_1_service2_port_0.getLInterfaces().getLInterface().add(service2_sub_interface_1);
136 LInterface ssc_1_int_ha_port_0 = new LInterface();
137 ssc_1_int_ha_port_0.setInterfaceId("00bb8407-650e-48b5-b919-33b88d6f8fe3");
138 vServer1.getLInterfaces().getLInterface().add(ssc_1_int_ha_port_0);
141 LInterface ssc_1_service1_port_0 = new LInterface();
142 ssc_1_service1_port_0.setInterfaceId("27391d94-33af-474a-927d-d409249e8fd3");
143 vServer1.getLInterfaces().getLInterface().add(ssc_1_service1_port_0);
144 ssc_1_service1_port_0.setLInterfaces(new LInterfaces());
146 LInterface service1_sub_interface_0 = new LInterface();
147 service1_sub_interface_0.setInterfaceId("d54dfd09-75c6-4e04-b204-909455b8f933");
148 ssc_1_service1_port_0.getLInterfaces().getLInterface().add(service1_sub_interface_0);
150 LInterface service1_sub_interface_1 = new LInterface();
151 service1_sub_interface_1.setInterfaceId("f7a998c0-8939-4b07-bf4a-0862e9c325e1");
152 ssc_1_service1_port_0.getLInterfaces().getLInterface().add(service1_sub_interface_1);
154 LInterface service1_sub_interface_2 = new LInterface();
155 service1_sub_interface_2.setInterfaceId("621c1fea-60b8-44ee-aede-c01b8b1aaa70");
156 ssc_1_service1_port_0.getLInterfaces().getLInterface().add(service1_sub_interface_2);
159 expectedVservers.add(vServer1);
162 Resources service1QueryResponse = objectMapper.readValue(new File("src/test/resources/Service1ResourceGroupResponse.json"), Resources.class);
163 doReturn(service1QueryResponse).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz/31d0647a-6043-49a4-81b6-ccab29380672/resources", cloudRegion, tenantId, Resources.class);
165 Resources service2QueryResponse =objectMapper.readValue(new File("src/test/resources/Service2ResourceGroupResponse.json"), Resources.class);
166 doReturn(service2QueryResponse).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service2_port_0_subinterfaces-hlzdigtimzst/447a9b41-714e-434b-b1d0-6cce8d9f0f0c/resources", cloudRegion, tenantId, Resources.class);
169 Stack service2StackQuerySubInt = stackObjectMapper.readValue(new File("src/test/resources/Service2SubInterface0.json"), Stack.class);
170 doReturn(service2StackQuerySubInt).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service2_port_0_subinterfaces-hlzdigtimzst-0-upfi5nhurk7y/f711be16-2654-4a09-b89d-0511fda20e81", cloudRegion,tenantId, Stack.class);
171 Resources service2ResourceQuerySubInt = objectMapper.readValue(new File("src/test/resources/Service2SubInterface1Resources.json"), Resources.class);
172 doReturn(service2ResourceQuerySubInt).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service2_port_0_subinterfaces-hlzdigtimzst-0-upfi5nhurk7y/f711be16-2654-4a09-b89d-0511fda20e81/resources", cloudRegion,tenantId, Resources.class);
174 Stack service1StackQuerySubInt1 =stackObjectMapper.readValue(new File("src/test/resources/Service1SubInterface0.json"), Stack.class);
175 doReturn(service1StackQuerySubInt1).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-1-fmn5laetg5cs/0d9cd813-2ae1-46c0-9ebb-48081f6cffbb", cloudRegion,tenantId, Stack.class);
176 Resources service1ResourceQuerySubInt1 = objectMapper.readValue(new File("src/test/resources/Service1SubInterface0Resources.json"), Resources.class);
177 doReturn(service1ResourceQuerySubInt1).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-1-fmn5laetg5cs/0d9cd813-2ae1-46c0-9ebb-48081f6cffbb/resources", cloudRegion,tenantId, Resources.class);
180 Stack service1StackQuerySubInt2 =stackObjectMapper.readValue(new File("src/test/resources/Service1SubInterface1.json"), Stack.class);
181 doReturn(service1StackQuerySubInt2).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-0-yghihziaf36m/b7019dd0-2ee9-4447-bdef-ac25676b205a", cloudRegion,tenantId, Stack.class);
182 Resources service1ResourceQuerySubInt2 = objectMapper.readValue(new File("src/test/resources/Service1SubInterface1Resources.json"), Resources.class);
183 doReturn(service1ResourceQuerySubInt2).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-0-yghihziaf36m/b7019dd0-2ee9-4447-bdef-ac25676b205a/resources", cloudRegion,tenantId, Resources.class);
185 Stack service1StackQuerySubInt3 =stackObjectMapper.readValue(new File("src/test/resources/Service1SubInterface2.json"), Stack.class);
186 doReturn(service1StackQuerySubInt3).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-2-y3ndsavmsymv/bd0fc728-cbde-4301-a581-db56f494675c", cloudRegion,tenantId, Stack.class);
187 Resources service1ResourceQuerySubInt3 = objectMapper.readValue(new File("src/test/resources/Service1SubInterface2Resources.json"), Resources.class);
188 doReturn(service1ResourceQuerySubInt3).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-2-y3ndsavmsymv/bd0fc728-cbde-4301-a581-db56f494675c/resources", cloudRegion,tenantId, Resources.class);
190 Set<Vserver> vServersToAudit = heatStackAudit.createVserverSet(resources, novaResources);
191 Set<Vserver> vserversWithSubInterfaces = heatStackAudit.processSubInterfaces(cloudRegion,tenantId,resourceGroups, vServersToAudit);
193 String actualValue = objectMapper.writeValueAsString(vserversWithSubInterfaces);
194 String expectedValue = objectMapper.writeValueAsString(expectedVservers);
196 JSONAssert.assertEquals(expectedValue, actualValue, false);
200 public void auditHeatStackNoServers_Test() throws Exception{
203 Resources getResource = objectMapper.readValue(new File("src/test/resources/Service1ResourceGroupResponse.json"), Resources.class);
204 doReturn(getResource).when(msoHeatUtilsMock).queryStackResources(cloudRegion, tenantId, "heatStackName");
206 boolean actual = heatStackAudit.auditHeatStackCreate(cloudRegion, "cloudOwner", tenantId, "heatStackName");
207 assertEquals(true, actual);
212 public void findInterfaceInformation_Test(){
213 List<Resource> novaResources = resources.getList().stream()
214 .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList());
215 Set<Vserver> expectedVservers = new HashSet<>();
216 Vserver vServer1= new Vserver();
217 vServer1.setVserverId("92272b67-d23f-42ca-87fa-7b06a9ec81f3");
218 LInterfaces vServer1Linterfaces = new LInterfaces();
219 vServer1.setLInterfaces(vServer1Linterfaces);
221 LInterface ssc_1_trusted_port_0 = new LInterface();
222 ssc_1_trusted_port_0.setInterfaceId("d2f51f82-0ec2-4581-bd1a-d2a82073e52b");
223 vServer1.getLInterfaces().getLInterface().add(ssc_1_trusted_port_0);
225 LInterface ssc_1_service1_port_0 = new LInterface();
226 ssc_1_service1_port_0.setInterfaceId("27391d94-33af-474a-927d-d409249e8fd3");
227 vServer1.getLInterfaces().getLInterface().add(ssc_1_service1_port_0);
229 LInterface ssc_1_mgmt_port_1 = new LInterface();
230 ssc_1_mgmt_port_1.setInterfaceId("07f5b14c-147a-4d14-8c94-a9e94dbc097b");
231 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_1);
233 LInterface ssc_1_mgmt_port_0 = new LInterface();
234 ssc_1_mgmt_port_0.setInterfaceId("8d93f63e-e972-48c7-ad98-b2122da47315");
235 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_0);
237 LInterface ssc_1_service2_port_0 = new LInterface();
238 ssc_1_service2_port_0.setInterfaceId("0594a2f2-7ea4-42eb-abc2-48ea49677fca");
239 vServer1.getLInterfaces().getLInterface().add(ssc_1_service2_port_0);
241 LInterface ssc_1_int_ha_port_0 = new LInterface();
242 ssc_1_int_ha_port_0.setInterfaceId("00bb8407-650e-48b5-b919-33b88d6f8fe3");
243 vServer1.getLInterfaces().getLInterface().add(ssc_1_int_ha_port_0);
245 expectedVservers.add(vServer1);
247 Set<Vserver> actualVservers = heatStackAudit.createVserverSet(resources, novaResources);
249 assertThat(actualVservers, sameBeanAs(expectedVservers));