Format Java code to ONAP standard
[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 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;
28
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;
45
46 @RunWith(SpringRunner.class)
47 @SpringBootTest
48 @AutoConfigureMockMvc
49 public class DocumentApiTest {
50
51     private static final String INDEXES_URI = "/test/indexes/";
52     private static final String DOCUMENT_URI = "documents/";
53
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\"}}}" + "]" + "}";
62
63     private static final String CREATE_JSON_CONTENT = "creation content";
64
65     @Autowired
66     private MockMvc mockMvc;
67
68     /**
69      * This test validates the behaviour of the 'Create Document' POST request endpoint.
70      *
71      * @throws IOException
72      * @throws ParseException
73      */
74     @Test
75     public void createDocumentTest() throws Exception {
76
77         MvcResult result = this.mockMvc.perform(post(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI)
78                 .contentType(MediaType.APPLICATION_JSON).content(CREATE_JSON_CONTENT)).andReturn();
79
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.
83
84         JSONParser parser = new JSONParser();
85         JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
86
87         assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
88     }
89
90     /**
91      * This test validates the behaviour of the 'Create Document' PUT request endpoint.
92      *
93      * @throws IOException
94      * @throws ParseException
95      */
96     @Test
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);
101
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))
105                 .andReturn();
106
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());
112
113         assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
114     }
115
116     /**
117      * This test validates the behaviour of the 'Get Document' GET request endpoint.
118      *
119      * @throws IOException
120      * @throws ParseException
121      */
122     @Test
123     public void getDocumentTest() throws Exception {
124         // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
125
126         // MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
127         // ).andReturn ();
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))
131                 .andReturn();
132
133
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());
139
140         assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
141
142     }
143
144     //
145     // /**
146     // * This test validates the behaviour of the 'Delete Document' DELETE request
147     // * endpoint.
148     // *
149     // * @throws IOException
150     // * @throws ParseException
151     // */
152     @Test
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))
160                 .andReturn();
161
162
163
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());
168
169     }
170
171     //
172     // /**
173     // * This test validates the behaviour of the 'Search Documents' GET request
174     // * endpoint.
175     // *
176     // * @throws IOException
177     // * @throws ParseException
178     // */
179     @Ignore
180     @Test
181     public void searchDocumentTest1() throws Exception {
182         // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI +
183         // SIMPLE_QUERY).request().get(String.class);
184
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))
188                 .andReturn();
189
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());
195
196         assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
197
198
199     }
200
201     //
202     /**
203      * This test validates the behaviour of the 'Search Documents' GET request endpoint.
204      *
205      * @throws IOException
206      * @throws ParseException
207      */
208     @Test
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);
212
213         MvcResult result = this.mockMvc.perform(get(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI)
214                 .contentType(MediaType.APPLICATION_JSON).content(COMPLEX_QUERY)).andReturn();
215
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");
222
223         assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));
224
225     }
226
227 }