Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / CloudOwnerServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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.vid.services;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.onap.vid.aai.AaiClientInterface;
30 import org.onap.vid.exceptions.GenericUncheckedException;
31 import org.onap.vid.mso.model.CloudConfiguration;
32 import org.onap.vid.mso.rest.RequestDetails;
33 import org.onap.vid.properties.Features;
34 import org.testng.annotations.AfterMethod;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.DataProvider;
37 import org.testng.annotations.Test;
38 import org.togglz.core.manager.FeatureManager;
39
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import static org.hamcrest.MatcherAssert.assertThat;
44 import static org.hamcrest.Matchers.hasKey;
45 import static org.hamcrest.Matchers.not;
46 import static org.mockito.Mockito.when;
47 import static org.testng.Assert.assertEquals;
48 import static org.testng.Assert.assertNull;
49
50 public class CloudOwnerServiceTest {
51
52     @Mock
53     private AaiClientInterface aaiClient;
54
55     @Mock
56     private FeatureManager featureManager;
57
58     @InjectMocks
59     private CloudOwnerServiceImpl cloudOwnerService;
60
61     @BeforeClass
62     public void initMocks() {
63         MockitoAnnotations.initMocks(this);
64     }
65
66     @AfterMethod
67     public void resetMocks() {
68         Mockito.reset(aaiClient);
69         Mockito.reset(featureManager);
70     }
71
72     @DataProvider
73     public static Object[][] testEnrichRequestDataProvider() {
74         return new Object[][]{{true}, {false}};
75     }
76
77     @Test(dataProvider = "testEnrichRequestDataProvider")
78     public void whenCloudConfigurationInAdditionalProperties_cloudConfigurationIsEnrichedWithCloudOwner(boolean isFeatureActive) {
79         when(featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)).thenReturn(isFeatureActive);
80         String lcpCloudRegionId = "lcp1";
81         RequestDetails requestDetails = createRequestDetailsWithCloudConfigurationInAdditionalProperties(lcpCloudRegionId);
82         String aaiCloudOwner = "myCloudOwner";
83         when(aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId)).thenReturn(aaiCloudOwner);
84         cloudOwnerService.enrichRequestWithCloudOwner(requestDetails);
85         if (isFeatureActive) {
86             assertEquals(aaiCloudOwner, requestDetails.extractValueByPathUsingAdditionalProperties(
87                     ImmutableList.of("requestDetails", "cloudConfiguration", "cloudOwner"), String.class));
88         }
89         else {
90             Map<String,Object> cloudConfiguration = requestDetails.extractValueByPathUsingAdditionalProperties(
91                     ImmutableList.of("requestDetails", "cloudConfiguration"), Map.class);
92             assertThat(cloudConfiguration, not(hasKey("cloudOwner")));
93         }
94     }
95
96     @Test(dataProvider = "testEnrichRequestDataProvider")
97     public void whenCloudConfigurationInRequestDetailsField_cloudConfigurationIsEnrichedWithCloudOwner(boolean isFeatureActive) {
98         when(featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)).thenReturn(isFeatureActive);
99         String lcpCloudRegionId = "lcp1";
100         RequestDetails requestDetails = createRequestDetailsWithCloudField(lcpCloudRegionId);
101         String aaiCloudOwner = "myCloudOwner";
102         when(aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId)).thenReturn(aaiCloudOwner);
103         cloudOwnerService.enrichRequestWithCloudOwner(requestDetails);
104         if (isFeatureActive) {
105             assertEquals(aaiCloudOwner, requestDetails.getCloudConfiguration().getCloudOwner());
106         }
107         else {
108             assertNull(requestDetails.getCloudConfiguration().getCloudOwner());
109         }
110     }
111
112     private RequestDetails createRequestDetailsWithCloudConfigurationInAdditionalProperties(String lcpCloudRegionId) {
113         RequestDetails requestDetails = new RequestDetails();
114         Map<String, Object> cloudConfiguration = new HashMap<>();
115         cloudConfiguration.put("lcpCloudRegionId", lcpCloudRegionId);
116         requestDetails.setAdditionalProperty("requestDetails",
117                 ImmutableMap.of("cloudConfiguration", cloudConfiguration));
118         return requestDetails;
119     }
120
121     private RequestDetails createRequestDetailsWithCloudField(String lcpCloudRegionId) {
122         CloudConfiguration cloudConfiguration = new CloudConfiguration();
123         cloudConfiguration.setLcpCloudRegionId(lcpCloudRegionId);
124         RequestDetails requestDetails = new RequestDetails();
125         requestDetails.setCloudConfiguration(cloudConfiguration);
126         return requestDetails;
127     }
128
129     @Test(expectedExceptions= GenericUncheckedException.class)
130     public void whenAaiClientThrowException_thenExceptionIsPopulatedByEnrichMethod() {
131         when(featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)).thenReturn(true);
132         String lcpCloudRegionId = "lcp1";
133         RequestDetails requestDetails = createRequestDetailsWithCloudConfigurationInAdditionalProperties(lcpCloudRegionId);
134         when(aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId)).thenThrow(new RuntimeException());
135         cloudOwnerService.enrichRequestWithCloudOwner(requestDetails);
136     }
137
138     @Test
139     public void whenThereIsNoCloudConfiguration_enrichmentMethodNotFailed() {
140         when(featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)).thenReturn(true);
141         RequestDetails requestDetails = new RequestDetails();
142         cloudOwnerService.enrichRequestWithCloudOwner(requestDetails);
143         //if no exception was thrown test success
144     }
145 }