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 static org.junit.Assert.assertTrue;
24 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
29 import java.io.IOException;
30 //import org.glassfish.jersey.server.ResourceConfig;
31 //import org.glassfish.jersey.test.JerseyTest;
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.json.simple.parser.ParseException;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.http.MediaType;
42 import org.springframework.test.context.junit4.SpringRunner;
43 import org.springframework.test.web.servlet.MockMvc;
44 import org.springframework.test.web.servlet.MvcResult;
46 @RunWith(SpringRunner.class)
49 public class DocumentApiTest {
51 private static final String INDEXES_URI = "/test/indexes/";
52 private static final String DOCUMENT_URI = "documents/";
54 private static final String SEARCH_URI = "query/";
55 private static final String INDEX_NAME = "test-index";
56 private static final String DOC_ID = "test-1";
57 private static final String SIMPLE_QUERY = "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
58 private static final String COMPLEX_QUERY =
62 + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
66 + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
70 private static final String CREATE_JSON_CONTENT = "creation content";
73 private MockMvc mockMvc;
76 * This test validates the behaviour of the 'Create Document' POST request
80 * @throws ParseException
83 public void createDocumentTest() throws Exception {
85 MvcResult result = this.mockMvc.perform ( post ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI ).contentType ( MediaType.APPLICATION_JSON )
86 .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
88 // Our stub document store DAO returns the parameters that it was
89 // passed as the result string, so now we can validate that our
90 // endpoint invoked it with the correct parameters.
92 JSONParser parser = new JSONParser();
93 JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
95 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
99 * This test validates the behaviour of the 'Create Document' PUT request
102 * @throws IOException
103 * @throws ParseException
106 public void updateDocumentTest() throws Exception {
107 // WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
108 // Builder request = target.request().header("If-Match", "1");
109 // String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
111 MvcResult result = this.mockMvc.perform ( put ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID ).contentType ( MediaType.APPLICATION_JSON )
112 .header ( "If-Match", "1" ).content ( CREATE_JSON_CONTENT ) ).andReturn ( );
114 // Our stub document store DAO returns the parameters that it was
115 // passed as the result string, so now we can validate that our
116 // endpoint invoked it with the correct parameters.
117 JSONParser parser = new JSONParser();
118 JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
120 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
124 * This test validates the behaviour of the 'Get Document' GET request
127 * @throws IOException
128 * @throws ParseException
131 public void getDocumentTest() throws Exception {
132 // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
134 // MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID ) ).andReturn ();
135 MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
136 .contentType ( MediaType.APPLICATION_JSON )
137 .header ( "If-Match", "1" )
138 .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
141 // Our stub document store DAO returns the parameters that it was
142 // passed as the result string, so now we can validate that our
143 // endpoint invoked it with the correct parameters.
144 JSONParser parser = new JSONParser();
145 JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
147 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
152 // * This test validates the behaviour of the 'Delete Document' DELETE request
155 // * @throws IOException
156 // * @throws ParseException
159 public void deleteDocumentTest() throws Exception {
160 // WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
161 // Builder request = target.request().header("If-Match", "1");
162 // String result = request.delete(String.class);
163 MvcResult result = this.mockMvc.perform ( delete ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
164 .contentType ( MediaType.APPLICATION_JSON )
165 .header ( "If-Match", "1" )
166 .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
170 // Our stub document store DAO returns the parameters that it was
171 // passed as the result string, so now we can validate that our
172 // endpoint invoked it with the correct parameters.
173 assertTrue("Unexpected Result ", result.getResponse ().getContentAsString ().isEmpty ());
178 // * This test validates the behaviour of the 'Search Documents' GET request
181 // * @throws IOException
182 // * @throws ParseException
186 public void searchDocumentTest1() throws Exception {
187 // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
189 MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY )
190 .contentType ( MediaType.APPLICATION_JSON )
191 .header ( "If-Match", "1" )
192 .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
194 // Our stub document store DAO returns the parameters that it was
195 // passed as the result string, so now we can validate that our
196 // endpoint invoked it with the correct parameters.
197 JSONParser parser = new JSONParser();
198 JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
200 assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
206 * This test validates the behaviour of the 'Search Documents' GET request
209 * @throws IOException
210 * @throws ParseException
213 public void searchDocumentTest2() throws Exception {
214 // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
216 MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI)
217 .contentType ( MediaType.APPLICATION_JSON )
218 .content ( COMPLEX_QUERY ) ).andReturn ( );
220 // Our stub document store DAO returns the parameters that it was
221 // passed as the result string, so now we can validate that our
222 // endpoint invoked it with the correct parameters.
223 JSONParser parser = new JSONParser();
224 JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
225 JSONObject resultJson = (JSONObject) json.get("searchResult");
227 assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));