cda119ef13f2e8aa6c95a5d57eb1b95f15deea31
[so.git] /
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.aaiclient.client.aai;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
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.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39 import javax.ws.rs.core.GenericType;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Spy;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.Relationship;
46 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
47 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
48 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
49 import org.onap.aaiclient.client.graphinventory.GraphInventoryPatchConverter;
50 import com.fasterxml.jackson.core.JsonParseException;
51 import com.fasterxml.jackson.core.type.TypeReference;
52 import com.fasterxml.jackson.databind.JsonMappingException;
53 import com.fasterxml.jackson.databind.ObjectMapper;
54 import com.fasterxml.jackson.databind.SerializationFeature;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class AAITransactionalClientTest {
58
59     private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/bulkprocess/";
60     AAIResourceUri uriA = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("test1"));
61     AAIResourceUri uriB = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().pserver("test2"));
62     AAIResourceUri uriC = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("test3"));
63     AAIResourceUri uriD = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().pserver("test4"));
64     AAIResourceUri uriE = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("test5"));
65     AAIResourceUri uriF = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().pserver("test6"));
66     AAIResourceUri uriG = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("test7"));
67
68     ObjectMapper mapper;
69
70     public AAIClient client = new AAIClient();
71
72     @Spy
73     public AAIResourcesClient aaiClient = new AAIResourcesClient();
74
75     @Before
76     public void before() throws JsonParseException, JsonMappingException, IOException {
77         mapper = new AAICommonObjectMapperProvider().getMapper();
78         mapper.enable(SerializationFeature.INDENT_OUTPUT);
79     }
80
81     @Test
82     public void testCreate() throws IOException {
83         final Relationship body = new Relationship();
84         body.setRelatedLink(uriB.build().toString());
85
86         AAITransactionalClient transactions = aaiClient.beginTransaction().create(uriA.clone().relationshipAPI(), body);
87
88         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
89         Map<String, Object> actual =
90                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
91         Map<String, Object> expected =
92                 mapper.readValue(getJson("test-request-small.json"), new TypeReference<Map<String, Object>>() {});
93
94         assertEquals(actual, expected);
95     }
96
97     @Test
98     public void testConnect() throws IOException {
99         List<AAIResourceUri> uris = new ArrayList<>();
100         uris.add(uriB);
101
102         Map<String, Object> map = new HashMap<>();
103         map.put("resource-version", "1234");
104         doReturn(Optional.of(map)).when(aaiClient).get(any(GenericType.class), eq(uriG));
105         AAIResourceUri uriAClone = uriA.clone();
106         AAITransactionalClient transactions = aaiClient.beginTransaction().connect(uriA, uris).connect(uriC, uriD)
107                 .beginNewTransaction().connect(uriE, uriF).beginNewTransaction().delete(uriG);
108
109         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
110         Map<String, Object> actual =
111                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
112         Map<String, Object> expected =
113                 mapper.readValue(getJson("test-request.json"), new TypeReference<Map<String, Object>>() {});
114
115         assertEquals(actual, expected);
116         assertEquals("uri not manipulated", uriAClone.build().toString(), uriA.build().toString());
117     }
118
119     @Test
120     public void testDisconnect() throws IOException {
121         List<AAIResourceUri> uris = new ArrayList<>();
122         uris.add(uriB);
123
124         AAITransactionalClient transactions = aaiClient.beginTransaction().disconnect(uriA, uris);
125
126         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
127         Map<String, Object> actual =
128                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
129         Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "delete"),
130                 new TypeReference<Map<String, Object>>() {});
131
132         assertEquals(actual, expected);
133     }
134
135     @Test
136     public void testUpdate() throws IOException {
137         final Relationship body = new Relationship();
138         body.setRelatedLink(uriB.build().toString());
139
140         AAIResourceUri uriAClone = uriA.clone().relationshipAPI();
141         AAITransactionalClient transactions = aaiClient.beginTransaction().update(uriAClone, body);
142
143         String serializedTransactions = mapper.writeValueAsString(transactions.getTransactions());
144         Map<String, Object> actual =
145                 mapper.readValue(serializedTransactions, new TypeReference<Map<String, Object>>() {});
146         Map<String, Object> expected = mapper.readValue(getJson("test-request-small.json").replace("put", "patch"),
147                 new TypeReference<Map<String, Object>>() {});
148
149         assertEquals(actual, expected);
150     }
151
152     @Test
153     public void verifyResponse() throws IOException {
154         AAITransactionalClient transactions = aaiClient.beginTransaction();
155
156         assertEquals("success status", Optional.empty(),
157                 transactions.locateErrorMessages(getJson("response-success.json")));
158         assertEquals(transactions.locateErrorMessages(getJson("response-failure.json")).get(),
159                 "another error message\nmy great error");
160     }
161
162     @Test
163     public void confirmPatchFormat() {
164         AAITransactionalClient transactionClient = spy(new AAITransactionalClient(aaiClient, client));
165         GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class);
166         doReturn(mock).when(transactionClient).getPatchConverter();
167         transactionClient.update(uriA, "{}");
168         verify(mock, times(1)).convertPatchFormat(any());
169     }
170
171     private String getJson(String filename) throws IOException {
172         return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
173     }
174
175 }