View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.sql;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertNull;
15  
16  import org.junit.jupiter.api.Test;
17  import org.springframework.mock.web.MockHttpServletRequest;
18  import org.springframework.mock.web.MockHttpServletResponse;
19  
20  import psiprobe.model.sql.DataSourceTestInfo;
21  
22  class QueryHistoryItemControllerTest {
23  
24    @Test
25    void handleRequestWritesRequestedSqlHistoryItem() throws Exception {
26      QueryHistoryItemController controller = new QueryHistoryItemController();
27      MockHttpServletRequest request =
28          new MockHttpServletRequest("GET", "/sql/queryHistoryItem.ajax");
29      request.addParameter("sqlId", "1");
30  
31      DataSourceTestInfo info = new DataSourceTestInfo();
32      info.setHistorySize(-1);
33      info.addQueryToHistory("select 1");
34      info.addQueryToHistory("select 2");
35      request.getSession(true).setAttribute(DataSourceTestInfo.DS_TEST_SESS_ATTR, info);
36  
37      MockHttpServletResponse response = new MockHttpServletResponse();
38  
39      assertNull(controller.handleRequest(request, response));
40      assertEquals("UTF-8", response.getCharacterEncoding());
41      assertEquals("select 1", response.getContentAsString());
42    }
43  
44    @Test
45    void handleRequestIgnoresMissingSessionDataAndOutOfBoundsHistoryIndex() throws Exception {
46      QueryHistoryItemController controller = new QueryHistoryItemController();
47      MockHttpServletRequest request =
48          new MockHttpServletRequest("GET", "/sql/queryHistoryItem.ajax");
49      request.addParameter("sqlId", "5");
50  
51      DataSourceTestInfo info = new DataSourceTestInfo();
52      info.setHistorySize(-1);
53      info.addQueryToHistory("select 1");
54      request.getSession(true).setAttribute(DataSourceTestInfo.DS_TEST_SESS_ATTR, info);
55  
56      MockHttpServletResponse response = new MockHttpServletResponse();
57  
58      assertNull(controller.handleRequest(request, response));
59      assertEquals("", response.getContentAsString());
60    }
61  }