d875f384b0077af69c5da3cee887f1118b3fcf56
[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.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Spy;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.Pserver;
46 import org.onap.aai.domain.yang.v9.Complex;
47 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
48 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse;
49 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
50 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
51 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
52 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
53 import org.skyscreamer.jsonassert.JSONAssert;
54
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 = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "pserver-hostname");
65         AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.COMPLEX, "my-complex");
66         
67         ObjectMapper mapper;
68         
69         public AAIClient client = new AAIClient();
70         
71         public AAIResourcesClient aaiClient = new AAIResourcesClient();
72         
73         @Before
74         public void before() throws JsonParseException, JsonMappingException, IOException {
75                 mapper = new AAICommonObjectMapperProvider().getMapper();
76                 mapper.enable(SerializationFeature.INDENT_OUTPUT);
77         }
78         
79         @Test
80         public void testRequest() throws JSONException,IOException {
81                 Pserver pserver = new Pserver();
82                 pserver.setHostname("pserver-hostname");
83                 pserver.setFqdn("pserver-bulk-process-single-transactions-multiple-actions-1-fqdn");
84                 Pserver pserver2 = new Pserver();
85                 pserver2.setFqdn("patched-fqdn");
86                 Complex complex = new Complex();
87                 complex.setCity("my-city");
88                 AAISingleTransactionClient singleTransaction = 
89                 aaiClient.beginSingleTransaction()
90                         .create(uriA, pserver)
91                         .update(uriA, pserver2)
92                         .create(uriB, complex);
93                 
94                 
95                 SingleTransactionRequest actual = singleTransaction.getRequest();
96                 
97                 SingleTransactionRequest expected = mapper.readValue(this.getJson("sample-request.json"), SingleTransactionRequest.class);
98                 
99                 JSONAssert.assertEquals(mapper.writeValueAsString(expected),mapper.writeValueAsString(actual), false);
100         }
101         
102         @Test
103         public void testFailure() throws IOException {
104                 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
105                 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response-failure.json"), SingleTransactionResponse.class);
106                 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
107                 
108                 assertThat(expected.getOperationResponses().size(), greaterThan(0));
109                 assertThat(errorMessage.isPresent(), equalTo(true));
110                 
111         }
112         
113         @Test
114         public void testSuccessResponse() throws IOException {
115                 AAISingleTransactionClient singleTransaction = aaiClient.beginSingleTransaction();
116                 SingleTransactionResponse expected = mapper.readValue(this.getJson("sample-response.json"), SingleTransactionResponse.class);
117                 Optional<String> errorMessage = singleTransaction.locateErrorMessages(expected);
118                 
119                 assertThat(expected.getOperationResponses().size(), greaterThan(0));
120                 assertThat(errorMessage.isPresent(), equalTo(false));
121                 
122         }
123         
124         @Test
125         public void confirmPatchFormat() {
126                 AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(aaiClient, client));
127                 GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
128                 doReturn(mock).when(singleTransaction).getPatchConverter();
129                 singleTransaction.update(uriA, "{}");
130                 verify(mock, times(1)).convertPatchFormat(any());
131         }
132         private String getJson(String filename) throws IOException {
133                  return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
134         }
135         
136 }