BlueprintCommands implements CommandMarker { @CliCommand(value = "blueprint add", help = "Add a new blueprint with either --url or --file") public String addBlueprint( @CliOption(key = "url", mandatory = false, help = "URL of the blueprint to download from") String url, @CliOption(key = "file", mandatory = false, help = "File which contains the blueprint") File file) { String message; try { String json = file == null ? readContent(url) : readContent(file); if (json != null) { client.addBlueprint(json); context.setHint(Hints.BUILD_CLUSTER); context.setBlueprintsAvailable(true); message = String.format("Blueprint: '%s' has been added", getBlueprintName(json)); } else { message = "No blueprint specified"; } } catch (Exception e) { message = "Cannot add blueprint: " + e.getMessage(); } return message; } @Autowired  BlueprintCommands(AmbariClient client, AmbariContext context, ObjectMapper jsonMapper); @CliAvailabilityIndicator("blueprint list") boolean isBlueprintListCommandAvailable(); @CliCommand(value = "blueprint list", help = "Lists all known blueprints") String listBlueprints(); @CliAvailabilityIndicator(value = "blueprint show") boolean isBlueprintShowCommandAvailable(); @CliCommand(value = "blueprint show", help = "Shows the blueprint by its id") String showBlueprint(
    @CliOption(key = "id", mandatory = true, help = "Id of the blueprint") Blueprint id); @CliAvailabilityIndicator(value = "blueprint add") boolean isBlueprintAddCommandAvailable(); @CliCommand(value = "blueprint add", help = "Add a new blueprint with either --url or --file") String addBlueprint(
    @CliOption(key = "url", mandatory = false, help = "URL of the blueprint to download from") String url,
    @CliOption(key = "file", mandatory = false, help = "File which contains the blueprint") File file); @CliAvailabilityIndicator(value = "blueprint defaults") boolean isBlueprintDefaultsAddCommandAvailable(); @CliCommand(value = "blueprint defaults", help = "Adds the default blueprints to Ambari") String addBlueprint();  }
@Test public void testAddBlueprintForFileReadPrecedence() throws IOException { File file = new File("src/test/resources/testBlueprint.json"); String json = IOUtils.toString(new FileInputStream(file)); JsonNode jsonNode = mock(JsonNode.class); when(objectMapper.readTree(json.getBytes())).thenReturn(jsonNode); when(jsonNode.get("Blueprints")).thenReturn(jsonNode); when(jsonNode.get("blueprint_name")).thenReturn(jsonNode); when(jsonNode.asText()).thenReturn("blueprintName"); String result = blueprintCommands.addBlueprint("url", file); verify(ambariClient).addBlueprint(json); verify(context).setHint(Hints.BUILD_CLUSTER); verify(context).setBlueprintsAvailable(true); assertEquals("Blueprint: 'blueprintName' has been added", result); }
@Test public void testAddBlueprintForException() throws IOException { File file = new File("src/test/resources/testBlueprint.json"); String json = IOUtils.toString(new FileInputStream(file)); doThrow(responseException).when(ambariClient).addBlueprint(json); when(responseException.getMessage()).thenReturn("error"); String result = blueprintCommands.addBlueprint("url", file); verify(ambariClient).addBlueprint(json); verify(responseException).getMessage(); assertEquals("Cannot add blueprint: error", result); }
@Test public void testAddBlueprintForDefaults() throws HttpResponseException { String result = blueprintCommands.addBlueprint(); verify(ambariClient).addDefaultBlueprints(); assertEquals("Default blueprints added", result); }
@Test public void testAddBlueprintForUnspecifiedValue() throws HttpResponseException { String response = blueprintCommands.addBlueprint(null, null); assertEquals("No blueprint specified", response); verify(ambariClient, times(0)).addBlueprint(null); }
@Test public void testAddBlueprintDefaultsForException() throws HttpResponseException { doThrow(responseException).when(ambariClient).addDefaultBlueprints(); when(responseException.getMessage()).thenReturn("error"); String result = blueprintCommands.addBlueprint(); verify(responseException).getMessage(); assertEquals("Failed to add the default blueprints: error", result); }
@Test public void testAddBlueprintDefaultsForConnectionRefused() throws HttpResponseException { doThrow(new RuntimeException("Connection refused")).when(ambariClient).addDefaultBlueprints(); when(responseException.getMessage()).thenReturn("error"); String result = blueprintCommands.addBlueprint(); assertEquals("Failed to add the default blueprints: Connection refused", result); }