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