NiFiRegistryClient { 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 Registry due to: " + errorMsg); } JsonNode rootNode = mapper.readTree(response.getEntityInputStream()); if (rootNode == null) { throw new Exception("Unable to retrieve resources from NiFi Registry"); } List<String> identifiers = rootNode.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; } } NiFiRegistryClient(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("/policies"); expectedResources.add("/tenants"); expectedResources.add("/proxy"); expectedResources.add("/actuator"); expectedResources.add("/swagger"); expectedResources.add("/buckets"); expectedResources.add("/buckets/fc0625e4-a9ae-4277-bab7-a2bc984f6c4f"); expectedResources.add("/buckets/0b5edba5-da83-4839-b64a-adf5f21abaf4"); List<String> resources = registryClient.getResources(resourceLookupContext); Assert.assertNotNull(resources); Assert.assertEquals(expectedResources.size(), resources.size()); Assert.assertTrue(resources.containsAll(expectedResources)); }
@Test public void testGetResourcesWithUserInputBeginning() throws Exception { ResourceLookupContext resourceLookupContext = Mockito.mock(ResourceLookupContext.class); when(resourceLookupContext.getUserInput()).thenReturn("/p"); final List<String> expectedResources = new ArrayList<>(); expectedResources.add("/policies"); expectedResources.add("/proxy"); List<String> resources = registryClient.getResources(resourceLookupContext); Assert.assertNotNull(resources); Assert.assertEquals(expectedResources.size(), resources.size()); Assert.assertTrue(resources.containsAll(expectedResources)); }
@Test public void testGetResourcesWithUserInputAnywhere() throws Exception { ResourceLookupContext resourceLookupContext = Mockito.mock(ResourceLookupContext.class); when(resourceLookupContext.getUserInput()).thenReturn("ant"); final List<String> expectedResources = new ArrayList<>(); expectedResources.add("/tenants"); List<String> resources = registryClient.getResources(resourceLookupContext); Assert.assertNotNull(resources); Assert.assertEquals(expectedResources.size(), resources.size()); Assert.assertTrue(resources.containsAll(expectedResources)); }
@Test public void testGetResourcesErrorResponse() { final String errorMsg = "unknown error"; registryClient = new MockNiFiRegistryClient(errorMsg, Response.Status.BAD_REQUEST.getStatusCode()); ResourceLookupContext resourceLookupContext = Mockito.mock(ResourceLookupContext.class); when(resourceLookupContext.getUserInput()).thenReturn(""); try { registryClient.getResources(resourceLookupContext); Assert.fail("should have thrown exception"); } catch (Exception e) { Assert.assertTrue(e.getMessage().contains(errorMsg)); } }