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