2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.aai;
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;
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;
54 @RunWith(MockitoJUnitRunner.class)
55 public class AAITransactionalClientTest {
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");
67 public AAIClient client = new AAIClient();
69 public AAIResourcesClient aaiClient = new AAIResourcesClient();
72 public void before() throws JsonParseException, JsonMappingException, IOException {
73 mapper = new AAICommonObjectMapperProvider().getMapper();
74 mapper.enable(SerializationFeature.INDENT_OUTPUT);
78 public void testCreate() throws IOException {
79 final Relationship body = new Relationship();
80 body.setRelatedLink(uriB.build().toString());
82 AAITransactionalClient transactions = aaiClient.beginTransaction().create(uriA.clone().relationshipAPI(), body);
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>>() {});
90 assertEquals(actual, expected);
94 public void testConnect() throws IOException {
95 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
98 AAIResourceUri uriAClone = uriA.clone();
99 AAITransactionalClient transactions = aaiClient.beginTransaction().connect(uriA, uris).connect(uriC, uriD)
100 .beginNewTransaction().connect(uriE, uriF);
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>>() {});
108 assertEquals(actual, expected);
109 assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
113 public void testDisconnect() throws IOException {
114 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
117 AAITransactionalClient transactions = aaiClient.beginTransaction().disconnect(uriA, uris);
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>>() {});
125 assertEquals(actual, expected);
129 public void testUpdate() throws IOException {
130 final Relationship body = new Relationship();
131 body.setRelatedLink(uriB.build().toString());
133 AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
134 AAITransactionalClient transactions = aaiClient.beginTransaction().update(uriAClone, body);
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>>() {});
142 assertEquals(actual, expected);
146 public void verifyResponse() throws IOException {
147 AAITransactionalClient transactions = aaiClient.beginTransaction();
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");
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());
164 private String getJson(String filename) throws IOException {
165 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));