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.databind.ObjectMapper;
 
  56 import com.fasterxml.jackson.databind.SerializationFeature;
 
  58 @RunWith(MockitoJUnitRunner.class)
 
  59 public class AAISingleTransactionClientTest {
 
  61     private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/singletransaction/";
 
  63             AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().pserver("pserver-hostname"));
 
  65             AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().complex("my-complex"));
 
  67             AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().complex("my-complex2"));
 
  71     public AAIClient client = new AAIClient();
 
  74     public AAIResourcesClient aaiClient = new AAIResourcesClient();
 
  77     public void before() {
 
  78         mapper = new AAICommonObjectMapperProvider().getMapper();
 
  79         mapper.enable(SerializationFeature.INDENT_OUTPUT);
 
  83     public void testRequest() throws JSONException, IOException {
 
  84         Pserver pserver = new Pserver();
 
  85         pserver.setHostname("pserver-hostname");
 
  86         pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
 
  87         Pserver pserver2 = new Pserver();
 
  88         pserver2.setFqdn("patched-fqdn");
 
  89         Complex complex = new Complex();
 
  90         complex.setCity("my-city");
 
  91         Map<String, Object> map = new HashMap<>();
 
  92         map.put("resource-version", "1234");
 
  93         doReturn(Optional.of(map)).when(aaiClient).get(any(GenericType.class), eq(uriC));
 
  94         AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction().create(uriA, pserver)
 
  95                 .update(uriA, pserver2).create(uriB, complex).delete(uriC);
 
  97         SingleTransactionRequest actual = singleTransaction.getRequest();
 
  99         SingleTransactionRequest expected =
 
 100                 mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
 
 102         JSONAssert.assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual), false);
 
 106     public void testFailure() throws IOException {
 
 107         AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
 
 108         SingleTransactionResponse expected =
 
 109                 mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
 
 110         Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
 
 112         assertThat(expected.getOperationResponses().size(), greaterThan(0));
 
 113         assertThat(errorMessage.isPresent(), equalTo(true));
 
 118     public void testSuccessResponse() throws IOException {
 
 119         AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
 
 120         SingleTransactionResponse expected =
 
 121                 mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
 
 122         Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
 
 124         assertThat(expected.getOperationResponses().size(), greaterThan(0));
 
 125         assertThat(errorMessage.isPresent(), equalTo(false));
 
 130     public void confirmPatchFormat() {
 
 131         AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(aaiClient, client));
 
 132         GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
 
 133         doReturn(mock).when(singleTransaction).getPatchConverter();
 
 134         singleTransaction.update(uriA, "{}");
 
 135         verify(mock, times(1)).convertPatchFormat(any());
 
 138     private String getJson(String filename) throws IOException {
 
 139         return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));