Merge "move aai-client into its own project"
[so.git] / graph-inventory / aai-client / src / test / java / org / onap / so / client / aai / AAISingleTransactionClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.aai;
22
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;
37 import java.util.Map;
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.so.client.aai.entities.singletransaction.SingleTransactionRequest;
49 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
50 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
52 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
53 import org.skyscreamer.jsonassert.JSONAssert;
54 import com.fasterxml.jackson.core.JsonParseException;
55 import com.fasterxml.jackson.databind.JsonMappingException;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57 import com.fasterxml.jackson.databind.SerializationFeature;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class AAISingleTransactionClientTest {
61
62     private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/singletransaction/";
63     AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "pserver-hostname");
64     AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex");
65     AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex2");
66
67     ObjectMapper mapper;
68
69     public AAIClient client = new AAIClient();
70
71     @Spy
72     public AAIResourcesClient aaiClient = new AAIResourcesClient();
73
74     @Before
75     public void before() throws JsonParseException, JsonMappingException, IOException {
76         mapper = new AAICommonObjectMapperProvider().getMapper();
77         mapper.enable(SerializationFeature.INDENT_OUTPUT);
78     }
79
80     @Test
81     public void testRequest() throws JSONException, IOException {
82         Pserver pserver = new Pserver();
83         pserver.setHostname("pserver-hostname");
84         pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
85         Pserver pserver2 = new Pserver();
86         pserver2.setFqdn("patched-fqdn");
87         Complex complex = new Complex();
88         complex.setCity("my-city");
89         Map<String, Object> map = new HashMap<>();
90         map.put("resource-version", "1234");
91         doReturn(Optional.of(map)).when(aaiClient).get(any(GenericType.class), eq(uriC));
92         AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction().create(uriA, pserver)
93                 .update(uriA, pserver2).create(uriB, complex).delete(uriC);
94
95         SingleTransactionRequest actual = singleTransaction.getRequest();
96
97         SingleTransactionRequest expected =
98                 mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
99
100         JSONAssert.assertEquals(mapper.writeValueAsString(expected), mapper.writeValueAsString(actual), false);
101     }
102
103     @Test
104     public void testFailure() throws IOException {
105         AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
106         SingleTransactionResponse expected =
107                 mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
108         Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
109
110         assertThat(expected.getOperationResponses().size(), greaterThan(0));
111         assertThat(errorMessage.isPresent(), equalTo(true));
112
113     }
114
115     @Test
116     public void testSuccessResponse() throws IOException {
117         AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
118         SingleTransactionResponse expected =
119                 mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
120         Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
121
122         assertThat(expected.getOperationResponses().size(), greaterThan(0));
123         assertThat(errorMessage.isPresent(), equalTo(false));
124
125     }
126
127     @Test
128     public void confirmPatchFormat() {
129         AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(aaiClient, client));
130         GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
131         doReturn(mock).when(singleTransaction).getPatchConverter();
132         singleTransaction.update(uriA, "{}");
133         verify(mock, times(1)).convertPatchFormat(any());
134     }
135
136     private String getJson(String filename) throws IOException {
137         return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
138     }
139
140 }