Add utilites and unit tests to help coverage
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / db / HttpEntryTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.rest.db;
23
24 import org.javatuples.Pair;
25 import org.junit.Before;
26 import org.junit.FixMethodOrder;
27 import org.junit.runners.MethodSorters;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.dbmap.DBConnectionType;
32 import org.onap.aai.domain.yang.Pserver;
33 import org.onap.aai.domain.yang.Pservers;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.*;
36 import org.onap.aai.parsers.query.QueryParser;
37 import org.onap.aai.restcore.HttpMethod;
38 import org.onap.aai.serialization.engines.QueryStyle;
39 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
40
41 import javax.ws.rs.core.*;
42
43 import java.io.BufferedReader;
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
46 import java.io.UnsupportedEncodingException;
47 import java.net.URI;
48 import java.util.*;
49 import java.util.stream.Collectors;
50
51 import com.google.gson.JsonObject;
52 import com.google.gson.JsonParser;
53
54 import static org.junit.Assert.assertEquals;
55 import static org.mockito.Matchers.anyObject;
56 import static org.mockito.Mockito.when;
57
58 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 public class HttpEntryTest extends AAISetup {
60
61     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
62
63     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
64
65     static {
66         VALID_HTTP_STATUS_CODES.add(200);
67         VALID_HTTP_STATUS_CODES.add(201);
68         VALID_HTTP_STATUS_CODES.add(204);
69     }
70
71     private HttpHeaders httpHeaders;
72
73     private UriInfo uriInfo;
74
75     private MultivaluedMap<String, String> headersMultiMap;
76     private MultivaluedMap<String, String> queryParameters;
77
78     private List<String> aaiRequestContextList;
79
80     private List<MediaType> outputMediaTypes;
81
82     @Before
83     public void setup(){
84
85         httpHeaders         = Mockito.mock(HttpHeaders.class);
86         uriInfo             = Mockito.mock(UriInfo.class);
87
88         headersMultiMap     = new MultivaluedHashMap<>();
89         queryParameters     = Mockito.spy(new MultivaluedHashMap<>());
90
91         headersMultiMap.add("X-FromAppId", "JUNIT");
92         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
93         headersMultiMap.add("Real-Time", "true");
94         headersMultiMap.add("Accept", "application/json");
95         headersMultiMap.add("aai-request-context", "");
96
97         outputMediaTypes = new ArrayList<>();
98         outputMediaTypes.add(APPLICATION_JSON);
99
100         aaiRequestContextList = new ArrayList<>();
101         aaiRequestContextList.add("");
102
103         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
104         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
105
106         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
107
108
109         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
110         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
111
112         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
113         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
114
115         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
116     }
117
118     
119     @Test
120     public void test1PutOnPserver() throws UnsupportedEncodingException, AAIException {
121
122         DBConnectionType type = DBConnectionType.REALTIME;
123         HttpEntry httpEntry = new HttpEntry(Version.getLatest(), ModelType.MOXY, QueryStyle.TRAVERSAL, type);
124         Loader loader = httpEntry.getLoader();
125         TransactionalGraphEngine dbEngine = httpEntry.getDbEngine();
126
127         URI uriObject = UriBuilder.fromPath("/cloud-infrastructure/pservers/pserver/junit-test1").build();
128
129         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
130
131         String content = "{\"hostname\":\"junit-test1\"}";
132
133         Introspector obj = loader.unmarshal("pserver", content, org.onap.aai.restcore.MediaType.getEnum("application/json"));
134
135         DBRequest dbRequest =
136                 new DBRequest.Builder(HttpMethod.PUT, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
137                         .rawRequestContent(content).build();
138
139         List<DBRequest> dbRequestList = new ArrayList<>();
140         dbRequestList.add(dbRequest);
141
142         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = httpEntry.process(dbRequestList, "JUNIT");
143         Response response = responsesTuple.getValue1().get(0).getValue1();
144         dbEngine.commit();
145         assertEquals("Expected the pserver to be created", 201, response.getStatus());
146     }
147
148     @Test
149     public void test2GetOnPserver() throws UnsupportedEncodingException, AAIException {
150
151         DBConnectionType type = DBConnectionType.REALTIME;
152         HttpEntry httpEntry = new HttpEntry(Version.getLatest(), ModelType.MOXY, QueryStyle.TRAVERSAL, type);
153         Loader loader = httpEntry.getLoader();
154         TransactionalGraphEngine dbEngine = httpEntry.getDbEngine();
155
156         URI uriObject = UriBuilder.fromPath("/cloud-infrastructure/pservers/pserver/junit-test1").build();
157
158         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
159
160         String content = "";
161
162         Introspector obj = loader.introspectorFromName("pserver");
163
164         DBRequest dbRequest =
165                 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
166                         .rawRequestContent(content).build();
167
168         List<DBRequest> dbRequestList = new ArrayList<>();
169         dbRequestList.add(dbRequest);
170
171         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = httpEntry.process(dbRequestList, "JUNIT");
172         Response response = responsesTuple.getValue1().get(0).getValue1();
173         dbEngine.commit();
174         assertEquals("Expected the pserver to be returned", 200, response.getStatus());
175     }
176   
177     @Test
178     public void test3MergePatchOnPserver() throws UnsupportedEncodingException, AAIException {
179
180         DBConnectionType type = DBConnectionType.REALTIME;
181         HttpEntry httpEntry = new HttpEntry(Version.getLatest(), ModelType.MOXY, QueryStyle.TRAVERSAL, type);
182         Loader loader = httpEntry.getLoader();
183         TransactionalGraphEngine dbEngine = httpEntry.getDbEngine();
184
185         URI uriObject = UriBuilder.fromPath("/cloud-infrastructure/pservers/pserver/junit-test1").build();
186
187         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
188
189         String content = "{\"hostname\":\"junit-test1\", \"equip-type\":\"junit-equip-type\"}";
190
191         Introspector obj = loader.unmarshal("pserver", content, org.onap.aai.restcore.MediaType.getEnum("application/json"));
192
193         DBRequest dbRequest =
194                 new DBRequest.Builder(HttpMethod.MERGE_PATCH, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
195                         .rawRequestContent(content).build();
196
197         List<DBRequest> dbRequestList = new ArrayList<>();
198         dbRequestList.add(dbRequest);
199
200         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = httpEntry.process(dbRequestList, "JUNIT");
201         Response response = responsesTuple.getValue1().get(0).getValue1();
202         dbEngine.commit();
203         assertEquals("Expected the pserver to be updated", 200, response.getStatus());
204     }
205     
206     private int doDelete(String resourceVersion) throws UnsupportedEncodingException, AAIException {
207         queryParameters.add("resource-version", resourceVersion);
208         DBConnectionType type = DBConnectionType.REALTIME;
209         HttpEntry httpEntry = new HttpEntry(Version.getLatest(), ModelType.MOXY, QueryStyle.TRAVERSAL, type);
210         Loader loader = httpEntry.getLoader();
211         TransactionalGraphEngine dbEngine = httpEntry.getDbEngine();
212
213         URI uriObject = UriBuilder.fromPath("/cloud-infrastructure/pservers/pserver/junit-test1").build();
214
215         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
216
217         String content = "";
218
219         Introspector obj = loader.introspectorFromName("pserver");
220
221         DBRequest dbRequest =
222                 new DBRequest.Builder(HttpMethod.DELETE, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
223                         .rawRequestContent(content).build();
224
225         List<DBRequest> dbRequestList = new ArrayList<>();
226         dbRequestList.add(dbRequest);
227
228         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = httpEntry.process(dbRequestList, "JUNIT");
229         Response response = responsesTuple.getValue1().get(0).getValue1();
230         dbEngine.commit();
231         return response.getStatus();
232     }
233     
234     @Test
235     public void test4DeleteOnPserver() throws UnsupportedEncodingException, AAIException {
236
237         DBConnectionType type = DBConnectionType.REALTIME;
238         HttpEntry httpEntry = new HttpEntry(Version.getLatest(), ModelType.MOXY, QueryStyle.TRAVERSAL, type);
239         Loader loader = httpEntry.getLoader();
240         TransactionalGraphEngine dbEngine = httpEntry.getDbEngine();
241
242         URI uriObject = UriBuilder.fromPath("/cloud-infrastructure/pservers/pserver/junit-test1").build();
243
244         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
245
246         String content = "";
247
248         Introspector obj = loader.introspectorFromName("pserver");
249
250         DBRequest dbRequest =
251                 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
252                         .rawRequestContent(content).build();
253
254         List<DBRequest> dbRequestList = new ArrayList<>();
255         dbRequestList.add(dbRequest);
256
257         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = httpEntry.process(dbRequestList, "JUNIT");
258         Response response = responsesTuple.getValue1().get(0).getValue1();
259         dbEngine.commit();
260         String msg = response.getEntity().toString();
261         JsonObject jsonObj = new JsonParser().parse(msg).getAsJsonObject();
262         String resourceVersion = "";
263         if ( jsonObj.isJsonObject()) {
264                 resourceVersion = jsonObj.get("resource-version").getAsString();
265         }
266         assertEquals("Expected the pserver to be deleted", 204, doDelete(resourceVersion));
267     }
268
269     @Test
270     public void test5FailedGetOnPserver() throws UnsupportedEncodingException, AAIException {
271
272         DBConnectionType type = DBConnectionType.REALTIME;
273         HttpEntry httpEntry = new HttpEntry(Version.getLatest(), ModelType.MOXY, QueryStyle.TRAVERSAL, type);
274         Loader loader = httpEntry.getLoader();
275         TransactionalGraphEngine dbEngine = httpEntry.getDbEngine();
276
277         URI uriObject = UriBuilder.fromPath("/cloud-infrastructure/pservers/pserver/junit-test2").build();
278
279         QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
280
281         String content = "";
282
283         Introspector obj = loader.introspectorFromName("pserver");
284
285         DBRequest dbRequest =
286                 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
287                         .rawRequestContent(content).build();
288
289         List<DBRequest> dbRequestList = new ArrayList<>();
290         dbRequestList.add(dbRequest);
291
292         Pair<Boolean, List<Pair<URI, Response>>> responsesTuple  = httpEntry.process(dbRequestList, "JUNIT");
293         Response response = responsesTuple.getValue1().get(0).getValue1();
294         dbEngine.commit();
295
296         assertEquals("Expected the pserver to be deleted", 404, response.getStatus());
297     }
298 }