2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sa.rest;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
27 import org.json.simple.JSONObject;
28 import org.json.simple.parser.JSONParser;
29 import org.json.simple.parser.ParseException;
30 import org.junit.Ignore;
31 import org.junit.Test;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.client.Invocation.Builder;
35 import javax.ws.rs.client.WebTarget;
36 import javax.ws.rs.core.Application;
37 import java.io.IOException;
39 import static org.junit.Assert.assertTrue;
41 public class DocumentApiTest extends JerseyTest {
43 private static final String INDEXES_URI = "/test/indexes/";
44 private static final String DOCUMENT_URI = "documents/";
46 private static final String SEARCH_URI = "query/";
47 private static final String INDEX_NAME = "test-index";
48 private static final String DOC_ID = "test-1";
49 private static final String SIMPLE_QUERY = "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
50 private static final String COMPLEX_QUERY =
54 + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
58 + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
62 private static final String CREATE_JSON_CONTENT = "creation content";
66 protected Application configure() {
68 // Make sure that our test endpoint is on the resource path
70 return new ResourceConfig(SearchServiceApiHarness.class);
74 * This test validates the behaviour of the 'Create Document' POST request
78 * @throws ParseException
81 public void createDocumentTest() throws IOException, ParseException {
82 String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI).request().post(Entity.json(CREATE_JSON_CONTENT), String.class);
85 // Our stub document store DAO returns the parameters that it was
86 // passed as the result string, so now we can validate that our
87 // endpoint invoked it with the correct parameters.
89 JSONParser parser = new JSONParser();
90 JSONObject json = (JSONObject) parser.parse(result);
92 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
96 * This test validates the behaviour of the 'Create Document' PUT request
100 * @throws ParseException
103 public void updateDocumentTest() throws IOException, ParseException {
104 WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
105 Builder request = target.request().header("If-Match", "1");
106 String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
108 // Our stub document store DAO returns the parameters that it was
109 // passed as the result string, so now we can validate that our
110 // endpoint invoked it with the correct parameters.
111 JSONParser parser = new JSONParser();
112 JSONObject json = (JSONObject) parser.parse(result);
114 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
118 * This test validates the behaviour of the 'Get Document' GET request
121 * @throws IOException
122 * @throws ParseException
125 public void getDocumentTest() throws IOException, ParseException {
126 String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
128 // Our stub document store DAO returns the parameters that it was
129 // passed as the result string, so now we can validate that our
130 // endpoint invoked it with the correct parameters.
131 JSONParser parser = new JSONParser();
132 JSONObject json = (JSONObject) parser.parse(result);
134 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
139 * This test validates the behaviour of the 'Delete Document' DELETE request
142 * @throws IOException
143 * @throws ParseException
146 public void deleteDocumentTest() throws IOException, ParseException {
147 WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
148 Builder request = target.request().header("If-Match", "1");
149 String result = request.delete(String.class);
152 // Our stub document store DAO returns the parameters that it was
153 // passed as the result string, so now we can validate that our
154 // endpoint invoked it with the correct parameters.
155 assertTrue("Unexpected Result ", result.isEmpty());
160 * This test validates the behaviour of the 'Search Documents' GET request
163 * @throws IOException
164 * @throws ParseException
168 public void searchDocumentTest1() throws IOException, ParseException {
169 String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
171 // Our stub document store DAO returns the parameters that it was
172 // passed as the result string, so now we can validate that our
173 // endpoint invoked it with the correct parameters.
174 JSONParser parser = new JSONParser();
175 JSONObject json = (JSONObject) parser.parse(result);
177 assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
183 * This test validates the behaviour of the 'Search Documents' GET request
186 * @throws IOException
187 * @throws ParseException
190 public void searchDocumentTest2() throws IOException, ParseException {
191 String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
193 // Our stub document store DAO returns the parameters that it was
194 // passed as the result string, so now we can validate that our
195 // endpoint invoked it with the correct parameters.
196 JSONParser parser = new JSONParser();
197 JSONObject json = (JSONObject) parser.parse(result);
198 JSONObject resultJson = (JSONObject) json.get("searchResult");
200 assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));