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;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.List;
37 import java.util.Optional;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Spy;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.Relationship;
46 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
47 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
48 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
49 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
51 import com.fasterxml.jackson.core.JsonParseException;
52 import com.fasterxml.jackson.core.type.TypeReference;
53 import com.fasterxml.jackson.databind.JsonMappingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.fasterxml.jackson.databind.SerializationFeature;
57 @RunWith(MockitoJUnitRunner.class)
58 public class AAITransactionalClientTest {
60 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
61 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
62 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
63 AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
64 AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
65 AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
66 AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
70 public AAIClient client = new AAIClient();
72 public AAIResourcesClient aaiClient = new AAIResourcesClient();
75 public void before() throws JsonParseException, JsonMappingException, IOException {
76 mapper = new AAICommonObjectMapperProvider().getMapper();
77 mapper.enable(SerializationFeature.INDENT_OUTPUT);
81 public void testCreate() throws IOException {
82 final Relationship body = new Relationship();
83 body.setRelatedLink(uriB.build().toString());
85 AAITransactionalClient transactions = aaiClient.beginTransaction()
86 .create(uriA.clone().relationshipAPI(), body);
88 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
89 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
90 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>(){});
92 assertEquals(actual, expected);
96 public void testConnect() throws IOException {
97 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
100 AAIResourceUri uriAClone = uriA.clone();
101 AAITransactionalClient transactions = aaiClient
102 .beginTransaction().connect(uriA, uris).connect(uriC, uriD)
103 .beginNewTransaction().connect(uriE, uriF);
105 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
106 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
107 Map<String, Object> expected = mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>(){});
109 assertEquals(actual, expected);
110 assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
114 public void testDisconnect() throws IOException {
115 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
118 AAITransactionalClient transactions = aaiClient.beginTransaction()
119 .disconnect(uriA, uris);
121 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
122 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
123 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"), 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 = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
138 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"), new TypeReference<Map<String, Object>>(){});
140 assertEquals(actual, expected);
144 public void verifyResponse() throws IOException {
145 AAITransactionalClient transactions = aaiClient
148 assertEquals("success status", Optional.empty(), transactions.locateErrorMessages(getJson("response-success.json")));
149 assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(), "another error message\nmy great error");
153 public void confirmPatchFormat() {
154 AAITransactionalClient transactionClient = spy(new AAITransactionalClient(aaiClient, client));
155 GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
156 doReturn(mock).when(transactionClient).getPatchConverter();
157 transactionClient.update(uriA, "{}");
158 verify(mock, times(1)).convertPatchFormat(any());
161 private String getJson(String filename) throws IOException {
162 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));