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.json.simple.JSONObject;
31 import org.json.simple.parser.JSONParser;
32 import org.json.simple.parser.ParseException;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.http.MediaType;
40 import org.springframework.test.context.junit4.SpringRunner;
41 import org.springframework.test.web.servlet.MockMvc;
42 import org.springframework.test.web.servlet.MvcResult;
44 @RunWith(SpringRunner.class)
47 public class DocumentApiTest {
49 private static final String INDEXES_URI = "/test/indexes/";
50 private static final String DOCUMENT_URI = "documents/";
52 private static final String SEARCH_URI = "query/";
53 private static final String INDEX_NAME = "test-index";
54 private static final String DOC_ID = "test-1";
55 private static final String SIMPLE_QUERY =
56 "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
57 private static final String COMPLEX_QUERY = "{" + "\"filter\": {" + "\"all\": ["
58 + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}" + "]" + "}," + "\"queries\": ["
59 + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}" + "]" + "}";
61 private static final String CREATE_JSON_CONTENT = "creation content";
64 private MockMvc mockMvc;
67 * This test validates the behaviour of the 'Create Document' POST request endpoint.
70 * @throws ParseException
73 public void createDocumentTest() throws Exception {
75 MvcResult result = this.mockMvc.perform(post(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI)
76 .contentType(MediaType.APPLICATION_JSON).content(CREATE_JSON_CONTENT)).andReturn();
78 // Our stub document store DAO returns the parameters that it was
79 // passed as the result string, so now we can validate that our
80 // endpoint invoked it with the correct parameters.
82 JSONParser parser = new JSONParser();
83 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
85 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
89 * This test validates the behaviour of the 'Create Document' PUT request endpoint.
92 * @throws ParseException
95 public void updateDocumentTest() throws Exception {
96 // WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
97 // Builder request = target.request().header("If-Match", "1");
98 // String result = request.put(Entity.json(CREATE_JSON_CONTENT), String.class);
100 MvcResult result = this.mockMvc
101 .perform(put(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID)
102 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
105 // Our stub document store DAO returns the parameters that it was
106 // passed as the result string, so now we can validate that our
107 // endpoint invoked it with the correct parameters.
108 JSONParser parser = new JSONParser();
109 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
111 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
115 * This test validates the behaviour of the 'Get Document' GET request endpoint.
117 * @throws IOException
118 * @throws ParseException
121 public void getDocumentTest() throws Exception {
122 // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
124 // MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
126 MvcResult result = this.mockMvc
127 .perform(get(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID)
128 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
132 // Our stub document store DAO returns the parameters that it was
133 // passed as the result string, so now we can validate that our
134 // endpoint invoked it with the correct parameters.
135 JSONParser parser = new JSONParser();
136 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
138 assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
144 // * This test validates the behaviour of the 'Delete Document' DELETE request
147 // * @throws IOException
148 // * @throws ParseException
151 public void deleteDocumentTest() throws Exception {
152 // WebTarget target = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID);
153 // Builder request = target.request().header("If-Match", "1");
154 // String result = request.delete(String.class);
155 MvcResult result = this.mockMvc
156 .perform(delete(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID)
157 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
162 // Our stub document store DAO returns the parameters that it was
163 // passed as the result string, so now we can validate that our
164 // endpoint invoked it with the correct parameters.
165 assertTrue("Unexpected Result ", result.getResponse().getContentAsString().isEmpty());
171 * This test validates the behaviour of the 'Search Documents' GET request endpoint.
176 public void searchDocumentTest1() throws Exception {
177 MvcResult result = this.mockMvc
178 .perform(get(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY)
179 .contentType(MediaType.APPLICATION_JSON).header("If-Match", "1").content(CREATE_JSON_CONTENT))
182 JSONParser parser = new JSONParser();
183 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
185 assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
189 * This test validates the behaviour of the 'Search Documents' GET request endpoint.
191 * @throws IOException
192 * @throws ParseException
195 public void searchDocumentTest2() throws Exception {
196 MvcResult result = this.mockMvc.perform(get(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI)
197 .contentType(MediaType.APPLICATION_JSON).content(COMPLEX_QUERY)).andReturn();
199 JSONParser parser = new JSONParser();
200 JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
201 JSONObject resultJson = (JSONObject) json.get("searchResult");
203 assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));