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;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.glassfish.jersey.test.JerseyTest;
28 import org.junit.Test;
29 import org.onap.aai.sa.rest.IndexApi;
30 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
31 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.core.Application;
35 import javax.ws.rs.core.Response;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertTrue;
43 * This suite of tests is intended to exercise the set of REST endpoints
44 * associated with manipulating Indexes in the document store.
46 public class IndexApiTest extends JerseyTest {
48 private final String TOP_URI = "/test/indexes/";
49 private final String SIMPLE_DOC_SCHEMA_JSON = "src/test/resources/json/simpleDocument.json";
53 protected Application configure() {
55 // Make sure that our test endpoint is on the resource path
57 return new ResourceConfig(SearchServiceApiHarness.class);
62 * This test validates that the {@link IndexApi} is able to convert {@link OperationResult}
63 * obects to standard REST {@link Response} objects.
65 * @throws FileNotFoundException
67 * @throws DocumentStoreOperationException
70 public void responseFromOperationResultTest() throws FileNotFoundException, IOException, DocumentStoreOperationException {
72 int SUCCESS_RESULT_CODE = 200;
73 String SUCCESS_RESULT_STRING = "Everything is ay-okay!";
74 int FAILURE_RESULT_CODE = 500;
75 String FAILURE_CAUSE_STRING = "Something went wrong!";
78 // Create an instance of the index API endpoint that we will test against.
79 // We will override the init() method because we don't want it to try to
80 // connect to a real document store.
81 IndexApi indexApi = new IndexApi(new SearchServiceApiHarness()) {
83 public void init() { /* do nothing */ }
86 //Construct an OperationResult instance with a success code and string.
87 OperationResult successResult = new OperationResult();
88 successResult.setResultCode(SUCCESS_RESULT_CODE);
89 successResult.setResult(SUCCESS_RESULT_STRING);
91 // Convert our success OperationResult to a standard REST Response...
92 Response successResponse = indexApi.responseFromOperationResult(successResult);
94 // ...and validate that the Response is correctly populated.
95 assertEquals("Unexpected result code", SUCCESS_RESULT_CODE, successResponse.getStatus());
96 assertTrue("Incorrect result string", ((String) successResponse.getEntity()).equals(SUCCESS_RESULT_STRING));
98 // Construct an OperationResult instance with an error code and failure
100 OperationResult failureResult = new OperationResult();
101 failureResult.setResultCode(FAILURE_RESULT_CODE);
102 failureResult.setFailureCause(FAILURE_CAUSE_STRING);
104 // Convert our failure OperationResult to a standard REST Response...
105 Response failureResponse = indexApi.responseFromOperationResult(failureResult);
107 // ...and validate that the Response is correctly populated.
108 assertEquals("Unexpected result code", FAILURE_RESULT_CODE, failureResponse.getStatus());
109 assertTrue("Incorrect result string", ((String) failureResponse.getEntity()).equals(FAILURE_CAUSE_STRING));
114 * This test validates the behaviour of the 'Create Index' POST request
117 * @throws IOException
120 public void createIndexTest() throws IOException {
122 String INDEX_NAME = "test-index";
123 String EXPECTED_SETTINGS =
126 + "{\"nGram_filter\": { "
127 + "\"type\": \"nGram\", "
128 + "\"min_gram\": 1, "
129 + "\"max_gram\": 50, "
130 + "\"token_chars\": [ \"letter\", \"digit\", \"punctuation\", \"symbol\" ]}},"
132 + "\"nGram_analyzer\": "
133 + "{\"type\": \"custom\","
134 + "\"tokenizer\": \"whitespace\","
135 + "\"filter\": [\"lowercase\",\"asciifolding\",\"nGram_filter\"]},"
136 + "\"whitespace_analyzer\": "
137 + "{\"type\": \"custom\","
138 + "\"tokenizer\": \"whitespace\","
139 + "\"filter\": [\"lowercase\",\"asciifolding\"]}}}}";
140 String EXPECTED_MAPPINGS =
142 + "\"serverName\": {"
143 + "\"type\": \"string\", "
144 + "\"index\": \"analyzed\", "
145 + "\"search_analyzer\": \"whitespace\"}, "
146 + "\"serverComplex\": {"
147 + "\"type\": \"string\", "
148 + "\"search_analyzer\": \"whitespace\"}}}";
150 // Read a valid document schema from a json file.
151 File schemaFile = new File(SIMPLE_DOC_SCHEMA_JSON);
152 String documentJson = TestUtils.readFileToString(schemaFile);
154 // Send a request to our 'create index' endpoint, using the schema
155 // which we just read.
156 String result = target(TOP_URI + INDEX_NAME).request().put(Entity.json(documentJson), String.class);
159 // Our stub document store DAO returns the parameters that it was
160 // passed as the result string, so now we can validate that our
161 // endpoint invoked it with the correct parameters.
162 String[] tokenizedResult = result.split("@");
163 assertTrue("Unexpected Index Name '" + tokenizedResult[0] + "' passed to doc store DAO",
164 tokenizedResult[0].equals(INDEX_NAME));
165 assertTrue("Unexpected settings string '" + tokenizedResult[1] + "' passed to doc store DAO",
166 tokenizedResult[1].equals(EXPECTED_SETTINGS));
167 assertTrue("Unexpected mappings string '" + tokenizedResult[2] + "' passed to doc store DAO",
168 tokenizedResult[2].equals(EXPECTED_MAPPINGS));
173 * This test validates that a 'create index' request with an improperly
174 * formatted document schema as the payload will result in an
175 * appropriate error being returned from the endpoint.
178 public void createIndexWithMangledSchemaTest() {
180 String INDEX_NAME = "test-index";
181 int BAD_REQUEST_CODE = 400;
183 String invalidSchemaString = "this is definitely not json!";
185 Response result = target(TOP_URI + INDEX_NAME).request().put(Entity.json(invalidSchemaString), Response.class);
187 assertEquals("Invalid document schema should result in a 400 error",
188 BAD_REQUEST_CODE, result.getStatus());
193 * This test validates the behaviour of the 'Delete Index' end point.
196 public void deleteIndexTest() {
198 String INDEX_NAME = "test-index";
200 // Send a request to the 'delete index' endpoint.
201 String result = target(TOP_URI + INDEX_NAME).request().delete(String.class);
203 // Validate that the expected parameters were passed to the document
205 assertTrue("Unexpected index name '" + result + "' passed to doc store DAO",
206 result.equals(INDEX_NAME));
211 * This test validates that attempting to delete an index which does not
212 * exist results in a 404 error.
215 public void deleteIndexDoesNotExistTest() {
217 int NOT_FOUND_CODE = 404;
219 // Send a request to the 'delete index' endpoint, specifying a
220 // non-existent index.
221 Response result = target(TOP_URI + StubEsController.DOES_NOT_EXIST_INDEX).request().delete(Response.class);
223 // Validate that a 404 error code is returned from the end point.
224 assertEquals("Deleting an index which does not exist should result in a 404 error",
225 NOT_FOUND_CODE, result.getStatus());