cbf8d67a82b2f98613a3b479a82d3150bbcbb82b
[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.Matchers.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
42 import org.onap.aai.domain.yang.Relationship;
43 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
45 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
46
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.core.type.TypeReference;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.databind.SerializationFeature;
52
53 public class AAITransactionalClientTest {
54
55         private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
56         AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test1");
57         AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test2");
58         AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3");
59         AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test4");
60         AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test5");
61         AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIObjectType.PSERVER, "test6");
62         
63         ObjectMapper mapper;
64         
65         @Before
66         public void before() throws JsonParseException, JsonMappingException, IOException {
67                 mapper = new AAICommonObjectMapperProvider().getMapper();
68                 mapper.enable(SerializationFeature.INDENT_OUTPUT);
69         }
70         
71         @Test
72         public void testCreate() throws IOException {
73                 final Relationship body = new Relationship();
74                 body.setRelatedLink(uriB.build().toString());
75                 
76                 AAITransactionalClient transactions = createClient().beginTransaction()
77                                 .create(uriA.clone().relationshipAPI(), body);
78                 
79                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
80                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
81                 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>(){});
82
83                 assertEquals(actual, expected);
84         }
85         
86         @Test
87         public void testConnect() throws IOException {
88                 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
89                 uris.add(uriB);
90                 
91                 AAIResourceUri uriAClone = uriA.clone();
92                 AAITransactionalClient transactions = createClient()
93                                 .beginTransaction().connect(uriA, uris).connect(uriC, uriD)
94                                 .beginNewTransaction().connect(uriE, uriF);
95                 
96                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
97                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
98                 Map<String, Object> expected = mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>(){});
99
100                 assertEquals(actual, expected);
101                 assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
102         }
103         
104         @Test
105         public void testDisconnect() throws IOException {
106                 List<AAIResourceUri> uris = new ArrayList<AAIResourceUri>();
107                 uris.add(uriB);
108                 
109                 AAITransactionalClient transactions = createClient().beginTransaction()
110                                 .disconnect(uriA, uris);
111                 
112                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
113                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
114                 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"), new TypeReference<Map<String, Object>>(){});
115
116                 assertEquals(actual, expected);
117         }
118         
119         @Test
120         public void testUpdate() throws IOException {
121                 final Relationship body = new Relationship();
122                 body.setRelatedLink(uriB.build().toString());
123                 
124                 AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
125                 AAITransactionalClient transactions = createClient().beginTransaction().update(uriAClone, body);
126                 
127                 String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
128                 Map<String, Object> actual = mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>(){});
129                 Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"), new TypeReference<Map<String, Object>>(){});
130
131                 assertEquals(actual, expected);
132         }
133         
134         @Test
135         public void verifyResponse() throws IOException {
136                 AAITransactionalClient transactions = createClient()
137                                 .beginTransaction();
138         
139                 assertEquals("success status", Optional.empty(), transactions.locateErrorMessages(getJson("response-success.json")));
140                 assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(), "another error message\nmy great error");
141         }
142         
143         @Test
144         public void confirmPatchFormat() {
145                 AAITransactionalClient client = spy(new AAITransactionalClient(AAIVersion.LATEST));
146                 AAIPatchConverter mock = mock(AAIPatchConverter.class);
147                 doReturn(mock).when(client).getPatchConverter();
148                 client.update(uriA, "{}");
149                 verify(mock, times(1)).convertPatchFormat(any());
150         }
151         
152         private String getJson(String filename) throws IOException {
153                  return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
154         }
155         
156         private AAIResourcesClient createClient() {
157                 AAIResourcesClient client = spy(new AAIResourcesClient());
158                 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
159                 return client;
160         }
161 }