NiFiClient { public List<String> getResources(ResourceLookupContext context) throws Exception { final WebResource resource = getWebResource(); final ClientResponse response = getResponse(resource, "application/json"); if (Response.Status.OK.getStatusCode() != response.getStatus()) { String errorMsg = IOUtils.toString(response.getEntityInputStream()); throw new Exception("Unable to retrieve resources from NiFi due to: " + errorMsg); } JsonNode rootNode = mapper.readTree(response.getEntityInputStream()); if (rootNode == null) { throw new Exception("Unable to retrieve resources from NiFi"); } JsonNode resourcesNode = rootNode.findValue("resources"); List<String> identifiers = resourcesNode.findValuesAsText("identifier"); final String userInput = context.getUserInput(); if (StringUtils.isBlank(userInput)) { return identifiers; } else { List<String> filteredIdentifiers = new ArrayList<>(); for (String identifier : identifiers) { if (identifier.contains(userInput)) { filteredIdentifiers.add(identifier); } } return filteredIdentifiers; } } NiFiClient(final String url, final SSLContext sslContext); HashMap<String, Object> connectionTest(); List<String> getResources(ResourceLookupContext context); String getUrl(); SSLContext getSslContext(); HostnameVerifier getHostnameVerifier();  }
@Test public void testGetResourcesNoUserInput() throws Exception { ResourceLookupContext resourceLookupContext = Mockito.mock(ResourceLookupContext.class); when(resourceLookupContext.getUserInput()).thenReturn(""); final List<String> expectedResources = new ArrayList<>(); expectedResources.add("/system"); expectedResources.add("/controller"); expectedResources.add("/flow"); expectedResources.add("/provenance"); expectedResources.add("/proxy"); expectedResources.add("/resources"); List<String> resources = niFiClient.getResources(resourceLookupContext); Assert.assertNotNull(resources); Assert.assertEquals(expectedResources.size(), resources.size()); resources.removeAll(expectedResources); Assert.assertEquals(0, resources.size()); }
@Test public void testGetResourcesWithUserInputBeginning() throws Exception { ResourceLookupContext resourceLookupContext = Mockito.mock(ResourceLookupContext.class); when(resourceLookupContext.getUserInput()).thenReturn("/pr"); final List<String> expectedResources = new ArrayList<>(); expectedResources.add("/provenance"); expectedResources.add("/proxy"); List<String> resources = niFiClient.getResources(resourceLookupContext); Assert.assertNotNull(resources); Assert.assertEquals(expectedResources.size(), resources.size()); resources.removeAll(expectedResources); Assert.assertEquals(0, resources.size()); }
@Test public void testGetResourcesWithUserInputAnywhere() throws Exception { ResourceLookupContext resourceLookupContext = Mockito.mock(ResourceLookupContext.class); when(resourceLookupContext.getUserInput()).thenReturn("trol"); final List<String> expectedResources = new ArrayList<>(); expectedResources.add("/controller"); List<String> resources = niFiClient.getResources(resourceLookupContext); Assert.assertNotNull(resources); Assert.assertEquals(expectedResources.size(), resources.size()); resources.removeAll(expectedResources); Assert.assertEquals(0, resources.size()); }
@Test public void testGetResourcesErrorResponse() throws Exception { final String errorMsg = "unknown error"; niFiClient = new MockNiFiClient(errorMsg, Response.Status.BAD_REQUEST.getStatusCode()); ResourceLookupContext resourceLookupContext = Mockito.mock(ResourceLookupContext.class); when(resourceLookupContext.getUserInput()).thenReturn(""); try { niFiClient.getResources(resourceLookupContext); Assert.fail("should have thrown exception"); } catch (Exception e) { Assert.assertTrue(e.getMessage().contains(errorMsg)); } }