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