3e2801c452a408d9999f86d14ba80522ef9cfd0c
[so.git] / common / src / test / java / org / onap / so / client / aai / AAITransactionalClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
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.List;
36 import java.util.Map;
37 import java.util.Optional;
38
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.Relationship;
46 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
47 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
48 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
49 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
50
51 import com.fasterxml.jackson.core.JsonParseException;
52 import com.fasterxml.jackson.core.type.TypeReference;
53 import com.fasterxml.jackson.databind.JsonMappingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.fasterxml.jackson.databind.SerializationFeature;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class AAITransactionalClientTest {
59
60         private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
61         AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
62         AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
63         AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
64         AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
65         AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
66         AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
67         
68         ObjectMapper mapper;
69         
70         public AAIClient client = new AAIClient();
71         
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 testCreate() throws IOException {
82                 final Relationship body = new Relationship();
83                 body.setRelatedLink(uriB.build().toString());
84                 
85                 AAITransactionalClient transactions = aaiClient.beginTransaction()
86                                 .create(uriA.clone().relationshipAPI(), body);
87                 
88                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
89                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
90                 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>(){});
91
92                 assertEquals(actual, expected);
93         }
94         
95         @Test
96         public void testConnect() throws IOException {
97                 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
98                 uris.add(uriB);
99                 
100                 AAIResourceUri uriAClone = uriA.clone();
101                 AAITransactionalClient transactions = aaiClient
102                                 .beginTransaction().connect(uriA, uris).connect(uriC, uriD)
103                                 .beginNewTransaction().connect(uriE, uriF);
104                 
105                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
106                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
107                 Map<String, Object> expected = mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>(){});
108
109                 assertEquals(actual, expected);
110                 assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
111         }
112         
113         @Test
114         public void testDisconnect() throws IOException {
115                 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
116                 uris.add(uriB);
117                 
118                 AAITransactionalClient transactions = aaiClient.beginTransaction()
119                                 .disconnect(uriA, uris);
120                 
121                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
122                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
123                 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"), new TypeReference<Map<String, Object>>(){});
124
125                 assertEquals(actual, expected);
126         }
127         
128         @Test
129         public void testUpdate() throws IOException {
130                 final Relationship body = new Relationship();
131                 body.setRelatedLink(uriB.build().toString());
132                 
133                 AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
134                 AAITransactionalClient transactions = aaiClient.beginTransaction().update(uriAClone, body);
135                 
136                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
137                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
138                 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"), new TypeReference<Map<String, Object>>(){});
139
140                 assertEquals(actual, expected);
141         }
142         
143         @Test
144         public void verifyResponse() throws IOException {
145                 AAITransactionalClient transactions = aaiClient
146                                 .beginTransaction();
147         
148                 assertEquals("success status", Optional.empty(), transactions.locateErrorMessages(getJson("response-success.json")));
149                 assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(), "another error message\nmy great error");
150         }
151         
152         @Test
153         public void confirmPatchFormat() {
154                 AAITransactionalClient transactionClient = spy(new AAITransactionalClient(aaiClient, client));
155                 GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
156                 doReturn(mock).when(transactionClient).getPatchConverter();
157                 transactionClient.update(uriA, "{}");
158                 verify(mock, times(1)).convertPatchFormat(any());
159         }
160         
161         private String getJson(String filename) throws IOException {
162                  return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
163         }
164         
165 }