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;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Optional;
38 import org.json.JSONException;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Spy;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.Pserver;
46 import org.onap.aai.domain.yang.v9.Complex;
47 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
48 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
49 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
50 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
51 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
52 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
53 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/";
64 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "pserver-hostname");
65 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex");
69 public AAIClient client = new AAIClient();
71 public AAIResourcesClient aaiClient = new AAIResourcesClient();
74 public void before() throws JsonParseException, JsonMappingException, IOException {
75 mapper = new AAICommonObjectMapperProvider().getMapper();
76 mapper.enable(SerializationFeature.INDENT_OUTPUT);
80 public void testRequest() throws JSONException,IOException {
81 Pserver pserver = new Pserver();
82 pserver.setHostname("pserver-hostname");
83 pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
84 Pserver pserver2 = new Pserver();
85 pserver2.setFqdn("patched-fqdn");
86 Complex complex = new Complex();
87 complex.setCity("my-city");
88 AAISingleTransactionClient singleTransaction =
89 aaiClient.beginSingleTransaction()
90 .create(uriA, pserver)
91 .update(uriA, pserver2)
92 .create(uriB, complex);
95 SingleTransactionRequest actual = singleTransaction.getRequest();
97 SingleTransactionRequest expected = mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
99 JSONAssert.assertEquals(mapper.writeValueAsString(expected),mapper.writeValueAsString(actual), false);
103 public void testFailure() throws IOException {
104 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
105 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
106 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
108 assertThat(expected.getOperationResponses().size(), greaterThan(0));
109 assertThat(errorMessage.isPresent(), equalTo(true));
114 public void testSuccessResponse() throws IOException {
115 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
116 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
117 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
119 assertThat(expected.getOperationResponses().size(), greaterThan(0));
120 assertThat(errorMessage.isPresent(), equalTo(false));
125 public void confirmPatchFormat() {
126 AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(aaiClient, client));
127 GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
128 doReturn(mock).when(singleTransaction).getPatchConverter();
129 singleTransaction.update(uriA, "{}");
130 verify(mock, times(1)).convertPatchFormat(any());
132 private String getJson(String filename) throws IOException {
133 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));