Fix file formatting issues
[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.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;
43
44 @RunWith(SpringRunner.class)
45 @SpringBootTest
46 @AutoConfigureMockMvc
47 public class DocumentApiTest {
48
49     private static final String INDEXES_URI = "/test/indexes/";
50     private static final String DOCUMENT_URI = "documents/";
51
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\"}}}" + "]" + "}";
60
61     private static final String CREATE_JSON_CONTENT = "creation content";
62
63     @Autowired
64     private MockMvc mockMvc;
65
66     /**
67      * This test validates the behaviour of the 'Create Document' POST request endpoint.
68      *
69      * @throws IOException
70      * @throws ParseException
71      */
72     @Test
73     public void createDocumentTest() throws Exception {
74
75         MvcResult result = this.mockMvc.perform(post(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI)
76                 .contentType(MediaType.APPLICATION_JSON).content(CREATE_JSON_CONTENT)).andReturn();
77
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.
81
82         JSONParser parser = new JSONParser();
83         JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
84
85         assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
86     }
87
88     /**
89      * This test validates the behaviour of the 'Create Document' PUT request endpoint.
90      *
91      * @throws IOException
92      * @throws ParseException
93      */
94     @Test
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);
99
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))
103                 .andReturn();
104
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());
110
111         assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
112     }
113
114     /**
115      * This test validates the behaviour of the 'Get Document' GET request endpoint.
116      *
117      * @throws IOException
118      * @throws ParseException
119      */
120     @Test
121     public void getDocumentTest() throws Exception {
122         // String result = target(INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID).request().get(String.class);
123
124         // MvcResult result = this.mockMvc.perform ( get ( INDEXES_URI + INDEX_NAME + "/" + DOCUMENT_URI + DOC_ID )
125         // ).andReturn ();
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))
129                 .andReturn();
130
131
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());
137
138         assertTrue("Unexpected Result ", !json.get("etag").toString().isEmpty());
139
140     }
141
142     //
143     // /**
144     // * This test validates the behaviour of the 'Delete Document' DELETE request
145     // * endpoint.
146     // *
147     // * @throws IOException
148     // * @throws ParseException
149     // */
150     @Test
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))
158                 .andReturn();
159
160
161
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());
166
167     }
168
169
170     /**
171      * This test validates the behaviour of the 'Search Documents' GET request endpoint.
172      *
173      * @throws Exception
174      */
175     @Ignore
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))
180                 .andReturn();
181
182         JSONParser parser = new JSONParser();
183         JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
184
185         assertTrue("Unexpected Result ", json.get("totalHits").toString().equals("1"));
186     }
187
188     /**
189      * This test validates the behaviour of the 'Search Documents' GET request endpoint.
190      *
191      * @throws IOException
192      * @throws ParseException
193      */
194     @Test
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();
198
199         JSONParser parser = new JSONParser();
200         JSONObject json = (JSONObject) parser.parse(result.getResponse().getContentAsString());
201         JSONObject resultJson = (JSONObject) json.get("searchResult");
202
203         assertTrue("Unexpected Result ", resultJson.get("totalHits").toString().equals("1"));
204     }
205
206 }