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