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;
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.glassfish.jersey.test.JerseyTest;
26 import org.junit.Test;
27 import org.onap.aai.sa.rest.IndexApi;
28 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
29 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.core.Application;
33 import javax.ws.rs.core.Response;
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertTrue;
41 * This suite of tests is intended to exercise the set of REST endpoints
42 * associated with manipulating Indexes in the document store.
44 public class IndexApiTest extends JerseyTest {
46 private final String TOP_URI = "/test/indexes/";
47 private final String SIMPLE_DOC_SCHEMA_JSON = "src/test/resources/json/simpleDocument.json";
51 protected Application configure() {
53 // Make sure that our test endpoint is on the resource path
55 return new ResourceConfig(SearchServiceApiHarness.class);
60 * This test validates that the {@link IndexApi} is able to convert {@link OperationResult}
61 * obects to standard REST {@link Response} objects.
63 * @throws FileNotFoundException
65 * @throws DocumentStoreOperationException
68 public void responseFromOperationResultTest() throws FileNotFoundException, IOException, DocumentStoreOperationException {
70 int SUCCESS_RESULT_CODE = 200;
71 String SUCCESS_RESULT_STRING = "Everything is ay-okay!";
72 int FAILURE_RESULT_CODE = 500;
73 String FAILURE_CAUSE_STRING = "Something went wrong!";
76 // Create an instance of the index API endpoint that we will test against.
77 // We will override the init() method because we don't want it to try to
78 // connect to a real document store.
79 IndexApi indexApi = new IndexApi(new SearchServiceApiHarness()) {
81 public void init() { /* do nothing */ }
84 //Construct an OperationResult instance with a success code and string.
85 OperationResult successResult = new OperationResult();
86 successResult.setResultCode(SUCCESS_RESULT_CODE);
87 successResult.setResult(SUCCESS_RESULT_STRING);
89 // Convert our success OperationResult to a standard REST Response...
90 Response successResponse = indexApi.responseFromOperationResult(successResult);
92 // ...and validate that the Response is correctly populated.
93 assertEquals("Unexpected result code", SUCCESS_RESULT_CODE, successResponse.getStatus());
94 assertTrue("Incorrect result string", ((String) successResponse.getEntity()).equals(SUCCESS_RESULT_STRING));
96 // Construct an OperationResult instance with an error code and failure
98 OperationResult failureResult = new OperationResult();
99 failureResult.setResultCode(FAILURE_RESULT_CODE);
100 failureResult.setFailureCause(FAILURE_CAUSE_STRING);
102 // Convert our failure OperationResult to a standard REST Response...
103 Response failureResponse = indexApi.responseFromOperationResult(failureResult);
105 // ...and validate that the Response is correctly populated.
106 assertEquals("Unexpected result code", FAILURE_RESULT_CODE, failureResponse.getStatus());
107 assertTrue("Incorrect result string", ((String) failureResponse.getEntity()).equals(FAILURE_CAUSE_STRING));
112 * This test validates the behaviour of the 'Create Index' POST request
115 * @throws IOException
118 public void createIndexTest() throws IOException {
120 String INDEX_NAME = "test-index";
121 String EXPECTED_SETTINGS =
124 + "{\"nGram_filter\": { "
125 + "\"type\": \"nGram\", "
126 + "\"min_gram\": 1, "
127 + "\"max_gram\": 50, "
128 + "\"token_chars\": [ \"letter\", \"digit\", \"punctuation\", \"symbol\" ]}},"
130 + "\"nGram_analyzer\": "
131 + "{\"type\": \"custom\","
132 + "\"tokenizer\": \"whitespace\","
133 + "\"filter\": [\"lowercase\",\"asciifolding\",\"nGram_filter\"]},"
134 + "\"whitespace_analyzer\": "
135 + "{\"type\": \"custom\","
136 + "\"tokenizer\": \"whitespace\","
137 + "\"filter\": [\"lowercase\",\"asciifolding\"]}}}}";
138 String EXPECTED_MAPPINGS =
140 + "\"serverName\": {"
141 + "\"type\": \"string\", "
142 + "\"index\": \"analyzed\", "
143 + "\"search_analyzer\": \"whitespace\"}, "
144 + "\"serverComplex\": {"
145 + "\"type\": \"string\", "
146 + "\"search_analyzer\": \"whitespace\"}}}";
148 // Read a valid document schema from a json file.
149 File schemaFile = new File(SIMPLE_DOC_SCHEMA_JSON);
150 String documentJson = TestUtils.readFileToString(schemaFile);
152 // Send a request to our 'create index' endpoint, using the schema
153 // which we just read.
154 String result = target(TOP_URI + INDEX_NAME).request().put(Entity.json(documentJson), String.class);
157 // Our stub document store DAO returns the parameters that it was
158 // passed as the result string, so now we can validate that our
159 // endpoint invoked it with the correct parameters.
160 String[] tokenizedResult = result.split("@");
161 assertTrue("Unexpected Index Name '" + tokenizedResult[0] + "' passed to doc store DAO",
162 tokenizedResult[0].equals(INDEX_NAME));
163 assertTrue("Unexpected settings string '" + tokenizedResult[1] + "' passed to doc store DAO",
164 tokenizedResult[1].equals(EXPECTED_SETTINGS));
165 assertTrue("Unexpected mappings string '" + tokenizedResult[2] + "' passed to doc store DAO",
166 tokenizedResult[2].equals(EXPECTED_MAPPINGS));
171 * This test validates that a 'create index' request with an improperly
172 * formatted document schema as the payload will result in an
173 * appropriate error being returned from the endpoint.
176 public void createIndexWithMangledSchemaTest() {
178 String INDEX_NAME = "test-index";
179 int BAD_REQUEST_CODE = 400;
181 String invalidSchemaString = "this is definitely not json!";
183 Response result = target(TOP_URI + INDEX_NAME).request().put(Entity.json(invalidSchemaString), Response.class);
185 assertEquals("Invalid document schema should result in a 400 error",
186 BAD_REQUEST_CODE, result.getStatus());
191 * This test validates the behaviour of the 'Delete Index' end point.
194 public void deleteIndexTest() {
196 String INDEX_NAME = "test-index";
198 // Send a request to the 'delete index' endpoint.
199 String result = target(TOP_URI + INDEX_NAME).request().delete(String.class);
201 // Validate that the expected parameters were passed to the document
203 assertTrue("Unexpected index name '" + result + "' passed to doc store DAO",
204 result.equals(INDEX_NAME));
209 * This test validates that attempting to delete an index which does not
210 * exist results in a 404 error.
213 public void deleteIndexDoesNotExistTest() {
215 int NOT_FOUND_CODE = 404;
217 // Send a request to the 'delete index' endpoint, specifying a
218 // non-existent index.
219 Response result = target(TOP_URI + StubEsController.DOES_NOT_EXIST_INDEX).request().delete(Response.class);
221 // Validate that a 404 error code is returned from the end point.
222 assertEquals("Deleting an index which does not exist should result in a 404 error",
223 NOT_FOUND_CODE, result.getStatus());