b7ae76915f9e27b7b88312a847a1dc65d63c4067
[vid.git] / vid-app-common / src / test / java / org / onap / vid / model / serviceInstantiation / InstantiationModelSerializationTest.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.model.serviceInstantiation;
22
23 import static java.util.Collections.emptyMap;
24 import static net.javacrumbs.jsonunit.JsonMatchers.jsonNodeAbsent;
25 import static net.javacrumbs.jsonunit.JsonMatchers.jsonPartEquals;
26 import static org.hamcrest.CoreMatchers.either;
27 import static org.hamcrest.CoreMatchers.equalTo;
28 import static org.hamcrest.CoreMatchers.not;
29 import static org.hamcrest.MatcherAssert.assertThat;
30 import static org.hamcrest.Matchers.hasItem;
31 import static org.hamcrest.Matchers.hasProperty;
32 import static org.hamcrest.Matchers.nullValue;
33 import static org.hamcrest.Matchers.samePropertyValuesAs;
34 import static org.onap.vid.model.Action.Create;
35 import static org.onap.vid.testUtils.TestUtils.setStringsInStringProperties;
36 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
37
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.ImmutableMap;
40 import java.lang.reflect.InvocationTargetException;
41 import java.util.List;
42 import java.util.Map;
43 import org.apache.commons.beanutils.PropertyUtils;
44 import org.onap.vid.model.VidNotions;
45 import org.onap.vid.model.VidNotions.InstantiationType;
46 import org.onap.vid.model.VidNotions.InstantiationUI;
47 import org.onap.vid.model.VidNotions.ModelCategory;
48 import org.onap.vid.mso.model.ModelInfo;
49 import org.onap.vid.mso.model.ServiceInstantiationRequestDetails.UserParamNameAndValue;
50 import org.testng.annotations.Test;
51
52 public class InstantiationModelSerializationTest {
53
54     final ImmutableList<Map<String, String>> instanceParams =
55         ImmutableList.of(
56             ImmutableMap.of("one", "1"),
57             ImmutableMap.of("two", "2")
58         );
59
60     @Test
61     public void serializeAndDeserializeServiceInstantiation() throws Exception {
62
63         ServiceInstantiation serviceInstantiation = new ServiceInstantiation(
64             newModelInfo(),
65             "owningEntityId",
66             "owningEntityName",
67             "projectName",
68             "globalSubscriberId",
69             "subscriberName",
70             "productFamilyId",
71             "instanceName",
72             "subscriptionServiceType",
73             "lcpCloudRegionId",
74             "legacyRegion",
75             "tenantId",
76             "tenantName",
77             "aicZoneId",
78             "aicZoneName",
79             emptyMap(),
80             emptyMap(),
81             emptyMap(),
82             emptyMap(),
83             instanceParams,
84             true,
85             1,
86             true,
87             true,
88             "testApi",
89             "instanceId",
90             "Delete",
91             "trackById",
92             true,
93             "statusMessage",
94             new VidNotions(InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED,
95                 ModelCategory.INFRASTRUCTURE_VPN,
96                 InstantiationUI.INFRASTRUCTURE_VPN,
97                 InstantiationType.Macro)
98         );
99
100         verifySerializationAndDeserialization(serviceInstantiation);
101     }
102
103     @Test
104     public void serializeAndDeserializeVnf() throws Exception {
105
106         Vnf vnf = new Vnf(
107             newModelInfo(), "productFamilyId",
108             "instanceName",
109             "Upgrade",
110             "platformName",
111             "lcpCloudRegionId",
112             "legacyRegion",
113             "tenantId",
114             instanceParams,
115             "lineOfBusinessName",
116             true,
117             "instanceId",
118             emptyMap(),
119             "trackById",
120             true,
121             "statusMessage",
122             5);
123
124         verifySerializationAndDeserialization(vnf);
125     }
126
127     @Test
128     public void serializeAndDeserializeVfModule() throws Exception {
129
130         List<UserParamNameAndValue> supplementaryParams = ImmutableList.of(
131             new UserParamNameAndValue("uno", "1"),
132             new UserParamNameAndValue("dos", "2"),
133             new UserParamNameAndValue("tres", "3")
134         );
135
136         VfModule vfModule = new VfModule(
137             newModelInfo(),
138             "instanceName",
139             "volumeGroupInstanceName",
140             "Delete",
141             "lcpCloudRegionId",
142             "legacyRegion",
143             "tenantId",
144             instanceParams,
145             supplementaryParams,
146             true,
147             true,
148             "instanceId",
149             "trackById",
150             true,
151             "statusMessage",
152             true,
153             true,
154             1);
155
156         verifySerializationAndDeserialization(vfModule);
157     }
158
159     @Test
160     public void VfModule_sdncPreLoad_shouldBeSerializedWithCorrectName() {
161
162         final boolean USE_PRELOAD = true;
163
164         VfModule vfModule = new VfModule(newModelInfo(), null, null, null,
165             null, null, null, null, null, false,
166             /* HERE ====> */ USE_PRELOAD,
167             null, null, null, null, null, null, null);
168
169         assertThat(vfModule, jsonPartEquals("sdncPreLoad", USE_PRELOAD));
170         assertThat(vfModule, jsonNodeAbsent("usePreload"));
171     }
172
173     @Test
174     public void VfModule_volumeGroupName_shouldBeSerializedWithCorrectName() {
175
176         final String VOLUME_GROUP_INSTANCE_NAME = "my volume group name";
177
178         VfModule vfModule = new VfModule(newModelInfo(), null,
179             /* HERE ====> */ VOLUME_GROUP_INSTANCE_NAME,
180             null, null, null, null, null, null,
181             false, null, null, null, null, null,
182             null, null, null);
183
184         assertThat(vfModule, jsonPartEquals("volumeGroupName", VOLUME_GROUP_INSTANCE_NAME));
185         assertThat(vfModule, jsonNodeAbsent("volumeGroupInstanceName"));
186     }
187
188     private ModelInfo newModelInfo() {
189         ModelInfo modelInfo = new ModelInfo();
190         setStringsInStringProperties(modelInfo);
191         return modelInfo;
192     }
193
194     private void verifySerializationAndDeserialization(Object object) throws Exception {
195
196         assertThatAllValuesAreNotDefaultValues(object);
197
198         String valueAsString = JACKSON_OBJECT_MAPPER.writeValueAsString(object);
199         Object objectReconstructed = JACKSON_OBJECT_MAPPER.readValue(valueAsString, object.getClass());
200
201         // verify that all fields' values were reconstructed
202         assertThat(objectReconstructed, samePropertyValuesAs(object));
203     }
204
205     private void assertThatAllValuesAreNotDefaultValues(Object object)
206         throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
207         assertThat("setup is expected to have no field with a default Java value",
208             PropertyUtils.describe(object).entrySet(),
209             not(hasItem(hasProperty("value",
210                 either(nullValue())
211                     .or(equalTo(0))
212                     .or(equalTo(""))
213                     .or(equalTo(false))
214                     .or(equalTo(Create))))));
215     }
216
217 }