Organise imports to ONAP Java standards
[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 = "\"parsed-query\": {\"my-field\": \"something\", \"query-string\": \"string\"}";
58   private static final String COMPLEX_QUERY =
59       "{"
60           + "\"filter\": {"
61           + "\"all\": ["
62           + "{\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}"
63           + "]"
64           + "},"
65           + "\"queries\": ["
66           + "{\"may\": {\"parsed-query\": {\"field\": \"searchTags\", \"query-string\": \"b\"}}}"
67           + "]"
68           + "}";
69
70   private static final String CREATE_JSON_CONTENT = "creation content";
71
72     @Autowired
73     private MockMvc mockMvc;
74
75   /**
76    * This test validates the behaviour of the 'Create Document' POST request
77    * endpoint.
78    *
79    * @throws IOException
80    * @throws ParseException
81    */
82   @Test
83   public void createDocumentTest() throws Exception {
84
85       MvcResult result = this.mockMvc.perform ( post ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI ).contentType ( MediaType.APPLICATION_JSON )
86               .content ( CREATE_JSON_CONTENT ) ).andReturn ( );
87
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.
91
92     JSONParser parser = new JSONParser();
93     JSONObject json = (JSONObject) parser.parse(result.getResponse ().getContentAsString ());
94
95     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
96   }
97
98   /**
99    * This test validates the behaviour of the 'Create Document' PUT request
100    * endpoint.
101    *
102    * @throws IOException
103    * @throws ParseException
104    */
105   @Test
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);
110
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 ( );
113
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 ());
119
120     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
121   }
122
123   /**
124    * This test validates the behaviour of the 'Get Document' GET request
125    * endpoint.
126    *
127    * @throws IOException
128    * @throws ParseException
129    */
130   @Test
131   public void getDocumentTest() throws Exception {
132     // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
133
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 ( );
139
140
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 ());
146
147     assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
148
149   }
150 //
151 //  /**
152 //   * This test validates the behaviour of the 'Delete Document' DELETE request
153 //   * endpoint.
154 //   *
155 //   * @throws IOException
156 //   * @throws ParseException
157 //   */
158   @Test
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 ( );
167
168
169
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 ());
174
175   }
176 //
177 //  /**
178 //   * This test validates the behaviour of the 'Search Documents' GET request
179 //   * endpoint.
180 //   *
181 //   * @throws IOException
182 //   * @throws ParseException
183 //   */
184   @Ignore
185   @Test
186   public void searchDocumentTest1() throws Exception {
187     // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI + SIMPLE_QUERY).request().get(String.class);
188
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 ( );
193
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 ());
199
200     assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
201
202
203   }
204 //
205   /**
206    * This test validates the behaviour of the 'Search Documents' GET request
207    * endpoint.
208    *
209    * @throws IOException
210    * @throws ParseException
211    */
212   @Test
213   public void searchDocumentTest2() throws Exception {
214     // String result = target(INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI).request().post(Entity.json(COMPLEX_QUERY), String.class);
215
216       MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + SEARCH_URI)
217               .contentType ( MediaType.APPLICATION_JSON )
218               .content ( COMPLEX_QUERY ) ).andReturn ( );
219
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");
226
227     assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));
228
229   }
230
231 }