From 11ec789ca58fd4198aba24d1dcf3c8639dd35b9e Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Tue, 7 Mar 2017 14:33:05 +0000 Subject: [PATCH] CHE-3616 Remove Machine API (server part) (#4331) --- .../machine/local/LocalDockerModule.java | 3 - ...njector.java => MachineLinksInjector.java} | 92 +---- .../environment/server/MachineService.java | 354 ------------------ .../server/WorkspaceServiceLinksInjector.java | 10 +- .../MachineServiceLinksInjectorTest.java | 35 +- .../server/MachineServiceTest.java | 145 ------- .../server/WorkspaceServiceTest.java | 4 +- 7 files changed, 14 insertions(+), 629 deletions(-) rename wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/{MachineServiceLinksInjector.java => MachineLinksInjector.java} (56%) delete mode 100644 wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java delete mode 100644 wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java index 12a6a3a06bb..263c8ee0849 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java @@ -17,7 +17,6 @@ import com.google.inject.multibindings.Multibinder; import com.google.inject.name.Names; -import org.eclipse.che.api.environment.server.MachineService; import org.eclipse.che.api.machine.server.spi.Instance; import org.eclipse.che.api.machine.server.spi.InstanceProcess; import org.eclipse.che.plugin.docker.client.DockerConnector; @@ -42,8 +41,6 @@ public class LocalDockerModule extends AbstractModule { @Override protected void configure() { - bind(MachineService.class); - install(new FactoryModuleBuilder() .implement(Instance.class, DockerInstance.class) .implement(InstanceProcess.class, DockerProcess.class) diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjector.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineLinksInjector.java similarity index 56% rename from wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjector.java rename to wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineLinksInjector.java index 758e0e00aa0..a54b5a21d4e 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjector.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineLinksInjector.java @@ -10,28 +10,20 @@ *******************************************************************************/ package org.eclipse.che.api.environment.server; -import com.google.common.collect.Lists; - import org.eclipse.che.api.core.rest.ServiceContext; import org.eclipse.che.api.core.rest.shared.dto.Link; import org.eclipse.che.api.core.rest.shared.dto.LinkParameter; -import org.eclipse.che.api.machine.shared.Constants; import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; import org.eclipse.che.api.machine.shared.dto.ServerDto; import javax.inject.Singleton; -import javax.ws.rs.HttpMethod; import javax.ws.rs.core.UriBuilder; -import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.List; import static java.lang.String.format; import static java.util.Collections.singletonList; -import static javax.ws.rs.core.MediaType.APPLICATION_JSON; -import static javax.ws.rs.core.MediaType.TEXT_PLAIN; import static org.eclipse.che.api.core.util.LinksHelper.createLink; import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE; import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_STATUS_CHANNEL_TEMPLATE; @@ -42,56 +34,16 @@ import static org.eclipse.che.dto.server.DtoFactory.newDto; /** - * Helps to inject {@link MachineService} related links. + * Helps to inject Machine related links. * * @author Anton Korneta */ @Singleton -public class MachineServiceLinksInjector { +public class MachineLinksInjector { public MachineDto injectLinks(MachineDto machine, ServiceContext serviceContext) { - final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); final List links = new ArrayList<>(); - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getMachineById") - .build(machine.getWorkspaceId(), machine.getId()) - .toString(), - APPLICATION_JSON, - "self link")); - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getMachines") - .build(machine.getWorkspaceId()) - .toString(), - null, - APPLICATION_JSON, - Constants.LINK_REL_GET_MACHINES)); - links.add(createLink(HttpMethod.DELETE, - uriBuilder.clone() - .path(MachineService.class, "stopMachine") - .build(machine.getWorkspaceId(), machine.getId()) - .toString(), - Constants.LINK_REL_DESTROY_MACHINE)); - links.add(createLink(HttpMethod.POST, - uriBuilder.clone() - .path(MachineService.class, "executeCommandInMachine") - .build(machine.getWorkspaceId(), machine.getId()) - .toString(), - APPLICATION_JSON, - APPLICATION_JSON, - Constants.LINK_REL_EXECUTE_COMMAND, - newDto(LinkParameter.class).withName("outputChannel") - .withRequired(false))); - URI getProcessesUri = uriBuilder.clone() - .path(MachineService.class, "getProcesses") - .build(machine.getWorkspaceId(), machine.getId()); - links.add(createLink(HttpMethod.GET, - getProcessesUri.toString(), - APPLICATION_JSON, - Constants.LINK_REL_GET_PROCESSES)); - injectTerminalLink(machine, serviceContext, links); injectExecAgentLink(machine, serviceContext, links); @@ -99,7 +51,9 @@ public MachineDto injectLinks(MachineDto machine, ServiceContext serviceContext) final Link workspaceChannelLink = createLink("GET", serviceContext.getBaseUriBuilder() .path("ws") - .scheme("https".equals(getProcessesUri.getScheme()) ? "wss" : "ws") + .scheme("https".equals( + serviceContext.getBaseUriBuilder().build().getScheme()) ? "wss" + : "ws") .build() .toString(), null); @@ -154,40 +108,4 @@ protected void injectExecAgentLink(MachineDto machine, ServiceContext serviceCon EXEC_AGENT_REFERENCE))); } } - - public MachineProcessDto injectLinks(MachineProcessDto process, - String workspaceId, - String machineId, - ServiceContext serviceContext) { - final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); - final List links = Lists.newArrayListWithExpectedSize(3); - - links.add(createLink(HttpMethod.DELETE, - uriBuilder.clone() - .path(MachineService.class, "stopProcess") - .build(workspaceId, - machineId, - process.getPid()) - .toString(), - Constants.LINK_REL_STOP_PROCESS)); - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getProcessLogs") - .build(workspaceId, - machineId, - process.getPid()) - .toString(), - TEXT_PLAIN, - Constants.LINK_REL_GET_PROCESS_LOGS)); - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getProcesses") - .build(workspaceId, - machineId) - .toString(), - APPLICATION_JSON, - Constants.LINK_REL_GET_PROCESSES)); - - return process.withLinks(links); - } } diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java deleted file mode 100644 index b096c555c51..00000000000 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java +++ /dev/null @@ -1,354 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012-2017 Codenvy, S.A. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Codenvy, S.A. - initial API and implementation - *******************************************************************************/ -package org.eclipse.che.api.environment.server; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; - -import com.google.common.io.CharStreams; - -import org.eclipse.che.api.core.BadRequestException; -import org.eclipse.che.api.core.ConflictException; -import org.eclipse.che.api.core.ForbiddenException; -import org.eclipse.che.api.core.NotFoundException; -import org.eclipse.che.api.core.ServerException; -import org.eclipse.che.api.core.model.machine.Machine; -import org.eclipse.che.api.core.rest.Service; -import org.eclipse.che.api.machine.server.DtoConverter; -import org.eclipse.che.api.machine.server.exception.MachineException; -import org.eclipse.che.api.machine.shared.dto.CommandDto; -import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; -import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; -import org.eclipse.che.api.workspace.server.WorkspaceManager; -import org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl; - -import javax.inject.Inject; -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import java.io.IOException; -import java.io.Reader; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -/** - * Machine API - * - * @author Alexander Garagatyi - * @author Anton Korneta - */ -@Api(value = "/machine", description = "Machine REST API") -@Path("/workspace/{workspaceId}/machine") -@Deprecated -public class MachineService extends Service { - private final MachineProcessManager machineProcessManager; - private final MachineServiceLinksInjector linksInjector; - private final WorkspaceManager workspaceManager; - private final CheEnvironmentValidator environmentValidator; - - @Inject - public MachineService(MachineProcessManager machineProcessManager, - MachineServiceLinksInjector linksInjector, - WorkspaceManager workspaceManager, - CheEnvironmentValidator environmentValidator) { - this.machineProcessManager = machineProcessManager; - this.linksInjector = linksInjector; - this.workspaceManager = workspaceManager; - this.environmentValidator = environmentValidator; - } - - @GET - @Path("/{machineId}") - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Get machine by ID") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains requested machine entity"), - @ApiResponse(code = 404, message = "Machine with specified id does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public MachineDto getMachineById(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId) - throws ServerException, - ForbiddenException, - NotFoundException { - - final Machine machine = workspaceManager.getMachineInstance(workspaceId, machineId); - return linksInjector.injectLinks(DtoConverter.asDto(machine), getServiceContext()); - } - - @GET - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Get all machines of workspace with specified ID", - response = MachineDto.class, - responseContainer = "List") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains requested list of machine entities"), - @ApiResponse(code = 400, message = "Workspace ID is not specified"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public List getMachines(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId) - throws ServerException, - BadRequestException, - NotFoundException { - - requiredNotNull(workspaceId, "Parameter workspace"); - - WorkspaceImpl workspace = workspaceManager.getWorkspace(workspaceId); - if (workspace.getRuntime() == null) { - return Collections.emptyList(); - } else { - return workspace.getRuntime() - .getMachines() - .stream() - .map(DtoConverter::asDto) - .map(machineDto -> linksInjector.injectLinks(machineDto, getServiceContext())) - .collect(Collectors.toList()); - } - } - - @POST - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Create a new machine based on the configuration", - notes = "This operation can be performed only by authorized user") - @ApiResponses({@ApiResponse(code = 204, message = "The machine successfully created"), - @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), - @ApiResponse(code = 403, message = "The user does not have access to create the new machine"), - @ApiResponse(code = 409, message = "Conflict error occurred during the machine creation" + - "(e.g. The machine with such name already exists)." + - "Workspace is not in RUNNING state"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void startMachine(@ApiParam("The workspace id") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "The new machine configuration", required = true) - MachineConfigDto machineConfig) throws ForbiddenException, - NotFoundException, - ServerException, - ConflictException, - BadRequestException { - requiredNotNull(machineConfig, "Machine configuration"); - requiredNotNull(machineConfig.getType(), "Machine type"); - requiredNotNull(machineConfig.getSource(), "Machine source"); - requiredNotNull(machineConfig.getSource().getType(), "Machine source type"); - // definition of source should come either with a content or with location - requiredOnlyOneNotNull(machineConfig.getSource().getLocation(), machineConfig.getSource().getContent(), - "Machine source should provide either location or content"); - - try { - environmentValidator.validateMachine(machineConfig); - } catch (IllegalArgumentException e) { - throw new BadRequestException(e.getLocalizedMessage()); - } - - workspaceManager.startMachine(machineConfig, workspaceId); - } - - @DELETE - @Path("/{machineId}") - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Stop machine") - @ApiResponses({@ApiResponse(code = 204, message = "Machine was successfully stopped"), - @ApiResponse(code = 404, message = "Machine with specified id does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void stopMachine(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") String machineId) throws NotFoundException, - ServerException, - ForbiddenException, - ConflictException { - workspaceManager.stopMachine(workspaceId, machineId); - } - - @POST - @Path("/{machineId}/command") - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Start specified command in machine") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains entity of created machine process"), - @ApiResponse(code = 400, message = "Command entity is invalid"), - @ApiResponse(code = 404, message = "Machine with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public MachineProcessDto executeCommandInMachine(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId, - @ApiParam(value = "Command to execute", required = true) - final CommandDto command, - @ApiParam(value = "Channel for command output") - @QueryParam("outputChannel") - String outputChannel) - throws NotFoundException, - ServerException, - ForbiddenException, - BadRequestException { - - requiredNotNull(command, "Command description"); - requiredNotNull(command.getCommandLine(), "Commandline"); - return linksInjector.injectLinks(DtoConverter.asDto(machineProcessManager.exec(workspaceId, - machineId, - command, - outputChannel)), - workspaceId, - machineId, - getServiceContext()); - } - - @GET - @Path("/{machineId}/process") - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Get processes of machine", - response = MachineProcessDto.class, - responseContainer = "List") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains machine process entities"), - @ApiResponse(code = 404, message = "Machine with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public List getProcesses(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId) - throws NotFoundException, - ServerException, - ForbiddenException { - - return machineProcessManager.getProcesses(workspaceId, machineId) - .stream() - .map(DtoConverter::asDto) - .map(machineProcess -> linksInjector.injectLinks(machineProcess, - workspaceId, - machineId, - getServiceContext())) - .collect(Collectors.toList()); - } - - @DELETE - @Path("/{machineId}/process/{processId}") - @ApiOperation(value = "Stop process in machine") - @ApiResponses({@ApiResponse(code = 204, message = "Process was successfully stopped"), - @ApiResponse(code = 404, message = "Machine with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void stopProcess(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId, - @ApiParam(value = "Process ID") - @PathParam("processId") - int processId) - throws NotFoundException, - ForbiddenException, - ServerException { - - machineProcessManager.stopProcess(workspaceId, machineId, processId); - } - - @GET - @Path("/{machineId}/process/{pid}/logs") - @Produces(MediaType.TEXT_PLAIN) - @ApiOperation(value = "Get logs of machine process") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains logs"), - @ApiResponse(code = 404, message = "Machine or process with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void getProcessLogs(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId, - @ApiParam(value = "Process ID") - @PathParam("pid") - int pid, - @Context - HttpServletResponse httpServletResponse) - throws NotFoundException, - ForbiddenException, - ServerException, - IOException { - - addProcessLogsToResponse(machineId, pid, httpServletResponse); - } - - /** - * Checks only one of the given object reference is {@code null} - * - * @param object1 - * object reference to check - * @param object2 - * object reference to check - * @param subject - * used as subject of exception message "{subject} required" - * @throws BadRequestException - * when objects are both null or have both a value reference is {@code null} - */ - private void requiredOnlyOneNotNull(Object object1, Object object2, String subject) throws BadRequestException { - if (object1 == null && object2 == null) { - throw new BadRequestException(subject + " required"); - } - if (object1 != null && object2 != null) { - throw new BadRequestException(subject + " required"); - } - } - - private void addProcessLogsToResponse(String machineId, int pid, HttpServletResponse httpServletResponse) throws IOException, - NotFoundException, - MachineException { - try (Reader logsReader = machineProcessManager.getProcessLogReader(machineId, pid)) { - // Response is written directly to the servlet request stream - httpServletResponse.setContentType("text/plain"); - CharStreams.copy(logsReader, httpServletResponse.getWriter()); - httpServletResponse.getWriter().flush(); - } - } - - /** - * Checks object reference is not {@code null} - * - * @param object - * object reference to check - * @param subject - * used as subject of exception message "{subject} required" - * @throws BadRequestException - * when object reference is {@code null} - */ - private void requiredNotNull(Object object, String subject) throws BadRequestException { - if (object == null) { - throw new BadRequestException(subject + " required"); - } - } -} diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java index 1bf7870a637..75724fb5579 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java @@ -13,7 +13,7 @@ import org.eclipse.che.api.core.rest.ServiceContext; import org.eclipse.che.api.core.rest.shared.dto.Link; import org.eclipse.che.api.core.rest.shared.dto.LinkParameter; -import org.eclipse.che.api.environment.server.MachineServiceLinksInjector; +import org.eclipse.che.api.environment.server.MachineLinksInjector; import org.eclipse.che.api.machine.shared.dto.MachineDto; import org.eclipse.che.api.machine.shared.dto.ServerDto; import org.eclipse.che.api.machine.shared.dto.SnapshotDto; @@ -63,12 +63,10 @@ @Singleton public class WorkspaceServiceLinksInjector { - //TODO: we need keep IDE context in some property to have possibility configure it because context is different in Che and Hosted packaging - //TODO: not good solution do it here but critical for this task https://jira.codenvycorp.com/browse/IDEX-3619 - private final MachineServiceLinksInjector machineLinksInjector; + private final MachineLinksInjector machineLinksInjector; @Inject - public WorkspaceServiceLinksInjector(MachineServiceLinksInjector machineLinksInjector) { + public WorkspaceServiceLinksInjector(MachineLinksInjector machineLinksInjector) { this.machineLinksInjector = machineLinksInjector; } @@ -246,7 +244,7 @@ protected void injectRuntimeLinks(WorkspaceDto workspace, URI ideUri, UriBuilder } } - public MachineDto injectMachineLinks(MachineDto machine, ServiceContext serviceContext) { + protected MachineDto injectMachineLinks(MachineDto machine, ServiceContext serviceContext) { return machineLinksInjector.injectLinks(machine, serviceContext); } } diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java index 7d0c117a0e6..26829b7e43a 100644 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java @@ -11,13 +11,11 @@ package org.eclipse.che.api.environment.server; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import org.eclipse.che.api.core.rest.ServiceContext; import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; import org.eclipse.che.api.machine.shared.dto.MachineRuntimeInfoDto; import org.eclipse.che.api.machine.shared.dto.ServerDto; import org.eclipse.che.commons.lang.Pair; @@ -37,20 +35,13 @@ import static java.util.Arrays.asList; import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_STATUS_CHANNEL_TEMPLATE; import static org.eclipse.che.api.machine.shared.Constants.EXEC_AGENT_REFERENCE; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_DESTROY_MACHINE; import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_EXECUTE_COMMAND; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_GET_MACHINES; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_GET_PROCESSES; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_GET_PROCESS_LOGS; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_SELF; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_STOP_PROCESS; import static org.eclipse.che.api.machine.shared.Constants.TERMINAL_REFERENCE; import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; /** - * Tests for {@link MachineServiceLinksInjector}. + * Tests for {@link MachineLinksInjector}. * * @author Anton Korneta */ @@ -71,11 +62,11 @@ public class MachineServiceLinksInjectorTest { private ServerDto execAgentServerMock; - private MachineServiceLinksInjector machineLinksInjector; + private MachineLinksInjector machineLinksInjector; @BeforeMethod public void setUp() throws Exception { - machineLinksInjector = new MachineServiceLinksInjector(); + machineLinksInjector = new MachineLinksInjector(); final UriBuilder uriBuilder = new UriBuilderImpl(); uriBuilder.uri(URI_BASE); when(serviceContextMock.getServiceUriBuilder()).thenReturn(uriBuilder); @@ -102,29 +93,9 @@ public void shouldInjectLinksIntoMachineDto() { .collect(Collectors.toSet()); final Set> expectedLinks = new HashSet<>(asList(Pair.of("GET", TERMINAL_REFERENCE), Pair.of("GET", EXEC_AGENT_REFERENCE), - Pair.of("GET", LINK_REL_SELF), - Pair.of("GET", LINK_REL_GET_MACHINES), - Pair.of("POST", LINK_REL_EXECUTE_COMMAND), - Pair.of("DELETE", LINK_REL_DESTROY_MACHINE), - Pair.of("GET", LINK_REL_GET_PROCESSES), Pair.of("GET", LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL), Pair.of("GET", ENVIRONMENT_STATUS_CHANNEL_TEMPLATE))); assertEquals(links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n"); } - - @Test - public void shouldInjectLinksIntoMachineProcessDto() { - final MachineProcessDto machineProcessDto = DtoFactory.newDto(MachineProcessDto.class); - machineLinksInjector.injectLinks(machineProcessDto, "workspaceId", "machineId", serviceContextMock); - final Set> links = machineProcessDto.getLinks() - .stream() - .map(link -> Pair.of(link.getMethod(), link.getRel())) - .collect(Collectors.toSet()); - final Set> expectedLinks = ImmutableSet.of(Pair.of("DELETE", LINK_REL_STOP_PROCESS), - Pair.of("GET", LINK_REL_GET_PROCESS_LOGS), - Pair.of("GET", LINK_REL_GET_PROCESSES)); - - assertEquals(links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n"); - } } diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java deleted file mode 100644 index 1a150b4861a..00000000000 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java +++ /dev/null @@ -1,145 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012-2017 Codenvy, S.A. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Codenvy, S.A. - initial API and implementation - *******************************************************************************/ -package org.eclipse.che.api.environment.server; - -import com.jayway.restassured.http.ContentType; -import com.jayway.restassured.response.Response; - -import org.eclipse.che.api.core.model.machine.MachineConfig; -import org.eclipse.che.api.core.rest.ApiExceptionMapper; -import org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl; -import org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl; -import org.eclipse.che.api.workspace.server.WorkspaceManager; -import org.eclipse.che.api.workspace.server.WorkspaceServiceTest; -import org.everrest.assured.EverrestJetty; -import org.mockito.Mock; -import org.mockito.testng.MockitoTestNGListener; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Listeners; -import org.testng.annotations.Test; - -import static com.jayway.restassured.RestAssured.given; -import static org.everrest.assured.JettyHttpServer.ADMIN_USER_NAME; -import static org.everrest.assured.JettyHttpServer.ADMIN_USER_PASSWORD; -import static org.everrest.assured.JettyHttpServer.SECURE_PATH; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.testng.Assert.assertEquals; - -/** - * @author Alexander Garagatyi - */ -@Listeners(value = {EverrestJetty.class, MockitoTestNGListener.class}) -public class MachineServiceTest { - @SuppressWarnings("unused") - static final ApiExceptionMapper MAPPER = new ApiExceptionMapper(); - @SuppressWarnings("unused") - static final WorkspaceServiceTest.EnvironmentFilter FILTER = new WorkspaceServiceTest.EnvironmentFilter(); - @Mock - WorkspaceManager wsManager; - @Mock - MachineProcessManager machineProcessManager; - @Mock - CheEnvironmentValidator environmentValidator; - - MachineService service; - - @BeforeMethod - public void setup() { - service = new MachineService(machineProcessManager, - new MachineServiceLinksInjector(), - wsManager, - environmentValidator); - } - - @Test(dataProvider = "illegalMachineConfigProvider") - public void shouldReturnErrorOnStartMachineIfBodyIsInvalid(MachineConfig machineConfig) throws Exception { - // given - String workspaceId = "wsId"; - - // when - final Response response = given().auth() - .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) - .when() - .body(machineConfig) - .contentType(ContentType.JSON) - .post(SECURE_PATH + "/workspace/" + workspaceId + "/machine"); - - // then - assertEquals(response.getStatusCode(), 400); - verify(wsManager, never()).startMachine(any(MachineConfig.class), anyString()); - } - - @DataProvider(name = "illegalMachineConfigProvider") - public static Object[][] illegalMachineConfigProvider() { - MachineConfigImpl.MachineConfigImplBuilder builder = - MachineConfigImpl.builder() - .setDev(false) - .setName("name") - .setType("type") - .setSource(new MachineSourceImpl("type1").setContent("content")); - return new Object[][] { - {builder.setType(null) - .build()}, - {builder.setSource(null) - .build()}, - {builder.setSource(new MachineSourceImpl((String)null).setContent("content")) - .build()}, - {builder.setSource(new MachineSourceImpl("type").setContent("content") - .setLocation("location")) - .build()}, - }; - } - - @Test - public void shouldStartMachine() throws Exception { - // given - String workspaceId = "wsId"; - MachineConfig machineConfig = MachineConfigImpl.builder() - .setDev(false) - .setName("name") - .setType("type") - .setSource(new MachineSourceImpl("type1").setContent("content")) - .build(); - - // when - final Response response = given().auth() - .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) - .when() - .body(machineConfig) - .contentType(ContentType.JSON) - .post(SECURE_PATH + "/workspace/" + workspaceId + "/machine"); - - // then - assertEquals(response.getStatusCode(), 204); - verify(wsManager).startMachine(any(MachineConfig.class), eq(workspaceId)); - } - - @Test - public void shouldStopMachine() throws Exception { - // given - String workspaceId = "wsId"; - String machineId = "mcId"; - - // when - final Response response = given().auth() - .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) - .when() - .delete(SECURE_PATH + "/workspace/" + workspaceId + "/machine/" + machineId); - - // then - assertEquals(response.getStatusCode(), 204); - } -} diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java index 4f16a53916c..c268067132b 100644 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java @@ -27,8 +27,8 @@ import org.eclipse.che.api.core.rest.ApiExceptionMapper; import org.eclipse.che.api.core.rest.shared.dto.Link; import org.eclipse.che.api.core.rest.shared.dto.ServiceError; +import org.eclipse.che.api.environment.server.MachineLinksInjector; import org.eclipse.che.api.environment.server.MachineProcessManager; -import org.eclipse.che.api.environment.server.MachineServiceLinksInjector; import org.eclipse.che.api.machine.server.model.impl.CommandImpl; import org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl; import org.eclipse.che.api.machine.server.model.impl.MachineImpl; @@ -151,7 +151,7 @@ public void setup() { wsManager, validator, wsAgentHealthChecker, - new WorkspaceServiceLinksInjector(new MachineServiceLinksInjector()), + new WorkspaceServiceLinksInjector(new MachineLinksInjector()), true, false); }