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.aaiclient.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.aaiclient.client.aai.entities.singletransaction.SingleTransactionRequest;
49 import org.onap.aaiclient.client.aai.entities.singletransaction.SingleTransactionResponse;
50 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
52 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
53 import org.onap.aaiclient.client.graphinventory.GraphInventoryPatchConverter;
54 import org.skyscreamer.jsonassert.JSONAssert;
55 import com.fasterxml.jackson.core.JsonParseException;
56 import com.fasterxml.jackson.databind.JsonMappingException;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58 import com.fasterxml.jackson.databind.SerializationFeature;
60 @RunWith(MockitoJUnitRunner.class)
61 public class AAISingleTransactionClientTest {
63 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/singletransaction/";
65 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().pserver("pserver-hostname"));
67 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().complex("my-complex"));
69 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().complex("my-complex2"));
73 public AAIClient client = new AAIClient();
76 public AAIResourcesClient aaiClient = new AAIResourcesClient();
79 public void before() throws JsonParseException, JsonMappingException, IOException {
80 mapper = new AAICommonObjectMapperProvider().getMapper();
81 mapper.enable(SerializationFeature.INDENT_OUTPUT);
85 public void testRequest() throws JSONException, IOException {
86 Pserver pserver = new Pserver();
87 pserver.setHostname("pserver-hostname");
88 pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
89 Pserver pserver2 = new Pserver();
90 pserver2.setFqdn("patched-fqdn");
91 Complex complex = new Complex();
92 complex.setCity("my-city");
93 Map<String, Object> map = new HashMap<>();
94 map.put("resource-version", "1234");
95 doReturn(Optional.of(map)).when(aaiClient).get(any(GenericType.class), eq(uriC));
96 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction().create(uriA, pserver)
97 .update(uriA, pserver2).create(uriB, complex).delete(uriC);
99 SingleTransactionRequest actual = singleTransaction.getRequest();
101 SingleTransactionRequest expected =
102 mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
104 JSONAssert.assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual), false);
108 public void testFailure() throws IOException {
109 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
110 SingleTransactionResponse expected =
111 mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
112 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
114 assertThat(expected.getOperationResponses().size(), greaterThan(0));
115 assertThat(errorMessage.isPresent(), equalTo(true));
120 public void testSuccessResponse() throws IOException {
121 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
122 SingleTransactionResponse expected =
123 mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
124 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
126 assertThat(expected.getOperationResponses().size(), greaterThan(0));
127 assertThat(errorMessage.isPresent(), equalTo(false));
132 public void confirmPatchFormat() {
133 AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(aaiClient, client));
134 GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
135 doReturn(mock).when(singleTransaction).getPatchConverter();
136 singleTransaction.update(uriA, "{}");
137 verify(mock, times(1)).convertPatchFormat(any());
140 private String getJson(String filename) throws IOException {
141 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));