b11e60b8e001a2c4b842a5eb06c3b9776946e7b4
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / DocumentApiTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.sa.rest;
22
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.glassfish.jersey.test.JerseyTest;
25 import org.json.simple.JSONObject;
26 import org.json.simple.parser.JSONParser;
27 import org.json.simple.parser.ParseException;
28 import org.junit.Ignore;
29 import org.junit.Test;
30
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.client.Invocation.Builder;
33 import javax.ws.rs.client.WebTarget;
34 import javax.ws.rs.core.Application;
35 import java.io.IOException;
36
37 import static org.junit.Assert.assertTrue;
38
39 public class DocumentApiTest extends JerseyTest {
40
41   private static final String INDEXES_URI = "/test/indexes/";
42   private static final String DOCUMENT_URI = "documents/";
43
44   private static final String SEARCH_URI = "query/";
45   private static final String INDEX_NAME = "test-index";
46   private static final String DOC_ID = "test-1";
47   private static final String SIMPLE_QUERY = "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
48   private static final String COMPLEX_QUERY =
49       "{"
50           + "\"filter\": {"
51           + "\"all\": ["
52           + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
53           + "]"
54           + "},"
55           + "\"queries\": ["
56           + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
57           + "]"
58           + "}";
59
60   private static final String CREATE_JSON_CONTENT = "creation content";
61
62
63   @Override
64   protected Application configure() {
65
66     // Make sure that our test endpoint is on the resource path
67     // for Jersey Test.
68     return new ResourceConfig(SearchServiceApiHarness.class);
69   }
70
71   /**
72    * This test validates the behaviour of the 'Create Document' POST request
73    * endpoint.
74    *
75    * @throws IOException
76    * @throws ParseException
77    */
78   @Test
79   public void createDocumentTest() throws IOException, ParseException {
80     String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI).request().post(Entity.json(CREATE_JSON_CONTENT), String.class);
81
82
83     // Our stub document store DAO returns the parameters that it was
84     // passed as the result string, so now we can validate that our
85     // endpoint invoked it with the correct parameters.
86
87     JSONParser parser = new JSONParser();
88     JSONObject json = (JSONObject) parser.parse(result);
89
90     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
91   }
92
93   /**
94    * This test validates the behaviour of the 'Create Document' PUT request
95    * endpoint.
96    *
97    * @throws IOException
98    * @throws ParseException
99    */
100   @Test
101   public void updateDocumentTest() throws IOException, ParseException {
102     WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
103     Builder request = target.request().header("If-Match", "1");
104     String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
105
106     // Our stub document store DAO returns the parameters that it was
107     // passed as the result string, so now we can validate that our
108     // endpoint invoked it with the correct parameters.
109     JSONParser parser = new JSONParser();
110     JSONObject json = (JSONObject) parser.parse(result);
111
112     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
113   }
114
115   /**
116    * This test validates the behaviour of the 'Get Document' GET request
117    * endpoint.
118    *
119    * @throws IOException
120    * @throws ParseException
121    */
122   @Test
123   public void getDocumentTest() throws IOException, ParseException {
124     String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
125
126     // Our stub document store DAO returns the parameters that it was
127     // passed as the result string, so now we can validate that our
128     // endpoint invoked it with the correct parameters.
129     JSONParser parser = new JSONParser();
130     JSONObject json = (JSONObject) parser.parse(result);
131
132     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
133
134   }
135
136   /**
137    * This test validates the behaviour of the 'Delete Document' DELETE request
138    * endpoint.
139    *
140    * @throws IOException
141    * @throws ParseException
142    */
143   @Test
144   public void deleteDocumentTest() throws IOException, ParseException {
145     WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
146     Builder request = target.request().header("If-Match", "1");
147     String result = request.delete(String.class);
148
149
150     // Our stub document store DAO returns the parameters that it was
151     // passed as the result string, so now we can validate that our
152     // endpoint invoked it with the correct parameters.
153     assertTrue("Unexpected Result ", result.isEmpty());
154
155   }
156
157   /**
158    * This test validates the behaviour of the 'Search Documents' GET request
159    * endpoint.
160    *
161    * @throws IOException
162    * @throws ParseException
163    */
164   @Ignore
165   @Test
166   public void searchDocumentTest1() throws IOException, ParseException {
167     String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
168
169     // Our stub document store DAO returns the parameters that it was
170     // passed as the result string, so now we can validate that our
171     // endpoint invoked it with the correct parameters.
172     JSONParser parser = new JSONParser();
173     JSONObject json = (JSONObject) parser.parse(result);
174
175     assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
176
177
178   }
179
180   /**
181    * This test validates the behaviour of the 'Search Documents' GET request
182    * endpoint.
183    *
184    * @throws IOException
185    * @throws ParseException
186    */
187   @Test
188   public void searchDocumentTest2() throws IOException, ParseException {
189     String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
190
191     // Our stub document store DAO returns the parameters that it was
192     // passed as the result string, so now we can validate that our
193     // endpoint invoked it with the correct parameters.
194     JSONParser parser = new JSONParser();
195     JSONObject json = (JSONObject) parser.parse(result);
196     JSONObject resultJson = (JSONObject) json.get("searchResult");
197
198     assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));
199
200   }
201
202 }