575ceab7ce2a88248ea5d04aa40051f465195070
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / RequestDetailsTest.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.mso.rest;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import org.assertj.core.api.AssertionsForClassTypes;
26 import org.onap.vid.exceptions.NotFoundException;
27 import org.testng.annotations.BeforeMethod;
28 import org.testng.annotations.DataProvider;
29 import org.testng.annotations.Test;
30
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34
35 import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding;
36 import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding;
37 import static org.hamcrest.MatcherAssert.assertThat;
38 import static org.testng.AssertJUnit.assertEquals;
39
40
41 public class RequestDetailsTest {
42
43         private RequestDetails requestDetails;
44
45         private String propertyName = "testProperty";
46         private String additionalProperty = "testAdditionalProperty";
47
48         private static final ImmutableList<String> LCP_CLOUD_REGION_ID_PATH =
49                         ImmutableList.of("requestDetails", "cloudConfiguration", "lcpCloudRegionId");
50
51
52         @BeforeMethod
53         public void setUp() {
54                 requestDetails = new RequestDetails();
55         }
56
57         @Test
58         public void shouldHaveProperSettersAndGetters() {
59                 assertThat(RequestDetails.class, hasValidGettersAndSettersExcluding("additionalProperties"));
60         }
61
62         @Test
63         public void shouldHaveProperGetterAndSetterForAdditionalProperties() {
64                 //      when
65                 requestDetails.setAdditionalProperty(propertyName,additionalProperty);
66
67                 //      then
68                 AssertionsForClassTypes.assertThat( requestDetails.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty);
69         }
70
71         @Test
72         public void shouldProperlyConvertRelatedInstanceObjectToString() {
73                 //      given
74                 requestDetails.setAdditionalProperty(propertyName,additionalProperty);
75
76                 //      when
77                 String response = requestDetails.toString();
78
79                 //      then
80                 AssertionsForClassTypes.assertThat(response).contains(
81                                                 "additionalProperties={"+propertyName+"="+additionalProperty+"}]"
82                 );
83         }
84
85         @Test
86         public void shouldProperlyCheckIfObjectsAreEqual() {
87                 assertThat(RequestDetails.class, hasValidBeanEqualsExcluding("additionalProperties"));
88         }
89
90         @DataProvider
91         public static Object[][] extractValueByPathDataProvider() {
92
93                 RequestDetails requestDetails1 = new RequestDetails();
94                 Map cloudConfiguration = ImmutableMap.of("lcpCloudRegionId", "lcp1");
95                 requestDetails1.setAdditionalProperty("requestDetails",
96                                 ImmutableMap.of("cloudConfiguration", cloudConfiguration));
97
98
99                 return new Object[][] {
100                                 { requestDetails1, LCP_CLOUD_REGION_ID_PATH, String.class, "lcp1" },
101                                 { requestDetails1, ImmutableList.of("requestDetails", "cloudConfiguration"), Map.class, cloudConfiguration },
102
103                 };
104         }
105
106         @Test(dataProvider = "extractValueByPathDataProvider")
107         public void testExtractValueByPath(RequestDetails requestDetails, List<String> keys, Class clz, Object expectedValue) {
108                 assertEquals(expectedValue, requestDetails.extractValueByPathUsingAdditionalProperties(keys, clz));
109         }
110
111         @DataProvider
112         public static Object[][] extractValueByPathDataProviderThrowException() {
113                 RequestDetails requestDetails1 = new RequestDetails();
114                 requestDetails1.setAdditionalProperty("requestDetails",
115                                 ImmutableMap.of("cloudConfiguration", "notMap"));
116
117                 RequestDetails requestDetails2 = new RequestDetails();
118                 requestDetails2.setAdditionalProperty("requestDetails",
119                                 ImmutableMap.of("cloudConfiguration", Collections.EMPTY_MAP));
120
121                 return new Object[][] {
122                                 { new RequestDetails(), LCP_CLOUD_REGION_ID_PATH, String.class},
123                                 { requestDetails1, LCP_CLOUD_REGION_ID_PATH, String.class},
124                                 { requestDetails1, ImmutableList.of("requestDetails", "abc"), String.class},
125                                 { requestDetails2, LCP_CLOUD_REGION_ID_PATH, String.class},
126                 };
127         }
128
129         @Test(dataProvider = "extractValueByPathDataProviderThrowException", expectedExceptions = NotFoundException.class)
130         public void testExtractValueByPathThrowException(RequestDetails requestDetails, List<String> keys, Class clz) {
131                 requestDetails.extractValueByPathUsingAdditionalProperties(keys, clz);
132         }
133 }