ElkConnectorImpl implements ElkConnector { @Override public JestResult search(PolicyIndexType type, String text) throws IllegalStateException, IllegalArgumentException { if (LOGGER.isTraceEnabled()) { LOGGER.trace("ENTER: " + text); } if (text == null || text.isEmpty()) { throw new IllegalArgumentException("No search string provided"); } if (!isAlphaNumeric(text)) { throw new IllegalArgumentException("Search must be alpha numeric"); } QueryStringQueryBuilder mQ = QueryBuilders.queryStringQuery("*" + text + "*"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(mQ); Builder searchBuilder = new Search.Builder(searchSourceBuilder.toString()).addIndex(ELK_INDEX_POLICY) .setParameter(Parameters.SIZE, ElkConnectorImpl.QUERY_MAXRECORDS); if (type == null || type == PolicyIndexType.all) { for (PolicyIndexType pT : PolicyIndexType.values()) { if (pT != PolicyIndexType.all) { searchBuilder.addType(pT.toString()); } } } else { searchBuilder.addType(type.toString()); } Search search = searchBuilder.build(); JestResult result; try { result = jestClient.execute(search); } catch (IOException ioe) { LOGGER.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + ":" + search + ": " + ioe.getMessage(), ioe); throw new IllegalStateException(ioe); } if (result.isSucceeded()) { if (LOGGER.isInfoEnabled()) { LOGGER.info("OK:" + result.getResponseCode() + ":" + search + ": " + result.getPathToResult() + ":" + System.lineSeparator() + result.getJsonString()); } } else { if (LOGGER.isWarnEnabled()) { LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW + ":" + result.getResponseCode() + ": " + search.getURI() + ":" + result.getPathToResult() + ":" + result.getJsonString() + ":" + result.getErrorMessage()); } String errorMessage = result.getErrorMessage(); if (errorMessage != null && !errorMessage.isEmpty()) { String xMessage; if (errorMessage.contains("TokenMgrError")) { int indexError = errorMessage.lastIndexOf("TokenMgrError"); xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); } else if (errorMessage.contains("QueryParsingException")) { int indexError = errorMessage.lastIndexOf("QueryParsingException"); xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); } else if (errorMessage.contains("JsonParseException")) { int indexError = errorMessage.lastIndexOf("JsonParseException"); xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); } else if (errorMessage.contains("Parse Failure")) { int indexError = errorMessage.lastIndexOf("Parse Failure"); xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); } else if (errorMessage.contains("SearchParseException")) { int indexError = errorMessage.lastIndexOf("SearchParseException"); xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); } else { xMessage = result.getErrorMessage(); } throw new IllegalStateException(xMessage); } } return result; } ElkConnectorImpl(); @Override JestResult search(PolicyIndexType type, String text); @Override JestResult search(PolicyIndexType type, String text, Map<String, String> filter_s); boolean put(PolicyRestAdapter policyData); @Override boolean delete(PolicyRestAdapter policyData); @Override boolean update(PolicyRestAdapter policyData);  }
@Test public void searchTest() { JestResult r1 = null, r2 = null, r3 = null, r4 = null; try { r1 = new ElkConnectorImpl().search(PolicyIndexType.decision, "abc123"); } catch (Exception e) { if (e instanceof IllegalStateException) { return; } fail(); } try { r2 = new ElkConnectorImpl().search(PolicyIndexType.decision, "The_quick_brown_fox_jumps_over_the_lazy_dog"); } catch (Exception e) { fail(); } try { r3 = new ElkConnectorImpl().search(PolicyIndexType.decision, "abc123{}"); } catch (Exception e) { if (!(e instanceof IllegalArgumentException)) { fail(); } } try { r4 = new ElkConnectorImpl().search(PolicyIndexType.decision, "The quick brown fox jumps over the lazy dog"); } catch (Exception e) { if (!(e instanceof IllegalArgumentException)) { fail(); } } assertNotNull(r1); assertNotNull(r2); assertNull(r3); assertNull(r4); }
@Test public void testSearchWithFilter() { thrown.expect(IllegalStateException.class); ElkConnectorImpl impl = new ElkConnectorImpl(); impl.search(PolicyIndexType.config, "search", null); fail("Expected exception to be thrown"); }