2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.model.serviceInstantiation;
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.VfModule.PauseInstantiation.afterCompletion;
36 import static org.onap.vid.testUtils.TestUtils.setStringsInStringFields;
37 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
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;
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;
53 public class InstantiationModelSerializationTest {
55 final ImmutableList<Map<String, String>> instanceParams =
57 ImmutableMap.of("one", "1"),
58 ImmutableMap.of("two", "2")
62 public void serializeAndDeserializeServiceInstantiation() throws Exception {
64 ServiceInstantiation serviceInstantiation = new ServiceInstantiation(
73 "subscriptionServiceType",
95 new VidNotions(InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED,
96 ModelCategory.INFRASTRUCTURE_VPN,
97 InstantiationUI.INFRASTRUCTURE_VPN,
98 InstantiationType.Macro),
102 verifySerializationAndDeserialization(serviceInstantiation);
106 public void serializeAndDeserializeVnf() throws Exception {
109 newModelInfo(), "productFamilyId",
117 "lineOfBusinessName",
127 verifySerializationAndDeserialization(vnf);
131 public void serializeAndDeserializeVfModule() throws Exception {
133 List<UserParamNameAndValue> supplementaryParams = ImmutableList.of(
134 new UserParamNameAndValue("uno", "1"),
135 new UserParamNameAndValue("dos", "2"),
136 new UserParamNameAndValue("tres", "3")
139 VfModule vfModule = new VfModule(
142 "volumeGroupInstanceName",
161 verifySerializationAndDeserialization(vfModule);
165 public void VfModule_sdncPreLoad_shouldBeSerializedWithCorrectName() {
167 final boolean USE_PRELOAD = true;
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);
174 assertThat(vfModule, jsonPartEquals("sdncPreLoad", USE_PRELOAD));
175 assertThat(vfModule, jsonNodeAbsent("usePreload"));
179 public void VfModule_volumeGroupName_shouldBeSerializedWithCorrectName() {
181 final String VOLUME_GROUP_INSTANCE_NAME = "my volume group name";
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);
189 assertThat(vfModule, jsonPartEquals("volumeGroupName", VOLUME_GROUP_INSTANCE_NAME));
190 assertThat(vfModule, jsonNodeAbsent("volumeGroupInstanceName"));
193 private ModelInfo newModelInfo() {
194 ModelInfo modelInfo = new ModelInfo();
195 setStringsInStringFields(modelInfo);
199 private void verifySerializationAndDeserialization(Object object) throws Exception {
201 assertThatAllValuesAreNotDefaultValues(object);
203 String valueAsString = JACKSON_OBJECT_MAPPER.writeValueAsString(object);
204 Object objectReconstructed = JACKSON_OBJECT_MAPPER.readValue(valueAsString, object.getClass());
206 // verify that all fields' values were reconstructed
207 assertThat(objectReconstructed, samePropertyValuesAs(object));
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",
219 .or(equalTo(Create))))));