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 =
58 "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
59 private static final String COMPLEX_QUERY = "{" + "\"filter\": {" + "\"all\": ["
60 + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}" + "]" + "}," + "\"queries\": ["
61 + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}" + "]" + "}";
63 private static final String CREATE_JSON_CONTENT = "creation content";
66 private MockMvc mockMvc;
69 * This test validates the behaviour of the 'Create Document' POST request endpoint.
72 * @throws ParseException
75 public void createDocumentTest() throws Exception {
77 MvcResult result = this.mockMvc.perform(post(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI)
78 .contentType(MediaType.APPLICATION_JSON).content(CREATE_JSON_CONTENT)).andReturn();
80 // Our stub document store DAO returns the parameters that it was
81 // passed as the result string, so now we can validate that our
82 // endpoint invoked it with the correct parameters.
84 JSONParser parser = new JSONParser();
85 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
87 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
91 * This test validates the behaviour of the 'Create Document' PUT request endpoint.
94 * @throws ParseException
97 public void updateDocumentTest() throws Exception {
98 // WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
99 // Builder request = target.request().header("If-Match", "1");
100 // String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
102 MvcResult result = this.mockMvc
103 .perform(put(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID)
104 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
107 // Our stub document store DAO returns the parameters that it was
108 // passed as the result string, so now we can validate that our
109 // endpoint invoked it with the correct parameters.
110 JSONParser parser = new JSONParser();
111 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
113 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
117 * This test validates the behaviour of the 'Get Document' GET request endpoint.
119 * @throws IOException
120 * @throws ParseException
123 public void getDocumentTest() throws Exception {
124 // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
126 // MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
128 MvcResult result = this.mockMvc
129 .perform(get(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID)
130 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
134 // Our stub document store DAO returns the parameters that it was
135 // passed as the result string, so now we can validate that our
136 // endpoint invoked it with the correct parameters.
137 JSONParser parser = new JSONParser();
138 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
140 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
146 // * This test validates the behaviour of the 'Delete Document' DELETE request
149 // * @throws IOException
150 // * @throws ParseException
153 public void deleteDocumentTest() throws Exception {
154 // WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
155 // Builder request = target.request().header("If-Match", "1");
156 // String result = request.delete(String.class);
157 MvcResult result = this.mockMvc
158 .perform(delete(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID)
159 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
164 // Our stub document store DAO returns the parameters that it was
165 // passed as the result string, so now we can validate that our
166 // endpoint invoked it with the correct parameters.
167 assertTrue("Unexpected Result ", result.getResponse().getContentAsString().isEmpty());
173 // * This test validates the behaviour of the 'Search Documents' GET request
176 // * @throws IOException
177 // * @throws ParseException
181 public void searchDocumentTest1() throws Exception {
182 // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI +
183 // SIMPLE_QUERY).request().get(String.class);
185 MvcResult result = this.mockMvc
186 .perform(get(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY)
187 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
190 // Our stub document store DAO returns the parameters that it was
191 // passed as the result string, so now we can validate that our
192 // endpoint invoked it with the correct parameters.
193 JSONParser parser = new JSONParser();
194 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
196 assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
203 * This test validates the behaviour of the 'Search Documents' GET request endpoint.
205 * @throws IOException
206 * @throws ParseException
209 public void searchDocumentTest2() throws Exception {
210 // String result = target(INDEXES_URI + INDEX_NAME + "/" +
211 // SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
213 MvcResult result = this.mockMvc.perform(get(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI)
214 .contentType(MediaType.APPLICATION_JSON).content(COMPLEX_QUERY)).andReturn();
216 // Our stub document store DAO returns the parameters that it was
217 // passed as the result string, so now we can validate that our
218 // endpoint invoked it with the correct parameters.
219 JSONParser parser = new JSONParser();
220 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
221 JSONObject resultJson = (JSONObject) json.get("searchResult");
223 assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));