Sync Integ to Master
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / resources / JsonParserUtilsTests.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.be.resources;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.fasterxml.jackson.databind.SerializationFeature;
26 import com.google.common.collect.ImmutableList;
27 import org.junit.Test;
28 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
29 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatCode;
38 import static org.openecomp.sdc.be.utils.FixtureHelpers.fixture;
39 import static org.openecomp.sdc.be.utils.JsonTester.testJsonMap;
40
41 public class JsonParserUtilsTests {
42
43     private static final String FIXTURE_PATH = "fixtures/ListCapabilityDataDefinition.json";
44
45     @Test
46     public void testToMap() {
47         String json = fixture(FIXTURE_PATH);
48         Map<String, ListCapabilityDataDefinition> actual = JsonParserUtils.toMap(json, ListCapabilityDataDefinition.class);
49         Map<String, ListCapabilityDataDefinition> expected = buildMap();
50         assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
51     }
52
53     @Test
54     public void testJacksonFasterXml() {
55         ObjectMapper mapper = new ObjectMapper()
56                 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
57                 .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
58         assertThatCode(() -> testJsonMap(buildMap(), ListCapabilityDataDefinition.class, FIXTURE_PATH, mapper))
59                 .doesNotThrowAnyException();
60     }
61
62     private Map<String, ListCapabilityDataDefinition> buildMap() {
63         Map<String, ListCapabilityDataDefinition> map = new HashMap<>();
64         map.put("org.openecomp.capabilities.Forwarder", buildListCapabilityDataDefinition());
65         return map;
66     }
67
68     private ListCapabilityDataDefinition buildListCapabilityDataDefinition() {
69         CapabilityDataDefinition dataDefinition = new CapabilityDataDefinition();
70         dataDefinition.setName("forwarder");
71         dataDefinition.setType("org.openecomp.capabilities.Forwarder");
72         dataDefinition.setUniqueId("capability.deb142fd-95eb-48f7-99ae-81ab09466b1e.forwarder");
73         dataDefinition.setOwnerId("deb142fd-95eb-48f7-99ae-81ab09466b1e");
74         dataDefinition.setMinOccurrences("1");
75         dataDefinition.setLeftOccurrences("UNBOUNDED");
76         dataDefinition.setMaxOccurrences("UNBOUNDED");
77         dataDefinition.setCapabilitySources(buildCapabilitySources());
78
79         return new ListCapabilityDataDefinition(ImmutableList.of(dataDefinition));
80     }
81
82     private List<String> buildCapabilitySources() {
83         return ImmutableList.of(
84                 "org.openecomp.resource.cp.nodes.network.Port",
85                 "org.openecomp.resource.cp.v2.extCP",
86                 "org.openecomp.resource.cp.v2.extContrailCP");
87     }
88 }