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.Matchers.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;
42 import org.onap.aai.domain.yang.Relationship;
43 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
45 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.core.type.TypeReference;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.databind.SerializationFeature;
53 public class AAITransactionalClientTest {
55 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
56 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
57 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
58 AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
59 AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
60 AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
61 AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
66 public void before() throws JsonParseException, JsonMappingException, IOException {
67 mapper = new AAICommonObjectMapperProvider().getMapper();
68 mapper.enable(SerializationFeature.INDENT_OUTPUT);
72 public void testCreate() throws IOException {
73 final Relationship body = new Relationship();
74 body.setRelatedLink(uriB.build().toString());
76 AAITransactionalClient transactions = createClient().beginTransaction()
77 .create(uriA.clone().relationshipAPI(), body);
79 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
80 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
81 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>(){});
83 assertEquals(actual, expected);
87 public void testConnect() throws IOException {
88 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
91 AAIResourceUri uriAClone = uriA.clone();
92 AAITransactionalClient transactions = createClient()
93 .beginTransaction().connect(uriA, uris).connect(uriC, uriD)
94 .beginNewTransaction().connect(uriE, uriF);
96 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
97 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
98 Map<String, Object> expected = mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>(){});
100 assertEquals(actual, expected);
101 assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
105 public void testDisconnect() throws IOException {
106 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
109 AAITransactionalClient transactions = createClient().beginTransaction()
110 .disconnect(uriA, uris);
112 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
113 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
114 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"), new TypeReference<Map<String, Object>>(){});
116 assertEquals(actual, expected);
120 public void testUpdate() throws IOException {
121 final Relationship body = new Relationship();
122 body.setRelatedLink(uriB.build().toString());
124 AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
125 AAITransactionalClient transactions = createClient().beginTransaction().update(uriAClone, body);
127 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
128 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
129 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"), new TypeReference<Map<String, Object>>(){});
131 assertEquals(actual, expected);
135 public void verifyResponse() throws IOException {
136 AAITransactionalClient transactions = createClient()
139 assertEquals("success status", Optional.empty(), transactions.locateErrorMessages(getJson("response-success.json")));
140 assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(), "another error message\nmy great error");
144 public void confirmPatchFormat() {
145 AAITransactionalClient client = spy(new AAITransactionalClient(AAIVersion.LATEST));
146 AAIPatchConverter mock = mock(AAIPatchConverter.class);
147 doReturn(mock).when(client).getPatchConverter();
148 client.update(uriA, "{}");
149 verify(mock, times(1)).convertPatchFormat(any());
152 private String getJson(String filename) throws IOException {
153 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
156 private AAIResourcesClient createClient() {
157 AAIResourcesClient client = spy(new AAIResourcesClient());
158 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();