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.Mockito.doReturn;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.util.Optional;
36 import org.json.JSONException;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Spy;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.aai.domain.yang.Pserver;
44 import org.onap.aai.domain.yang.v9.Complex;
45 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
46 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
47 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
48 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
49 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
50 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
51 import org.skyscreamer.jsonassert.JSONAssert;
52 import com.fasterxml.jackson.core.JsonParseException;
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 AAISingleTransactionClientTest {
60 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/singletransaction/";
61 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "pserver-hostname");
62 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex");
66 public AAIClient client = new AAIClient();
68 public AAIResourcesClient aaiClient = new AAIResourcesClient();
71 public void before() throws JsonParseException, JsonMappingException, IOException {
72 mapper = new AAICommonObjectMapperProvider().getMapper();
73 mapper.enable(SerializationFeature.INDENT_OUTPUT);
77 public void testRequest() throws JSONException, IOException {
78 Pserver pserver = new Pserver();
79 pserver.setHostname("pserver-hostname");
80 pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
81 Pserver pserver2 = new Pserver();
82 pserver2.setFqdn("patched-fqdn");
83 Complex complex = new Complex();
84 complex.setCity("my-city");
85 AAISingleTransactionClient singleTransaction =
86 aaiClient.beginSingleTransaction().create(uriA, pserver).update(uriA, pserver2).create(uriB, complex);
89 SingleTransactionRequest actual = singleTransaction.getRequest();
91 SingleTransactionRequest expected =
92 mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
94 JSONAssert.assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual), false);
98 public void testFailure() throws IOException {
99 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
100 SingleTransactionResponse expected =
101 mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
102 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
104 assertThat(expected.getOperationResponses().size(), greaterThan(0));
105 assertThat(errorMessage.isPresent(), equalTo(true));
110 public void testSuccessResponse() throws IOException {
111 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
112 SingleTransactionResponse expected =
113 mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
114 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
116 assertThat(expected.getOperationResponses().size(), greaterThan(0));
117 assertThat(errorMessage.isPresent(), equalTo(false));
122 public void confirmPatchFormat() {
123 AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(aaiClient, client));
124 GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
125 doReturn(mock).when(singleTransaction).getPatchConverter();
126 singleTransaction.update(uriA, "{}");
127 verify(mock, times(1)).convertPatchFormat(any());
130 private String getJson(String filename) throws IOException {
131 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));