Renaming openecomp to onap
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / IndexApiTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sa.rest;
24
25
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;
32
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.core.Application;
35 import javax.ws.rs.core.Response;
36 import java.io.*;
37
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertTrue;
40
41
42 /**
43  * This suite of tests is intended to exercise the set of REST endpoints
44  * associated with manipulating Indexes in the document store.
45  */
46 public class IndexApiTest extends JerseyTest {
47
48   private final String TOP_URI = "/test/indexes/";
49   private final String SIMPLE_DOC_SCHEMA_JSON = "src/test/resources/json/simpleDocument.json";
50
51
52   @Override
53   protected Application configure() {
54
55     // Make sure that our test endpoint is on the resource path
56     // for Jersey Test.
57     return new ResourceConfig(SearchServiceApiHarness.class);
58   }
59
60
61   /**
62    * This test validates that the {@link IndexApi} is able to convert {@link OperationResult}
63    * obects to standard REST {@link Response} objects.
64    *
65    * @throws FileNotFoundException
66    * @throws IOException
67    * @throws DocumentStoreOperationException
68    */
69   @Test
70   public void responseFromOperationResultTest() throws FileNotFoundException, IOException, DocumentStoreOperationException {
71
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!";
76
77
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()) {
82       @Override
83       public void init() { /* do nothing */ }
84     };
85
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);
90
91     // Convert our success OperationResult to a standard REST Response...
92     Response successResponse = indexApi.responseFromOperationResult(successResult);
93
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));
97
98     // Construct an OperationResult instance with an error code and failure
99     // cause.
100     OperationResult failureResult = new OperationResult();
101     failureResult.setResultCode(FAILURE_RESULT_CODE);
102     failureResult.setFailureCause(FAILURE_CAUSE_STRING);
103
104     // Convert our failure OperationResult to a standard REST Response...
105     Response failureResponse = indexApi.responseFromOperationResult(failureResult);
106
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));
110   }
111
112
113   /**
114    * This test validates the behaviour of the 'Create Index' POST request
115    * endpoint.
116    *
117    * @throws IOException
118    */
119   @Test
120   public void createIndexTest() throws IOException {
121
122     String INDEX_NAME = "test-index";
123     String EXPECTED_SETTINGS =
124         "{\"analysis\": "
125             + "{\"filter\": "
126             + "{\"nGram_filter\": { "
127             + "\"type\": \"nGram\", "
128             + "\"min_gram\": 1, "
129             + "\"max_gram\": 50, "
130             + "\"token_chars\": [ \"letter\", \"digit\", \"punctuation\", \"symbol\" ]}},"
131             + "\"analyzer\": {"
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 =
141         "{\"properties\": {"
142             + "\"serverName\": {"
143             + "\"type\": \"string\", "
144             + "\"index\": \"analyzed\", "
145             + "\"search_analyzer\": \"whitespace\"}, "
146             + "\"serverComplex\": {"
147             + "\"type\": \"string\", "
148             + "\"search_analyzer\": \"whitespace\"}}}";
149
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);
153
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);
157
158
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));
169   }
170
171
172   /**
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.
176    */
177   @Test
178   public void createIndexWithMangledSchemaTest() {
179
180     String INDEX_NAME = "test-index";
181     int BAD_REQUEST_CODE = 400;
182
183     String invalidSchemaString = "this is definitely not json!";
184
185     Response result = target(TOP_URI + INDEX_NAME).request().put(Entity.json(invalidSchemaString), Response.class);
186
187     assertEquals("Invalid document schema should result in a 400 error",
188         BAD_REQUEST_CODE, result.getStatus());
189   }
190
191
192   /**
193    * This test validates the behaviour of the 'Delete Index' end point.
194    */
195   @Test
196   public void deleteIndexTest() {
197
198     String INDEX_NAME = "test-index";
199
200     // Send a request to the 'delete index' endpoint.
201     String result = target(TOP_URI + INDEX_NAME).request().delete(String.class);
202
203     // Validate that the expected parameters were passed to the document
204     // store DAO.
205     assertTrue("Unexpected index name '" + result + "' passed to doc store DAO",
206         result.equals(INDEX_NAME));
207   }
208
209
210   /**
211    * This test validates that attempting to delete an index which does not
212    * exist results in a 404 error.
213    */
214   @Test
215   public void deleteIndexDoesNotExistTest() {
216
217     int NOT_FOUND_CODE = 404;
218
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);
222
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());
226   }
227 }