49a10df72220369a3e491e95fa84cb53c788bb55
[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 import org.junit.runner.RunWith;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.http.MediaType;
35 import org.springframework.test.context.junit4.SpringRunner;
36 import org.springframework.test.web.servlet.MockMvc;
37 import org.springframework.test.web.servlet.MvcResult;
38
39 import javax.ws.rs.client.Entity;
40 import javax.ws.rs.client.Invocation.Builder;
41 import javax.ws.rs.client.WebTarget;
42 import javax.ws.rs.core.Application;
43 import java.io.IOException;
44
45 import static org.junit.Assert.assertTrue;
46
47 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
48 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
49 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
50 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
51 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
52 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
53 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
54
55 @RunWith(SpringRunner.class)
56 @SpringBootTest
57 @AutoConfigureMockMvc
58 public class DocumentApiTest  {
59
60   private static final String INDEXES_URI = "/test/indexes/";
61   private static final String DOCUMENT_URI = "documents/";
62
63   private static final String SEARCH_URI = "query/";
64   private static final String INDEX_NAME = "test-index";
65   private static final String DOC_ID = "test-1";
66   private static final String SIMPLE_QUERY = "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
67   private static final String COMPLEX_QUERY =
68       "{"
69           + "\"filter\": {"
70           + "\"all\": ["
71           + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
72           + "]"
73           + "},"
74           + "\"queries\": ["
75           + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
76           + "]"
77           + "}";
78
79   private static final String CREATE_JSON_CONTENT = "creation content";
80
81     @Autowired
82     private MockMvc mockMvc;
83
84   /**
85    * This test validates the behaviour of the 'Create Document' POST request
86    * endpoint.
87    *
88    * @throws IOException
89    * @throws ParseException
90    */
91   @Test
92   public void createDocumentTest() throws Exception {
93
94       MvcResult result = this.mockMvc.perform ( post ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI ).contentType ( MediaType.APPLICATION_JSON )
95               .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
96
97     // Our stub document store DAO returns the parameters that it was
98     // passed as the result string, so now we can validate that our
99     // endpoint invoked it with the correct parameters.
100
101     JSONParser parser = new JSONParser();
102     JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
103
104     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
105   }
106
107   /**
108    * This test validates the behaviour of the 'Create Document' PUT request
109    * endpoint.
110    *
111    * @throws IOException
112    * @throws ParseException
113    */
114   @Test
115   public void updateDocumentTest() throws Exception {
116     // WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
117     // Builder request = target.request().header("If-Match", "1");
118     // String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
119
120       MvcResult result = this.mockMvc.perform ( put ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID ).contentType ( MediaType.APPLICATION_JSON )
121               .header ( "If-Match", "1" ).content ( CREATE_JSON_CONTENT ) ).andReturn ( );
122
123     // Our stub document store DAO returns the parameters that it was
124     // passed as the result string, so now we can validate that our
125     // endpoint invoked it with the correct parameters.
126     JSONParser parser = new JSONParser();
127     JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
128
129     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
130   }
131
132   /**
133    * This test validates the behaviour of the 'Get Document' GET request
134    * endpoint.
135    *
136    * @throws IOException
137    * @throws ParseException
138    */
139   @Test
140   public void getDocumentTest() throws Exception {
141     // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
142
143       // MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID ) ).andReturn ();
144       MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
145               .contentType ( MediaType.APPLICATION_JSON )
146               .header ( "If-Match", "1" )
147               .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
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     JSONParser parser = new JSONParser();
154     JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
155
156     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
157
158   }
159 //
160 //  /**
161 //   * This test validates the behaviour of the 'Delete Document' DELETE request
162 //   * endpoint.
163 //   *
164 //   * @throws IOException
165 //   * @throws ParseException
166 //   */
167   @Test
168   public void deleteDocumentTest() throws Exception {
169 //    WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
170 //    Builder request = target.request().header("If-Match", "1");
171 //    String result = request.delete(String.class);
172       MvcResult result = this.mockMvc.perform ( delete ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
173               .contentType ( MediaType.APPLICATION_JSON )
174               .header ( "If-Match", "1" )
175               .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
176
177
178
179     // Our stub document store DAO returns the parameters that it was
180     // passed as the result string, so now we can validate that our
181     // endpoint invoked it with the correct parameters.
182     assertTrue("Unexpected Result ", result.getResponse ().getContentAsString ().isEmpty ());
183
184   }
185 //
186 //  /**
187 //   * This test validates the behaviour of the 'Search Documents' GET request
188 //   * endpoint.
189 //   *
190 //   * @throws IOException
191 //   * @throws ParseException
192 //   */
193   @Ignore
194   @Test
195   public void searchDocumentTest1() throws Exception {
196     // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
197
198       MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY )
199               .contentType ( MediaType.APPLICATION_JSON )
200               .header ( "If-Match", "1" )
201               .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
202
203     // Our stub document store DAO returns the parameters that it was
204     // passed as the result string, so now we can validate that our
205     // endpoint invoked it with the correct parameters.
206     JSONParser parser = new JSONParser();
207     JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
208
209     assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
210
211
212   }
213 //
214   /**
215    * This test validates the behaviour of the 'Search Documents' GET request
216    * endpoint.
217    *
218    * @throws IOException
219    * @throws ParseException
220    */
221   @Test
222   public void searchDocumentTest2() throws Exception {
223     // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
224
225       MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI)
226               .contentType ( MediaType.APPLICATION_JSON )
227               .content ( COMPLEX_QUERY ) ).andReturn ( );
228
229     // Our stub document store DAO returns the parameters that it was
230     // passed as the result string, so now we can validate that our
231     // endpoint invoked it with the correct parameters.
232     JSONParser parser = new JSONParser();
233     JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
234     JSONObject resultJson = (JSONObject) json.get("searchResult");
235
236     assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));
237
238   }
239
240 }