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.Matchers.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.junit.Before;
39 import org.junit.Test;
40 import org.onap.aai.domain.yang.Pserver;
41 import org.onap.aai.domain.yang.v9.Complex;
42 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
43 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
44 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
45 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
46 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
47 import org.skyscreamer.jsonassert.JSONAssert;
49 import com.fasterxml.jackson.core.JsonParseException;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52 import com.fasterxml.jackson.databind.SerializationFeature;
54 public class AAISingleTransactionClientTest {
56 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/singletransaction/";
57 AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "pserver-hostname");
58 AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex");
63 public void before() throws JsonParseException, JsonMappingException, IOException {
64 mapper = new AAICommonObjectMapperProvider().getMapper();
65 mapper.enable(SerializationFeature.INDENT_OUTPUT);
69 public void testRequest() throws IOException {
70 AAIResourcesClient client = createClient();
71 Pserver pserver = new Pserver();
72 pserver.setHostname("pserver-hostname");
73 pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
74 Pserver pserver2 = new Pserver();
75 pserver2.setFqdn("patched-fqdn");
76 Complex complex = new Complex();
77 complex.setCity("my-city");
78 AAISingleTransactionClient singleTransaction =
79 client.beginSingleTransaction()
80 .create(uriA, pserver)
81 .update(uriA, pserver2)
82 .create(uriB, complex);
85 SingleTransactionRequest actual = singleTransaction.getRequest();
87 SingleTransactionRequest expected = mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
89 JSONAssert.assertEquals(mapper.writeValueAsString(expected),mapper.writeValueAsString(actual), false);
93 public void testFailure() throws IOException {
94 AAIResourcesClient client = createClient();
95 AAISingleTransactionClient singleTransaction = client.beginSingleTransaction();
96 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
97 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
99 assertThat(expected.getOperationResponses().size(), greaterThan(0));
100 assertThat(errorMessage.isPresent(), equalTo(true));
105 public void testSuccessResponse() throws IOException {
106 AAIResourcesClient client = createClient();
107 AAISingleTransactionClient singleTransaction = client.beginSingleTransaction();
108 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
109 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
111 assertThat(expected.getOperationResponses().size(), greaterThan(0));
112 assertThat(errorMessage.isPresent(), equalTo(false));
117 public void confirmPatchFormat() {
118 AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(AAIVersion.LATEST));
119 AAIPatchConverter mock = mock(AAIPatchConverter.class);
120 doReturn(mock).when(singleTransaction).getPatchConverter();
121 singleTransaction.update(uriA, "{}");
122 verify(mock, times(1)).convertPatchFormat(any());
124 private String getJson(String filename) throws IOException {
125 return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
128 private AAIResourcesClient createClient() {
129 AAIResourcesClient client = spy(new AAIResourcesClient());
130 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();