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