Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / test / java / org / onap / so / client / aai / entities / AAIResultWrapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.client.aai.entities;
22
23 import static org.junit.Assert.assertEquals;
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Optional;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.junit.runner.RunWith;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.aai.domain.yang.GenericVnf;
38 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
39 import org.springframework.util.SerializationUtils;
40 import com.fasterxml.jackson.core.JsonParseException;
41 import com.fasterxml.jackson.core.type.TypeReference;
42 import com.fasterxml.jackson.databind.JsonMappingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class AAIResultWrapperTest {
47     String json;
48     @Rule
49     public ExpectedException thrown = ExpectedException.none();
50
51     AAIResultWrapper aaiResultWrapper;
52     AAIResultWrapper aaiResultWrapperEmpty;
53
54     @Before
55     public void init() throws IOException {
56         final String RESOURCE_PATH = "src/test/resources/__files/aai/resources/";
57         json = new String(Files.readAllBytes(Paths.get(RESOURCE_PATH + "e2e-complex.json")));
58
59         aaiResultWrapper = new AAIResultWrapper(json);
60         aaiResultWrapperEmpty = new AAIResultWrapper("{}");
61     }
62
63     @Test
64     public void testAAIResultWrapperIsSerializable() throws IOException {
65         AAIResultWrapper original = new AAIResultWrapper("");
66         byte[] serialized = SerializationUtils.serialize(original);
67         AAIResultWrapper deserialized = (AAIResultWrapper) SerializationUtils.deserialize(serialized);
68         assertEquals(deserialized.getJson(), original.getJson());
69     }
70
71     @Test
72     public void testGetRelationshipsEmpty() {
73         Optional<Relationships> relationships = aaiResultWrapperEmpty.getRelationships();
74         assertEquals("Compare relationships", Optional.empty(), relationships);
75     }
76
77     @Test
78     public void testAsMap() throws JsonParseException, JsonMappingException, IOException {
79         ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
80         Map<String, Object> expected = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
81
82         Map<String, Object> actual = aaiResultWrapper.asMap();
83         assertEquals(expected, actual);
84     }
85
86     @Test
87     public void testAsMapEmpty() {
88         Map<String, Object> actual = aaiResultWrapperEmpty.asMap();
89         assertEquals(new HashMap<>(), actual);
90     }
91
92     @Test
93     public void nullCases() {
94
95         AAIResultWrapper wrapper = new AAIResultWrapper(null);
96
97         assertEquals(Optional.empty(), wrapper.getRelationships());
98         assertEquals("{}", wrapper.getJson());
99         assertEquals(Optional.empty(), wrapper.asBean(GenericVnf.class));
100         assertEquals(true, wrapper.asMap().isEmpty());
101         assertEquals("{}", wrapper.toString());
102
103     }
104
105     @Test
106     public void objectConstructor() {
107         AAIResultWrapper wrapper = new AAIResultWrapper(new GenericVnf());
108         assertEquals("{}", wrapper.getJson());
109     }
110 }