Initial search service commit
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / rest / DocumentApiTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.rest;
26
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;
34
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;
40
41 import static org.junit.Assert.assertTrue;
42
43 public class DocumentApiTest extends JerseyTest {
44
45   private static final String INDEXES_URI = "/test/indexes/";
46   private static final String DOCUMENT_URI = "documents/";
47
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 =
53       "{"
54           + "\"filter\": {"
55           + "\"all\": ["
56           + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
57           + "]"
58           + "},"
59           + "\"queries\": ["
60           + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
61           + "]"
62           + "}";
63
64   private static final String CREATE_JSON_CONTENT = "creation content";
65
66
67   @Override
68   protected Application configure() {
69
70     // Make sure that our test endpoint is on the resource path
71     // for Jersey Test.
72     return new ResourceConfig(SearchServiceApiHarness.class);
73   }
74
75   /**
76    * This test validates the behaviour of the 'Create Document' POST request
77    * endpoint.
78    *
79    * @throws IOException
80    * @throws ParseException
81    */
82   @Test
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);
85
86
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.
90
91     JSONParser parser = new JSONParser();
92     JSONObject json = (JSONObject) parser.parse(result);
93
94     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
95   }
96
97   /**
98    * This test validates the behaviour of the 'Create Document' PUT request
99    * endpoint.
100    *
101    * @throws IOException
102    * @throws ParseException
103    */
104   @Test
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);
109
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);
115
116     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
117   }
118
119   /**
120    * This test validates the behaviour of the 'Get Document' GET request
121    * endpoint.
122    *
123    * @throws IOException
124    * @throws ParseException
125    */
126   @Test
127   public void getDocumentTest() throws IOException, ParseException {
128     String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
129
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);
135
136     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
137
138   }
139
140   /**
141    * This test validates the behaviour of the 'Delete Document' DELETE request
142    * endpoint.
143    *
144    * @throws IOException
145    * @throws ParseException
146    */
147   @Test
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);
152
153
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());
158
159   }
160
161   /**
162    * This test validates the behaviour of the 'Search Documents' GET request
163    * endpoint.
164    *
165    * @throws IOException
166    * @throws ParseException
167    */
168   @Ignore
169   @Test
170   public void searchDocumentTest1() throws IOException, ParseException {
171     String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
172
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);
178
179     assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
180
181
182   }
183
184   /**
185    * This test validates the behaviour of the 'Search Documents' GET request
186    * endpoint.
187    *
188    * @throws IOException
189    * @throws ParseException
190    */
191   @Test
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);
194
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");
201
202     assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));
203
204   }
205
206 }