Springboot 2.0 upgrade
[so.git] / common / 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.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;
32
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Optional;
37
38 import org.json.JSONException;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.aai.domain.yang.Pserver;
42 import org.onap.aai.domain.yang.v9.Complex;
43 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
44 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
45 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
46 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
47 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
48 import org.skyscreamer.jsonassert.JSONAssert;
49
50 import com.fasterxml.jackson.core.JsonParseException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.databind.SerializationFeature;
54
55 public class AAISingleTransactionClientTest {
56
57         private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/singletransaction/";
58         AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "pserver-hostname");
59         AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex");
60         
61         ObjectMapper mapper;
62         
63         @Before
64         public void before() throws JsonParseException, JsonMappingException, IOException {
65                 mapper = new AAICommonObjectMapperProvider().getMapper();
66                 mapper.enable(SerializationFeature.INDENT_OUTPUT);
67         }
68         
69         @Test
70         public void testRequest() throws JSONException,IOException {
71                 AAIResourcesClient client = createClient();
72                 Pserver pserver = new Pserver();
73                 pserver.setHostname("pserver-hostname");
74                 pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
75                 Pserver pserver2 = new Pserver();
76                 pserver2.setFqdn("patched-fqdn");
77                 Complex complex = new Complex();
78                 complex.setCity("my-city");
79                 AAISingleTransactionClient singleTransaction = 
80                 client.beginSingleTransaction()
81                         .create(uriA, pserver)
82                         .update(uriA, pserver2)
83                         .create(uriB, complex);
84                 
85                 
86                 SingleTransactionRequest actual = singleTransaction.getRequest();
87                 
88                 SingleTransactionRequest expected = mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
89                 
90                 JSONAssert.assertEquals(mapper.writeValueAsString(expected),mapper.writeValueAsString(actual), false);
91         }
92         
93         @Test
94         public void testFailure() throws IOException {
95                 AAIResourcesClient client = createClient();
96                 AAISingleTransactionClient singleTransaction = client.beginSingleTransaction();
97                 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
98                 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
99                 
100                 assertThat(expected.getOperationResponses().size(), greaterThan(0));
101                 assertThat(errorMessage.isPresent(), equalTo(true));
102                 
103         }
104         
105         @Test
106         public void testSuccessResponse() throws IOException {
107                 AAIResourcesClient client = createClient();
108                 AAISingleTransactionClient singleTransaction = client.beginSingleTransaction();
109                 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
110                 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
111                 
112                 assertThat(expected.getOperationResponses().size(), greaterThan(0));
113                 assertThat(errorMessage.isPresent(), equalTo(false));
114                 
115         }
116         
117         @Test
118         public void confirmPatchFormat() {
119                 AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(AAIVersion.LATEST));
120                 AAIPatchConverter mock = mock(AAIPatchConverter.class);
121                 doReturn(mock).when(singleTransaction).getPatchConverter();
122                 singleTransaction.update(uriA, "{}");
123                 verify(mock, times(1)).convertPatchFormat(any());
124         }
125         private String getJson(String filename) throws IOException {
126                  return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
127         }
128         
129         private AAIResourcesClient createClient() {
130                 AAIResourcesClient client = spy(new AAIResourcesClient());
131                 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
132                 return client;
133         }
134 }