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