2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sa.rest;
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.glassfish.jersey.test.JerseyTest;
25 import org.json.simple.JSONObject;
26 import org.json.simple.parser.JSONParser;
27 import org.json.simple.parser.ParseException;
28 import org.junit.Ignore;
29 import org.junit.Test;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.client.Invocation.Builder;
33 import javax.ws.rs.client.WebTarget;
34 import javax.ws.rs.core.Application;
35 import java.io.IOException;
37 import static org.junit.Assert.assertTrue;
39 public class DocumentApiTest extends JerseyTest {
41 private static final String INDEXES_URI = "/test/indexes/";
42 private static final String DOCUMENT_URI = "documents/";
44 private static final String SEARCH_URI = "query/";
45 private static final String INDEX_NAME = "test-index";
46 private static final String DOC_ID = "test-1";
47 private static final String SIMPLE_QUERY = "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
48 private static final String COMPLEX_QUERY =
52 + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
56 + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
60 private static final String CREATE_JSON_CONTENT = "creation content";
64 protected Application configure() {
66 // Make sure that our test endpoint is on the resource path
68 return new ResourceConfig(SearchServiceApiHarness.class);
72 * This test validates the behaviour of the 'Create Document' POST request
76 * @throws ParseException
79 public void createDocumentTest() throws IOException, ParseException {
80 String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI).request().post(Entity.json(CREATE_JSON_CONTENT), String.class);
83 // Our stub document store DAO returns the parameters that it was
84 // passed as the result string, so now we can validate that our
85 // endpoint invoked it with the correct parameters.
87 JSONParser parser = new JSONParser();
88 JSONObject json = (JSONObject) parser.parse(result);
90 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
94 * This test validates the behaviour of the 'Create Document' PUT request
98 * @throws ParseException
101 public void updateDocumentTest() throws IOException, ParseException {
102 WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
103 Builder request = target.request().header("If-Match", "1");
104 String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
106 // Our stub document store DAO returns the parameters that it was
107 // passed as the result string, so now we can validate that our
108 // endpoint invoked it with the correct parameters.
109 JSONParser parser = new JSONParser();
110 JSONObject json = (JSONObject) parser.parse(result);
112 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
116 * This test validates the behaviour of the 'Get Document' GET request
119 * @throws IOException
120 * @throws ParseException
123 public void getDocumentTest() throws IOException, ParseException {
124 String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
126 // Our stub document store DAO returns the parameters that it was
127 // passed as the result string, so now we can validate that our
128 // endpoint invoked it with the correct parameters.
129 JSONParser parser = new JSONParser();
130 JSONObject json = (JSONObject) parser.parse(result);
132 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
137 * This test validates the behaviour of the 'Delete Document' DELETE request
140 * @throws IOException
141 * @throws ParseException
144 public void deleteDocumentTest() throws IOException, ParseException {
145 WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
146 Builder request = target.request().header("If-Match", "1");
147 String result = request.delete(String.class);
150 // Our stub document store DAO returns the parameters that it was
151 // passed as the result string, so now we can validate that our
152 // endpoint invoked it with the correct parameters.
153 assertTrue("Unexpected Result ", result.isEmpty());
158 * This test validates the behaviour of the 'Search Documents' GET request
161 * @throws IOException
162 * @throws ParseException
166 public void searchDocumentTest1() throws IOException, ParseException {
167 String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
169 // Our stub document store DAO returns the parameters that it was
170 // passed as the result string, so now we can validate that our
171 // endpoint invoked it with the correct parameters.
172 JSONParser parser = new JSONParser();
173 JSONObject json = (JSONObject) parser.parse(result);
175 assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
181 * This test validates the behaviour of the 'Search Documents' GET request
184 * @throws IOException
185 * @throws ParseException
188 public void searchDocumentTest2() throws IOException, ParseException {
189 String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
191 // Our stub document store DAO returns the parameters that it was
192 // passed as the result string, so now we can validate that our
193 // endpoint invoked it with the correct parameters.
194 JSONParser parser = new JSONParser();
195 JSONObject json = (JSONObject) parser.parse(result);
196 JSONObject resultJson = (JSONObject) json.get("searchResult");
198 assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));