2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 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.junit.Assert.assertEquals;
 
  24 import static org.mockito.ArgumentMatchers.any;
 
  25 import static org.mockito.ArgumentMatchers.eq;
 
  26 import static org.mockito.Mockito.doReturn;
 
  27 import static org.mockito.Mockito.mock;
 
  28 import static org.mockito.Mockito.spy;
 
  29 import static org.mockito.Mockito.times;
 
  30 import static org.mockito.Mockito.verify;
 
  31 import java.io.IOException;
 
  32 import java.nio.file.Files;
 
  33 import java.nio.file.Paths;
 
  34 import java.util.ArrayList;
 
  35 import java.util.HashMap;
 
  36 import java.util.List;
 
  38 import java.util.Optional;
 
  39 import javax.ws.rs.core.GenericType;
 
  40 import org.junit.Before;
 
  41 import org.junit.Test;
 
  42 import org.junit.runner.RunWith;
 
  43 import org.mockito.Spy;
 
  44 import org.mockito.junit.MockitoJUnitRunner;
 
  45 import org.onap.aai.domain.yang.Relationship;
 
  46 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
 
  47 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
 
  48 import org.onap.aaiclient.client.graphinventory.GraphInventoryPatchConverter;
 
  49 import com.fasterxml.jackson.core.JsonParseException;
 
  50 import com.fasterxml.jackson.core.type.TypeReference;
 
  51 import com.fasterxml.jackson.databind.JsonMappingException;
 
  52 import com.fasterxml.jackson.databind.ObjectMapper;
 
  53 import com.fasterxml.jackson.databind.SerializationFeature;
 
  55 @RunWith(MockitoJUnitRunner.class)
 
  56 public class AAITransactionalClientTest {
 
  58     private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
 
  59     AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
 
  60     AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
 
  61     AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
 
  62     AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
 
  63     AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
 
  64     AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
 
  65     AAIResourceUri uriG = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test7");
 
  69     public AAIClient client = new AAIClient();
 
  72     public AAIResourcesClient aaiClient = new AAIResourcesClient();
 
  75     public void before() throws JsonParseException, JsonMappingException, IOException {
 
  76         mapper = new AAICommonObjectMapperProvider().getMapper();
 
  77         mapper.enable(SerializationFeature.INDENT_OUTPUT);
 
  81     public void testCreate() throws IOException {
 
  82         final Relationship body = new Relationship();
 
  83         body.setRelatedLink(uriB.build().toString());
 
  85         AAITransactionalClient transactions = aaiClient.beginTransaction().create(uriA.clone().relationshipAPI(), body);
 
  87         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
 
  88         Map<String, Object> actual =
 
  89                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
 
  90         Map<String, Object> expected =
 
  91                 mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>() {});
 
  93         assertEquals(actual, expected);
 
  97     public void testConnect() throws IOException {
 
  98         List<AAIResourceUri> uris = new ArrayList<>();
 
 101         Map<String, Object> map = new HashMap<>();
 
 102         map.put("resource-version", "1234");
 
 103         doReturn(Optional.of(map)).when(aaiClient).get(any(GenericType.class), eq(uriG));
 
 104         AAIResourceUri uriAClone = uriA.clone();
 
 105         AAITransactionalClient transactions = aaiClient.beginTransaction().connect(uriA, uris).connect(uriC, uriD)
 
 106                 .beginNewTransaction().connect(uriE, uriF).beginNewTransaction().delete(uriG);
 
 108         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
 
 109         Map<String, Object> actual =
 
 110                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
 
 111         Map<String, Object> expected =
 
 112                 mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>() {});
 
 114         assertEquals(actual, expected);
 
 115         assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
 
 119     public void testDisconnect() throws IOException {
 
 120         List<AAIResourceUri> uris = new ArrayList<>();
 
 123         AAITransactionalClient transactions = aaiClient.beginTransaction().disconnect(uriA, uris);
 
 125         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
 
 126         Map<String, Object> actual =
 
 127                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
 
 128         Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"),
 
 129                 new TypeReference<Map<String, Object>>() {});
 
 131         assertEquals(actual, expected);
 
 135     public void testUpdate() throws IOException {
 
 136         final Relationship body = new Relationship();
 
 137         body.setRelatedLink(uriB.build().toString());
 
 139         AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
 
 140         AAITransactionalClient transactions = aaiClient.beginTransaction().update(uriAClone, body);
 
 142         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
 
 143         Map<String, Object> actual =
 
 144                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
 
 145         Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"),
 
 146                 new TypeReference<Map<String, Object>>() {});
 
 148         assertEquals(actual, expected);
 
 152     public void verifyResponse() throws IOException {
 
 153         AAITransactionalClient transactions = aaiClient.beginTransaction();
 
 155         assertEquals("success status", Optional.empty(),
 
 156                 transactions.locateErrorMessages(getJson("response-success.json")));
 
 157         assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(),
 
 158                 "another error message\nmy great error");
 
 162     public void confirmPatchFormat() {
 
 163         AAITransactionalClient transactionClient = spy(new AAITransactionalClient(aaiClient, client));
 
 164         GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
 
 165         doReturn(mock).when(transactionClient).getPatchConverter();
 
 166         transactionClient.update(uriA, "{}");
 
 167         verify(mock, times(1)).convertPatchFormat(any());
 
 170     private String getJson(String filename) throws IOException {
 
 171         return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));