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