2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.rest;
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;
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;
45 import static org.junit.Assert.assertTrue;
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;
55 @RunWith(SpringRunner.class)
58 public class DocumentApiTest {
60 private static final String INDEXES_URI = "/test/indexes/";
61 private static final String DOCUMENT_URI = "documents/";
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 =
71 + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
75 + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
79 private static final String CREATE_JSON_CONTENT = "creation content";
82 private MockMvc mockMvc;
85 * This test validates the behaviour of the 'Create Document' POST request
89 * @throws ParseException
92 public void createDocumentTest() throws Exception {
94 MvcResult result = this.mockMvc.perform ( post ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI ).contentType ( MediaType.APPLICATION_JSON )
95 .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
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.
101 JSONParser parser = new JSONParser();
102 JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
104 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
108 * This test validates the behaviour of the 'Create Document' PUT request
111 * @throws IOException
112 * @throws ParseException
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);
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 ( );
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 ());
129 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
133 * This test validates the behaviour of the 'Get Document' GET request
136 * @throws IOException
137 * @throws ParseException
140 public void getDocumentTest() throws Exception {
141 // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
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 ( );
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 ());
156 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
161 // * This test validates the behaviour of the 'Delete Document' DELETE request
164 // * @throws IOException
165 // * @throws ParseException
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 ( );
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 ());
187 // * This test validates the behaviour of the 'Search Documents' GET request
190 // * @throws IOException
191 // * @throws ParseException
195 public void searchDocumentTest1() throws Exception {
196 // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
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 ( );
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 ());
209 assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
215 * This test validates the behaviour of the 'Search Documents' GET request
218 * @throws IOException
219 * @throws ParseException
222 public void searchDocumentTest2() throws Exception {
223 // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
225 MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI)
226 .contentType ( MediaType.APPLICATION_JSON )
227 .content ( COMPLEX_QUERY ) ).andReturn ( );
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");
236 assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));