2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.Matchers.greaterThan;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.HashMap;
38 import java.util.Optional;
39 import javax.ws.rs.core.GenericType;
40 import org.json.JSONException;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Spy;
45 import org.mockito.junit.MockitoJUnitRunner;
46 import org.onap.aai.domain.yang.Pserver;
47 import org.onap.aai.domain.yang.v9.Complex;
48 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
49 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
50 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
52 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
53 import org.skyscreamer.jsonassert.JSONAssert;
54 import com.fasterxml.jackson.core.JsonParseException;
55 import com.fasterxml.jackson.databind.JsonMappingException;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57 import com.fasterxml.jackson.databind.SerializationFeature;
59 @RunWith(MockitoJUnitRunner.class)
60 public class AAISingleTransactionClientTest {
62 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/singletransaction/";
63 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "pserver-hostname");
64 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex");
65 AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex2");
69 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 testRequest() throws JSONException, IOException {
82 Pserver pserver = new Pserver();
83 pserver.setHostname("pserver-hostname");
84 pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
85 Pserver pserver2 = new Pserver();
86 pserver2.setFqdn("patched-fqdn");
87 Complex complex = new Complex();
88 complex.setCity("my-city");
89 Map<String, Object> map = new HashMap<>();
90 map.put("resource-version", "1234");
91 doReturn(Optional.of(map)).when(aaiClient).get(any(GenericType.class), eq(uriC));
92 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction().create(uriA, pserver)
93 .update(uriA, pserver2).create(uriB, complex).delete(uriC);
95 SingleTransactionRequest actual = singleTransaction.getRequest();
97 SingleTransactionRequest expected =
98 mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
100 JSONAssert.assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual), false);
104 public void testFailure() throws IOException {
105 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
106 SingleTransactionResponse expected =
107 mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
108 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
110 assertThat(expected.getOperationResponses().size(), greaterThan(0));
111 assertThat(errorMessage.isPresent(), equalTo(true));
116 public void testSuccessResponse() throws IOException {
117 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
118 SingleTransactionResponse expected =
119 mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
120 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
122 assertThat(expected.getOperationResponses().size(), greaterThan(0));
123 assertThat(errorMessage.isPresent(), equalTo(false));
128 public void confirmPatchFormat() {
129 AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(aaiClient, client));
130 GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
131 doReturn(mock).when(singleTransaction).getPatchConverter();
132 singleTransaction.update(uriA, "{}");
133 verify(mock, times(1)).convertPatchFormat(any());
136 private String getJson(String filename) throws IOException {
137 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));