CommandConverter { public org.eclipse.che.api.workspace.server.model.impl.CommandImpl toWorkspaceCommand( Command devfileCommand, FileContentProvider fileContentProvider) throws DevfileException { if (devfileCommand.getActions().size() != 1) { throw new DevfileFormatException( format("Command `%s` MUST has one and only one action", devfileCommand.getName())); } Action commandAction = devfileCommand.getActions().get(0); return toWorkspaceCommand(devfileCommand, commandAction, fileContentProvider); }  CommandImpl toDevfileCommand(
      org.eclipse.che.api.workspace.server.model.impl.CommandImpl command); org.eclipse.che.api.workspace.server.model.impl.CommandImpl toWorkspaceCommand(
      Command devfileCommand, FileContentProvider fileContentProvider);  }
@Test public void shouldConvertDevfileCommandToWorkspaceCommands() throws Exception { CommandImpl devfileCommand = new CommandImpl(); devfileCommand.setName("build"); devfileCommand.setAttributes(ImmutableMap.of("attr", "value")); ActionImpl action = new ActionImpl(); action.setComponent("dockerimageComponent"); action.setType(EXEC_ACTION_TYPE); action.setWorkdir("/tmp"); action.setCommand("mvn clean install"); devfileCommand.getActions().add(action); org.eclipse.che.api.workspace.server.model.impl.CommandImpl workspaceCommand = commandConverter.toWorkspaceCommand(devfileCommand, null); assertEquals(workspaceCommand.getName(), "build"); assertEquals(workspaceCommand.getType(), EXEC_ACTION_TYPE); assertEquals(workspaceCommand.getAttributes().size(), 3); assertEquals(workspaceCommand.getAttributes().get("attr"), "value"); assertEquals(workspaceCommand.getAttributes().get(WORKING_DIRECTORY_ATTRIBUTE), "/tmp"); assertEquals( workspaceCommand.getAttributes().get(COMPONENT_ALIAS_COMMAND_ATTRIBUTE), "dockerimageComponent"); assertEquals(workspaceCommand.getCommandLine(), "mvn clean install"); }
@Test public void shouldNotSetWorkingDirAttributeIfItIsMissingInDevfileCommandDuringConvertingDevfileCommandToWorkspaceCommands() throws Exception { CommandImpl devfileCommand = new CommandImpl(); ActionImpl action = new ActionImpl(); action.setWorkdir(null); devfileCommand.getActions().add(action); devfileCommand.setAttributes(new HashMap<>()); org.eclipse.che.api.workspace.server.model.impl.CommandImpl workspaceCommand = commandConverter.toWorkspaceCommand(devfileCommand, null); assertFalse(workspaceCommand.getAttributes().containsKey(WORKING_DIRECTORY_ATTRIBUTE)); }
@Test( expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "Command `build` MUST has one and only one action") public void shouldThrowAnExceptionIfDevfileCommandHasMultipleActions() throws Exception { CommandImpl devfileCommand = new CommandImpl(); devfileCommand.setName("build"); devfileCommand.setAttributes(ImmutableMap.of("attr", "value")); devfileCommand.getActions().add(new ActionImpl()); devfileCommand.getActions().add(new ActionImpl()); commandConverter.toWorkspaceCommand(devfileCommand, null); }
@Test public void shouldAcceptActionWithCommand() throws Exception { CommandImpl devfileCommand = new CommandImpl(); devfileCommand.setName("build"); ActionImpl action = new ActionImpl(); action.setType("exec"); action.setCommand("blah"); devfileCommand.getActions().add(action); Command command = commandConverter.toWorkspaceCommand(devfileCommand, null); assertEquals(command.getCommandLine(), "blah"); }
@Test public void shouldAcceptActionWithReference() throws Exception { CommandImpl devfileCommand = new CommandImpl(); devfileCommand.setName("build"); ActionImpl action = new ActionImpl(); action.setType("exec"); action.setReference("blah"); devfileCommand.getActions().add(action); Command command = commandConverter.toWorkspaceCommand(devfileCommand, fileURL -> "content"); assertNull(command.getCommandLine()); assertEquals("blah", command.getAttributes().get(Command.COMMAND_ACTION_REFERENCE_ATTRIBUTE)); assertEquals( "content", command.getAttributes().get(Command.COMMAND_ACTION_REFERENCE_CONTENT_ATTRIBUTE)); }
@Test public void shouldAcceptActionWithReferenceContent() throws Exception { CommandImpl devfileCommand = new CommandImpl(); devfileCommand.setName("build"); ActionImpl action = new ActionImpl(); action.setType("exec"); action.setReference("blah"); action.setReferenceContent("content"); devfileCommand.getActions().add(action); Command command = commandConverter.toWorkspaceCommand(devfileCommand, null); assertNull(command.getCommandLine()); assertEquals("blah", command.getAttributes().get(Command.COMMAND_ACTION_REFERENCE_ATTRIBUTE)); assertEquals( "content", command.getAttributes().get(Command.COMMAND_ACTION_REFERENCE_CONTENT_ATTRIBUTE)); }