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