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.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.List;
33 import java.util.Optional;
35 import org.junit.Before;
36 import org.junit.Test;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
40 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
41 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
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 import com.fasterxml.jackson.databind.SerializationFeature;
49 public class AAITransactionalClientTest {
51 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
52 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
53 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
54 AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
55 AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
56 AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
57 AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
62 public void before() throws JsonParseException, JsonMappingException, IOException {
63 mapper = new AAICommonObjectMapperProvider().getMapper();
64 mapper.enable(SerializationFeature.INDENT_OUTPUT);
68 public void testCreate() throws IOException {
69 final Relationship body = new Relationship();
70 body.setRelatedLink(uriB.build().toString());
72 AAITransactionalClient transactions = createClient().beginTransaction()
73 .create(uriA.clone().relationshipAPI(), body);
75 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
76 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
77 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>(){});
79 assertEquals(actual, expected);
83 public void testConnect() throws IOException {
84 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
87 AAIResourceUri uriAClone = uriA.clone();
88 AAITransactionalClient transactions = createClient()
89 .beginTransaction().connect(uriA, uris).connect(uriC, uriD)
90 .beginNewTransaction().connect(uriE, uriF);
92 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
93 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
94 Map<String, Object> expected = mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>(){});
96 assertEquals(actual, expected);
97 assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
101 public void testDisconnect() throws IOException {
102 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
105 AAITransactionalClient transactions = createClient().beginTransaction()
106 .disconnect(uriA, uris);
108 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
109 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
110 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"), new TypeReference<Map<String, Object>>(){});
112 assertEquals(actual, expected);
116 public void testUpdate() throws IOException {
117 final Relationship body = new Relationship();
118 body.setRelatedLink(uriB.build().toString());
120 AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
121 AAITransactionalClient transactions = createClient().beginTransaction().update(uriAClone, body);
123 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
124 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
125 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"), new TypeReference<Map<String, Object>>(){});
127 assertEquals(actual, expected);
131 public void verifyResponse() throws IOException {
132 AAITransactionalClient transactions = createClient()
135 assertEquals("success status", Optional.empty(), transactions.locateErrorMessages(getJson("response-success.json")));
136 assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(), "another error message\nmy great error");
139 private String getJson(String filename) throws IOException {
140 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
143 private AAIResourcesClient createClient() {
144 AAIResourcesClient client = spy(new AAIResourcesClient());
145 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();