Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / test / java / org / onap / so / client / aai / AAITransactionalClientTest.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;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Optional;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Spy;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.aai.domain.yang.Relationship;
44 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
45 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
46 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
47 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
48 import com.fasterxml.jackson.core.JsonParseException;
49 import com.fasterxml.jackson.core.type.TypeReference;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52 import com.fasterxml.jackson.databind.SerializationFeature;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class AAITransactionalClientTest {
56
57     private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
58     AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
59     AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
60     AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
61     AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
62     AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
63     AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
64
65     ObjectMapper mapper;
66
67     public AAIClient client = new AAIClient();
68
69     public AAIResourcesClient aaiClient = new AAIResourcesClient();
70
71     @Before
72     public void before() throws JsonParseException, JsonMappingException, IOException {
73         mapper = new AAICommonObjectMapperProvider().getMapper();
74         mapper.enable(SerializationFeature.INDENT_OUTPUT);
75     }
76
77     @Test
78     public void testCreate() throws IOException {
79         final Relationship body = new Relationship();
80         body.setRelatedLink(uriB.build().toString());
81
82         AAITransactionalClient transactions = aaiClient.beginTransaction().create(uriA.clone().relationshipAPI(), body);
83
84         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
85         Map<String, Object> actual =
86                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
87         Map<String, Object> expected =
88                 mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>() {});
89
90         assertEquals(actual, expected);
91     }
92
93     @Test
94     public void testConnect() throws IOException {
95         List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
96         uris.add(uriB);
97
98         AAIResourceUri uriAClone = uriA.clone();
99         AAITransactionalClient transactions = aaiClient.beginTransaction().connect(uriA, uris).connect(uriC, uriD)
100                 .beginNewTransaction().connect(uriE, uriF);
101
102         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
103         Map<String, Object> actual =
104                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
105         Map<String, Object> expected =
106                 mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>() {});
107
108         assertEquals(actual, expected);
109         assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
110     }
111
112     @Test
113     public void testDisconnect() throws IOException {
114         List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
115         uris.add(uriB);
116
117         AAITransactionalClient transactions = aaiClient.beginTransaction().disconnect(uriA, uris);
118
119         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
120         Map<String, Object> actual =
121                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
122         Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"),
123                 new TypeReference<Map<String, Object>>() {});
124
125         assertEquals(actual, expected);
126     }
127
128     @Test
129     public void testUpdate() throws IOException {
130         final Relationship body = new Relationship();
131         body.setRelatedLink(uriB.build().toString());
132
133         AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
134         AAITransactionalClient transactions = aaiClient.beginTransaction().update(uriAClone, body);
135
136         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
137         Map<String, Object> actual =
138                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
139         Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"),
140                 new TypeReference<Map<String, Object>>() {});
141
142         assertEquals(actual, expected);
143     }
144
145     @Test
146     public void verifyResponse() throws IOException {
147         AAITransactionalClient transactions = aaiClient.beginTransaction();
148
149         assertEquals("success status", Optional.empty(),
150                 transactions.locateErrorMessages(getJson("response-success.json")));
151         assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(),
152                 "another error message\nmy great error");
153     }
154
155     @Test
156     public void confirmPatchFormat() {
157         AAITransactionalClient transactionClient = spy(new AAITransactionalClient(aaiClient, client));
158         GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
159         doReturn(mock).when(transactionClient).getPatchConverter();
160         transactionClient.update(uriA, "{}");
161         verify(mock, times(1)).convertPatchFormat(any());
162     }
163
164     private String getJson(String filename) throws IOException {
165         return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
166     }
167
168 }