SlackSlashCommand implements HttpFunction { @Override public void service(HttpRequest request, HttpResponse response) throws IOException { if (!"POST".equals(request.getMethod())) { response.setStatusCode(HttpURLConnection.HTTP_BAD_METHOD); return; } String bodyString = request.getReader().lines().collect(Collectors.joining()); Map<String, String> body = new HashMap<>(); for (String keyValuePair : bodyString.split("&")) { String[] keyAndValue = keyValuePair.split("="); if (keyAndValue.length == 2) { String key = keyAndValue[0]; String value = keyAndValue[1]; body.put(key, value); } } if (body == null || !body.containsKey("text")) { response.setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST); return; } if (!isValidSlackWebhook(request, bodyString)) { response.setStatusCode(HttpURLConnection.HTTP_UNAUTHORIZED); return; } String query = body.get("text"); JsonObject kgResponse = searchKnowledgeGraph(query); BufferedWriter writer = response.getWriter(); writer.write(formatSlackMessage(kgResponse, query)); response.setContentType("application/json"); } SlackSlashCommand();   SlackSlashCommand(SlackSignature.Verifier verifier);   SlackSlashCommand(SlackSignature.Verifier verifier, String apiKey); @Override void service(HttpRequest request, HttpResponse response);  }
@Test public void onlyAcceptsPostRequestsTest() throws IOException, GeneralSecurityException { when(request.getMethod()).thenReturn("GET"); new SlackSlashCommand().service(request, response); writerOut.flush(); verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_METHOD); }
@Test public void requiresSlackAuthHeadersTest() throws IOException, GeneralSecurityException { String urlEncodedStr = "text=foo"; StringReader requestReadable = new StringReader(urlEncodedStr); when(request.getMethod()).thenReturn("POST"); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); new SlackSlashCommand().service(request, response); verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_UNAUTHORIZED); }
@Test public void recognizesValidSlackTokenTest() throws IOException, GeneralSecurityException { StringReader requestReadable = new StringReader(""); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); new SlackSlashCommand().service(request, response); verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST); }
@Test public void handlesSearchErrorTest() throws IOException, GeneralSecurityException { String urlEncodedStr = "text=foo"; StringReader requestReadable = new StringReader(urlEncodedStr); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); SlackSlashCommand functionInstance = new SlackSlashCommand(alwaysValidVerifier, "gibberish"); assertThrows( GoogleJsonResponseException.class, () -> functionInstance.service(request, response)); }
@Test public void handlesEmptyKgResultsTest() throws IOException, GeneralSecurityException { String urlEncodedStr = "text=asdfjkl13579"; StringReader requestReadable = new StringReader(urlEncodedStr); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); SlackSlashCommand functionInstance = new SlackSlashCommand(alwaysValidVerifier); functionInstance.service(request, response); writerOut.flush(); assertThat(responseOut.toString()).contains("No results match your query..."); }
@Test public void handlesPopulatedKgResultsTest() throws IOException, GeneralSecurityException { String urlEncodedStr = "text=lion"; StringReader requestReadable = new StringReader(urlEncodedStr); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); SlackSlashCommand functionInstance = new SlackSlashCommand(alwaysValidVerifier); functionInstance.service(request, response); writerOut.flush(); assertThat(responseOut.toString()).contains("https: }
@Test public void handlesMultipleUrlParamsTest() throws IOException, GeneralSecurityException { String urlEncodedStr = "unused=foo&text=lion"; StringReader requestReadable = new StringReader(urlEncodedStr); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); SlackSlashCommand functionInstance = new SlackSlashCommand(alwaysValidVerifier); functionInstance.service(request, response); writerOut.flush(); assertThat(responseOut.toString()).contains("https: }