2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.rest;
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.json.simple.JSONObject;
30 import org.json.simple.parser.JSONParser;
31 import org.json.simple.parser.ParseException;
32 import org.junit.Ignore;
33 import org.junit.Test;
35 import javax.ws.rs.client.Entity;
36 import javax.ws.rs.client.Invocation.Builder;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.Application;
39 import java.io.IOException;
41 import static org.junit.Assert.assertTrue;
43 public class DocumentApiTest extends JerseyTest {
45 private static final String INDEXES_URI = "/test/indexes/";
46 private static final String DOCUMENT_URI = "documents/";
48 private static final String SEARCH_URI = "query/";
49 private static final String INDEX_NAME = "test-index";
50 private static final String DOC_ID = "test-1";
51 private static final String SIMPLE_QUERY = "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
52 private static final String COMPLEX_QUERY =
56 + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
60 + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
64 private static final String CREATE_JSON_CONTENT = "creation content";
68 protected Application configure() {
70 // Make sure that our test endpoint is on the resource path
72 return new ResourceConfig(SearchServiceApiHarness.class);
76 * This test validates the behaviour of the 'Create Document' POST request
80 * @throws ParseException
83 public void createDocumentTest() throws IOException, ParseException {
84 String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI).request().post(Entity.json(CREATE_JSON_CONTENT), String.class);
87 // Our stub document store DAO returns the parameters that it was
88 // passed as the result string, so now we can validate that our
89 // endpoint invoked it with the correct parameters.
91 JSONParser parser = new JSONParser();
92 JSONObject json = (JSONObject) parser.parse(result);
94 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
98 * This test validates the behaviour of the 'Create Document' PUT request
101 * @throws IOException
102 * @throws ParseException
105 public void updateDocumentTest() throws IOException, ParseException {
106 WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
107 Builder request = target.request().header("If-Match", "1");
108 String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
110 // Our stub document store DAO returns the parameters that it was
111 // passed as the result string, so now we can validate that our
112 // endpoint invoked it with the correct parameters.
113 JSONParser parser = new JSONParser();
114 JSONObject json = (JSONObject) parser.parse(result);
116 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
120 * This test validates the behaviour of the 'Get Document' GET request
123 * @throws IOException
124 * @throws ParseException
127 public void getDocumentTest() throws IOException, ParseException {
128 String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
130 // Our stub document store DAO returns the parameters that it was
131 // passed as the result string, so now we can validate that our
132 // endpoint invoked it with the correct parameters.
133 JSONParser parser = new JSONParser();
134 JSONObject json = (JSONObject) parser.parse(result);
136 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
141 * This test validates the behaviour of the 'Delete Document' DELETE request
144 * @throws IOException
145 * @throws ParseException
148 public void deleteDocumentTest() throws IOException, ParseException {
149 WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
150 Builder request = target.request().header("If-Match", "1");
151 String result = request.delete(String.class);
154 // Our stub document store DAO returns the parameters that it was
155 // passed as the result string, so now we can validate that our
156 // endpoint invoked it with the correct parameters.
157 assertTrue("Unexpected Result ", result.isEmpty());
162 * This test validates the behaviour of the 'Search Documents' GET request
165 * @throws IOException
166 * @throws ParseException
170 public void searchDocumentTest1() throws IOException, ParseException {
171 String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
173 // Our stub document store DAO returns the parameters that it was
174 // passed as the result string, so now we can validate that our
175 // endpoint invoked it with the correct parameters.
176 JSONParser parser = new JSONParser();
177 JSONObject json = (JSONObject) parser.parse(result);
179 assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
185 * This test validates the behaviour of the 'Search Documents' GET request
188 * @throws IOException
189 * @throws ParseException
192 public void searchDocumentTest2() throws IOException, ParseException {
193 String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
195 // Our stub document store DAO returns the parameters that it was
196 // passed as the result string, so now we can validate that our
197 // endpoint invoked it with the correct parameters.
198 JSONParser parser = new JSONParser();
199 JSONObject json = (JSONObject) parser.parse(result);
200 JSONObject resultJson = (JSONObject) json.get("searchResult");
202 assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));