Merge "fix major sonar bug"
[so.git] / adapters / mso-openstack-adapters / src / test / java / org / onap / so / adapters / audit / HeatStackAuditTest.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.adapters.audit;
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.mockito.Mockito.doReturn;
27
28 import java.io.File;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34
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;
46
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;
52
53
54 @RunWith(MockitoJUnitRunner.Silent.class)
55 public class HeatStackAuditTest extends HeatStackAudit {
56
57         @InjectMocks
58         private HeatStackAudit heatStackAudit = new HeatStackAudit();
59
60         @Mock
61         private MsoHeatUtils msoHeatUtilsMock;
62         
63         @Mock
64         private AuditVServer auditVserver;
65
66         private static final String cloudRegion = "cloudRegion";
67         private static final String tenantId = "tenantId";
68         
69         private Resources resources = new Resources();
70         
71         private ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
72         
73         private ObjectMapper stackObjectMapper = new ObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
74
75         @Before
76         public void setup() throws Exception{           
77                 resources= objectMapper.readValue(new File("src/test/resources/GetResources.json"), Resources.class);
78                 
79         } 
80         
81         @Test
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());
85         }
86         
87         @Test
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());
91         }
92         
93         @Test
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());
97         }
98
99         @Test
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());
103                 
104                 List<Resource> resourceGroups = resources.getList().stream()
105                                 .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType())).collect(Collectors.toList());
106                 
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);
112                 
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);
116                 
117
118                 
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);
122                 
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);
126                 
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);
131                 
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);
135                 
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);             
139                 
140                 
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());                
145                 
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);
149                 
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);
153                 
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);
157
158                 
159                 expectedVservers.add(vServer1);
160                 
161                 
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);
164                 
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);
167                 
168                 
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);
173                 
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);
178
179         
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);
184
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);
189         
190                 Set<Vserver> vServersToAudit = heatStackAudit.createVserverSet(resources, novaResources);
191                 Set<Vserver> vserversWithSubInterfaces = heatStackAudit.processSubInterfaces(cloudRegion,tenantId,resourceGroups, vServersToAudit);
192                 
193                 String actualValue = objectMapper.writeValueAsString(vserversWithSubInterfaces);
194                 String expectedValue = objectMapper.writeValueAsString(expectedVservers);
195                 
196                 JSONAssert.assertEquals(expectedValue, actualValue, false);
197         }
198         
199
200         @Test
201         public void findInterfaceInformation_Test(){
202                 List<Resource> novaResources = resources.getList().stream()
203                                 .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList());
204                 Set<Vserver> expectedVservers = new HashSet<>();
205                 Vserver vServer1= new Vserver();
206                 vServer1.setVserverId("92272b67-d23f-42ca-87fa-7b06a9ec81f3");
207                 LInterfaces vServer1Linterfaces = new LInterfaces();
208                 vServer1.setLInterfaces(vServer1Linterfaces);
209                 
210                 LInterface ssc_1_trusted_port_0 = new LInterface();
211                 ssc_1_trusted_port_0.setInterfaceId("d2f51f82-0ec2-4581-bd1a-d2a82073e52b");
212                 vServer1.getLInterfaces().getLInterface().add(ssc_1_trusted_port_0);
213                 
214                 LInterface ssc_1_service1_port_0 = new LInterface();
215                 ssc_1_service1_port_0.setInterfaceId("27391d94-33af-474a-927d-d409249e8fd3");
216                 vServer1.getLInterfaces().getLInterface().add(ssc_1_service1_port_0);
217                 
218                 LInterface ssc_1_mgmt_port_1 = new LInterface();
219                 ssc_1_mgmt_port_1.setInterfaceId("07f5b14c-147a-4d14-8c94-a9e94dbc097b");
220                 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_1);
221                 
222                 LInterface ssc_1_mgmt_port_0 = new LInterface();
223                 ssc_1_mgmt_port_0.setInterfaceId("8d93f63e-e972-48c7-ad98-b2122da47315");
224                 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_0);
225                 
226                 LInterface ssc_1_service2_port_0 = new LInterface();
227                 ssc_1_service2_port_0.setInterfaceId("0594a2f2-7ea4-42eb-abc2-48ea49677fca");
228                 vServer1.getLInterfaces().getLInterface().add(ssc_1_service2_port_0);
229                 
230                 LInterface ssc_1_int_ha_port_0 = new LInterface();
231                 ssc_1_int_ha_port_0.setInterfaceId("00bb8407-650e-48b5-b919-33b88d6f8fe3");
232                 vServer1.getLInterfaces().getLInterface().add(ssc_1_int_ha_port_0);             
233                 
234                 expectedVservers.add(vServer1);
235         
236                 Set<Vserver> actualVservers = heatStackAudit.createVserverSet(resources, novaResources);
237                 
238                 assertThat(actualVservers, sameBeanAs(expectedVservers));
239         }
240
241
242 }