OperationalEnvironment improvements
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / OperationalEnvironmentTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.aai;
23
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import org.onap.vid.aai.model.RelationshipList;
32 import org.testng.annotations.Test;
33
34
35 public class OperationalEnvironmentTest {
36
37     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
38
39     private static final String OPERATIONAL_ENVIRONMENT_JSON_DASHES = "{\n" +
40         "\"operational-environment-id\": \"testEnvironmentId\",\n" +
41         "\"operational-environment-name\": \"testEnvironmentName\",\n" +
42         "\"operational-environment-type\": \"testEnvironmentType\",\n" +
43         "\"operational-environment-status\": \"testEnvironmentStatus\",\n" +
44         "\"tenant-context\": \"testTenantContext\",\n" +
45         "\"workload-context\": \"testWorkloadContext\",\n" +
46         "\"resource-version\": \"testResourceVersion\",\n" +
47         "\"relationship-list\": {\n" +
48         "\"relationship\": []\n" +
49         "}\n" +
50         "}";
51
52     private static final String OPERATIONAL_ENVIRONMENT_JSON_CAMELCASE = "{\n" +
53         "\"operationalEnvironmentId\": \"testEnvironmentId\",\n" +
54         "\"operationalEnvironmentName\": \"testEnvironmentName\",\n" +
55         "\"operationalEnvironmentType\": \"testEnvironmentType\",\n" +
56         "\"operationalEnvironmentStatus\": \"testEnvironmentStatus\",\n" +
57         "\"tenantContext\": \"testTenantContext\",\n" +
58         "\"workloadContext\": \"testWorkloadContext\",\n" +
59         "\"resourceVersion\": \"testResourceVersion\",\n" +
60         "\"relationshipList\": {\n" +
61         "\"relationship\": []\n" +
62         "}\n" +
63         "}";
64
65     @Test
66     public void shouldProperlyConvertJsonToOperationalEnvironment_whenJsonPropertyNamesContainDashSeparators()
67         throws IOException {
68         assertOperationalEnvironmentDeserialization(OPERATIONAL_ENVIRONMENT_JSON_DASHES);
69     }
70
71     @Test
72     public void shouldProperlyConvertJsonToOperationalEnvironment_whenJsonPropertyNamesAreCamelCase()
73         throws IOException {
74         assertOperationalEnvironmentDeserialization(OPERATIONAL_ENVIRONMENT_JSON_CAMELCASE);
75     }
76
77     private void assertOperationalEnvironmentDeserialization(String operationalEnvironmentTestDashes)
78         throws IOException {
79         OperationalEnvironment operationalEnvironment =
80             OBJECT_MAPPER.readValue(operationalEnvironmentTestDashes, OperationalEnvironment.class);
81
82         assertThat(operationalEnvironment.getOperationalEnvironmentId()).isEqualTo("testEnvironmentId");
83         assertThat(operationalEnvironment.getOperationalEnvironmentName()).isEqualTo("testEnvironmentName");
84         assertThat(operationalEnvironment.getOperationalEnvironmentType()).isEqualTo("testEnvironmentType");
85         assertThat(operationalEnvironment.getOperationalEnvironmentStatus()).isEqualTo("testEnvironmentStatus");
86         assertThat(operationalEnvironment.getTenantContext()).isEqualTo("testTenantContext");
87         assertThat(operationalEnvironment.getWorkloadContext()).isEqualTo("testWorkloadContext");
88         assertThat(operationalEnvironment.getResourceVersion()).isEqualTo("testResourceVersion");
89         assertThat(operationalEnvironment.getRelationshipList().getRelationship()).hasSize(0);
90     }
91
92     @Test
93     public void shouldSerializeToJson_usingActualPropertyNameAsJsonLogicalProperty() throws JsonProcessingException {
94         RelationshipList relationshipList = new RelationshipList();
95         relationshipList.relationship = new ArrayList<>();
96
97         OperationalEnvironment operationalEnvironment = createOperationalEnvironment(relationshipList);
98
99         assertThat(OPERATIONAL_ENVIRONMENT_JSON_CAMELCASE)
100             .isEqualToIgnoringWhitespace(OBJECT_MAPPER.writeValueAsString(operationalEnvironment));
101     }
102
103     private OperationalEnvironment createOperationalEnvironment(RelationshipList relationshipList) {
104         return new OperationalEnvironment.OperationalEnvironmentBuilder()
105             .withOperationalEnvironmentId("testEnvironmentId")
106             .withOperationalEnvironmentName("testEnvironmentName")
107             .withOperationalEnvironmentType("testEnvironmentType")
108             .withOperationalEnvironmentStatus("testEnvironmentStatus")
109             .withTenantContext("testTenantContext")
110             .withWorkloadContext("testWorkloadContext")
111             .withResourceVersion("testResourceVersion")
112             .withRelationshipList(relationshipList)
113             .build();
114     }
115 }