Fix for Penetration test _ Session and cookie management
[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             emptyMap(),
85             instanceParams,
86             true,
87             1,
88             true,
89             true,
90             "testApi",
91             "instanceId",
92             "Delete",
93             "trackById",
94             true,
95             "statusMessage",
96             new VidNotions(InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED,
97                 ModelCategory.INFRASTRUCTURE_VPN,
98                 InstantiationUI.INFRASTRUCTURE_VPN,
99                 InstantiationType.Macro),
100             "originalName"
101         );
102
103         verifySerializationAndDeserialization(serviceInstantiation);
104     }
105
106     @Test
107     public void serializeAndDeserializeVnf() throws Exception {
108
109         Vnf vnf = new Vnf(
110             newModelInfo(), "productFamilyId",
111             "instanceName",
112             "Upgrade",
113             "platformName",
114             "lcpCloudRegionId",
115             "legacyRegion",
116             "tenantId",
117             instanceParams,
118             "lineOfBusinessName",
119             true,
120             "instanceId",
121             emptyMap(),
122             "trackById",
123             true,
124             "statusMessage",
125             5,
126             "originalName");
127
128         verifySerializationAndDeserialization(vnf);
129     }
130
131     @Test
132     public void serializeAndDeserializeVfModule() throws Exception {
133
134         List<UserParamNameAndValue> supplementaryParams = ImmutableList.of(
135             new UserParamNameAndValue("uno", "1"),
136             new UserParamNameAndValue("dos", "2"),
137             new UserParamNameAndValue("tres", "3")
138         );
139
140         VfModule vfModule = new VfModule(
141             newModelInfo(),
142             "instanceName",
143             "volumeGroupInstanceName",
144             "Delete",
145             "lcpCloudRegionId",
146             "legacyRegion",
147             "tenantId",
148             instanceParams,
149             supplementaryParams,
150             true,
151             true,
152             "instanceId",
153             "trackById",
154             true,
155             "statusMessage",
156             true,
157             true,
158             1,
159             afterCompletion,
160             "originalName");
161
162         verifySerializationAndDeserialization(vfModule);
163     }
164
165     @Test
166     public void VfModule_sdncPreLoad_shouldBeSerializedWithCorrectName() {
167
168         final boolean USE_PRELOAD = true;
169
170         VfModule vfModule = new VfModule(newModelInfo(), null, null, null,
171             null, null, null, null, null, false,
172             /* HERE ====> */ USE_PRELOAD,
173             null, null, null, null, null, null, null, null , null);
174
175         assertThat(vfModule, jsonPartEquals("sdncPreLoad", USE_PRELOAD));
176         assertThat(vfModule, jsonNodeAbsent("usePreload"));
177     }
178
179     @Test
180     public void VfModule_volumeGroupName_shouldBeSerializedWithCorrectName() {
181
182         final String VOLUME_GROUP_INSTANCE_NAME = "my volume group name";
183
184         VfModule vfModule = new VfModule(newModelInfo(), null,
185             /* HERE ====> */ VOLUME_GROUP_INSTANCE_NAME,
186             null, null, null, null, null, null,
187             false, null, null, null, null, null,
188             null, null, null, null, null);
189
190         assertThat(vfModule, jsonPartEquals("volumeGroupName", VOLUME_GROUP_INSTANCE_NAME));
191         assertThat(vfModule, jsonNodeAbsent("volumeGroupInstanceName"));
192     }
193
194     private ModelInfo newModelInfo() {
195         ModelInfo modelInfo = new ModelInfo();
196         setStringsInStringFields(modelInfo);
197         return modelInfo;
198     }
199
200     private void verifySerializationAndDeserialization(Object object) throws Exception {
201
202         assertThatAllValuesAreNotDefaultValues(object);
203
204         String valueAsString = JACKSON_OBJECT_MAPPER.writeValueAsString(object);
205         Object objectReconstructed = JACKSON_OBJECT_MAPPER.readValue(valueAsString, object.getClass());
206
207         // verify that all fields' values were reconstructed
208         assertThat(objectReconstructed, samePropertyValuesAs(object));
209     }
210
211     private void assertThatAllValuesAreNotDefaultValues(Object object)
212         throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
213         assertThat("setup is expected to have no field with a default Java value",
214             PropertyUtils.describe(object).entrySet(),
215             not(hasItem(hasProperty("value",
216                 either(nullValue())
217                     .or(equalTo(0))
218                     .or(equalTo(""))
219                     .or(equalTo(false))
220                     .or(equalTo(Create))))));
221     }
222
223 }