This is an automated email from the ASF dual-hosted git repository.

ofuks pushed a commit to branch DLAB-1633
in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git

commit 8f404935c41020d0dc26bd7e43b7721a30565042
Author: Oleh Fuks <olegfuk...@gmail.com>
AuthorDate: Wed Mar 18 16:22:22 2020 +0200

    [DLAB-1633] Fixed issue with instance management fails via environment 
management on remote/local endpoints
---
 .../backendapi/resources/EnvironmentResource.java  |  66 ++++++
 .../backendapi/service/EnvironmentService.java     |  11 +
 .../service/impl/EnvironmentServiceImpl.java       |  30 +++
 .../resources/EnvironmentResourceTest.java         | 253 +++++++++++++++++++++
 .../service/impl/EnvironmentServiceImplTest.java   |  94 ++++++++
 5 files changed, 454 insertions(+)

diff --git 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/EnvironmentResource.java
 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/EnvironmentResource.java
index 879b527..ae60e39 100644
--- 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/EnvironmentResource.java
+++ 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/EnvironmentResource.java
@@ -24,10 +24,14 @@ import com.epam.dlab.backendapi.service.EnvironmentService;
 import com.google.inject.Inject;
 import io.dropwizard.auth.Auth;
 import lombok.extern.slf4j.Slf4j;
+import org.hibernate.validator.constraints.NotEmpty;
 
 import javax.annotation.security.RolesAllowed;
+import javax.ws.rs.Consumes;
 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.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -59,4 +63,66 @@ public class EnvironmentResource {
                log.debug("Admin {} requested information about all user's 
environment", userInfo.getName());
                return Response.ok(environmentService.getAllEnv()).build();
        }
+
+       @POST
+       @Consumes(MediaType.TEXT_PLAIN)
+       @Produces(MediaType.APPLICATION_JSON)
+       @Path("stop/{projectName}")
+       public Response stopEnv(@Auth UserInfo userInfo, @NotEmpty String user, 
@PathParam("projectName") String projectName) {
+               log.info("User {} is stopping {} environment", 
userInfo.getName(), user);
+               environmentService.stopEnvironment(userInfo, user, projectName);
+               return Response.ok().build();
+       }
+
+       @POST
+       @Consumes(MediaType.TEXT_PLAIN)
+       @Produces(MediaType.APPLICATION_JSON)
+       @Path("stop/{projectName}/{exploratoryName}")
+       public Response stopNotebook(@Auth UserInfo userInfo, @NotEmpty String 
user,
+                                                                
@PathParam("projectName") String projectName,
+                                                                
@PathParam("exploratoryName") String exploratoryName) {
+               log.info("Admin {} is stopping notebook {} of user {}", 
userInfo.getName(), exploratoryName, user);
+               environmentService.stopExploratory(userInfo, user, projectName, 
exploratoryName);
+               return Response.ok().build();
+       }
+
+       @POST
+       @Consumes(MediaType.TEXT_PLAIN)
+       @Produces(MediaType.APPLICATION_JSON)
+       @Path("stop/{projectName}/{exploratoryName}/{computationalName}")
+       public Response stopCluster(@Auth UserInfo userInfo, @NotEmpty String 
user,
+                                                               
@PathParam("projectName") String projectName,
+                                                               
@PathParam("exploratoryName") String exploratoryName,
+                                                               
@PathParam("computationalName") String computationalName) {
+               log.info("Admin {} is stopping computational resource {} 
affiliated with exploratory {} of user {}",
+                               userInfo.getName(), computationalName, 
exploratoryName, user);
+               environmentService.stopComputational(userInfo, user, 
projectName, exploratoryName, computationalName);
+               return Response.ok().build();
+       }
+
+       @POST
+       @Consumes(MediaType.TEXT_PLAIN)
+       @Produces(MediaType.APPLICATION_JSON)
+       @Path("terminate/{projectName}/{exploratoryName}")
+       public Response terminateNotebook(@Auth UserInfo userInfo, @NotEmpty 
String user,
+                                                                         
@PathParam("projectName") String projectName,
+                                                                         
@PathParam("exploratoryName") String exploratoryName) {
+               log.info("Admin {} is terminating notebook {} of user {}", 
userInfo.getName(), exploratoryName, user);
+               environmentService.terminateExploratory(userInfo, user, 
projectName, exploratoryName);
+               return Response.ok().build();
+       }
+
+       @POST
+       @Consumes(MediaType.TEXT_PLAIN)
+       @Produces(MediaType.APPLICATION_JSON)
+       @Path("terminate/{projectName}/{exploratoryName}/{computationalName}")
+       public Response terminateCluster(@Auth UserInfo userInfo, @NotEmpty 
String user,
+                                                                        
@PathParam("projectName") String projectName,
+                                                                        
@PathParam("exploratoryName") String exploratoryName,
+                                                                        
@PathParam("computationalName") String computationalName) {
+               log.info("Admin {} is terminating computational resource {} 
affiliated with exploratory {} of user {}",
+                               userInfo.getName(), computationalName, 
exploratoryName, user);
+               environmentService.terminateComputational(userInfo, user, 
projectName, exploratoryName, computationalName);
+               return Response.ok().build();
+       }
 }
diff --git 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java
 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java
index de4dcb3..f765aa4 100644
--- 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java
+++ 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java
@@ -19,6 +19,7 @@
 
 package com.epam.dlab.backendapi.service;
 
+import com.epam.dlab.auth.UserInfo;
 import com.epam.dlab.backendapi.resources.dto.UserDTO;
 import com.epam.dlab.backendapi.resources.dto.UserResourceInfo;
 
@@ -35,7 +36,17 @@ public interface EnvironmentService {
 
        void stopAll();
 
+       void stopEnvironment(UserInfo userInfo, String user, String project);
+
        void stopEnvironmentWithServiceAccount(String user);
 
        void stopProjectEnvironment(String project);
+
+       void stopExploratory(UserInfo userInfo, String user, String project, 
String exploratoryName);
+
+       void stopComputational(UserInfo userInfo, String user, String project, 
String exploratoryName, String computationalName);
+
+       void terminateExploratory(UserInfo userInfo, String user, String 
project, String exploratoryName);
+
+       void terminateComputational(UserInfo userInfo, String user, String 
project, String exploratoryName, String computationalName);
 }
diff --git 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java
 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java
index 18a354b..3dfaaae 100644
--- 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java
+++ 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java
@@ -112,6 +112,14 @@ public class EnvironmentServiceImpl implements 
EnvironmentService {
        }
 
        @Override
+       public void stopEnvironment(UserInfo userInfo, String user, String 
project) {
+               log.debug("Stopping environment for user {}", user);
+               checkState(user, "stop");
+               exploratoryDAO.fetchRunningExploratoryFields(user)
+                               .forEach(e -> stopExploratory(userInfo, user, 
project, e.getExploratoryName()));
+       }
+
+       @Override
        public void stopEnvironmentWithServiceAccount(String user) {
                log.debug("Stopping environment for user {} by scheduler", 
user);
                checkState(user, "stop");
@@ -132,6 +140,28 @@ public class EnvironmentServiceImpl implements 
EnvironmentService {
                                                endpoint.getName(), project));
        }
 
+       @Override
+       public void stopExploratory(UserInfo userInfo, String user, String 
project, String exploratoryName) {
+               exploratoryService.stop(new UserInfo(user, 
userInfo.getAccessToken()), project, exploratoryName);
+       }
+
+       @Override
+       public void stopComputational(UserInfo userInfo, String user, String 
project, String exploratoryName, String computationalName) {
+               computationalService.stopSparkCluster(new UserInfo(user, 
userInfo.getAccessToken()), project, exploratoryName,
+                               computationalName);
+       }
+
+       @Override
+       public void terminateExploratory(UserInfo userInfo, String user, String 
project, String exploratoryName) {
+               exploratoryService.terminate(new UserInfo(user, 
userInfo.getAccessToken()), project, exploratoryName);
+       }
+
+       @Override
+       public void terminateComputational(UserInfo userInfo, String user, 
String project, String exploratoryName, String computationalName) {
+               computationalService.terminateComputational(new UserInfo(user, 
userInfo.getAccessToken()), project, exploratoryName,
+                               computationalName);
+       }
+
        private UserDTO toUserDTO(String u, UserDTO.Status status) {
                return new UserDTO(u, 
settingsDAO.getAllowedBudget(u).orElse(null), status);
        }
diff --git 
a/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/EnvironmentResourceTest.java
 
b/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/EnvironmentResourceTest.java
index 8df0856..c257995 100644
--- 
a/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/EnvironmentResourceTest.java
+++ 
b/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/EnvironmentResourceTest.java
@@ -19,8 +19,10 @@
 
 package com.epam.dlab.backendapi.resources;
 
+import com.epam.dlab.auth.UserInfo;
 import com.epam.dlab.backendapi.resources.dto.UserDTO;
 import com.epam.dlab.backendapi.service.EnvironmentService;
+import com.epam.dlab.exceptions.ResourceConflictException;
 import io.dropwizard.auth.AuthenticationException;
 import io.dropwizard.testing.junit.ResourceTestRule;
 import org.apache.http.HttpStatus;
@@ -28,6 +30,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 
+import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.GenericType;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -36,6 +39,11 @@ import java.util.Collections;
 import java.util.List;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -122,4 +130,249 @@ public class EnvironmentResourceTest extends TestBase {
 
                verifyZeroInteractions(environmentService);
        }
+
+       @Test
+       public void stopEnv() {
+               
doNothing().when(environmentService).stopEnvironment(any(UserInfo.class), 
anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               .target("/environment/stop/projectName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_OK, response.getStatus());
+               assertNull(response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).stopEnvironment(new UserInfo(USER, 
TOKEN), USER, "projectName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void stopEnvWithFailedAuth() throws AuthenticationException {
+               authFailSetup();
+               
doNothing().when(environmentService).stopEnvironment(any(UserInfo.class), 
anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               .target("/environment/stop/projectName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verifyZeroInteractions(environmentService);
+       }
+
+       @Test
+       public void stopEnvWithResourceConflictException() {
+               doThrow(new ResourceConflictException("Can not stop environment 
because one of the user resources is in " +
+                               "status CREATING or 
STARTING")).when(environmentService).stopEnvironment(any(UserInfo.class), 
anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               .target("/environment/stop/projectName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, 
response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).stopEnvironment(new UserInfo(USER, 
TOKEN), USER, "projectName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void stopNotebook() {
+               
doNothing().when(environmentService).stopExploratory(any(UserInfo.class), 
anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/stop/projectName/explName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_OK, response.getStatus());
+               assertNull(response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).stopExploratory(new UserInfo(USER, 
TOKEN), USER, "projectName", "explName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void stopNotebookWithFailedAuth() throws AuthenticationException 
{
+               authFailSetup();
+               
doNothing().when(environmentService).stopExploratory(any(UserInfo.class), 
anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/stop/projectName/explName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verifyZeroInteractions(environmentService);
+       }
+
+       @Test
+       public void stopNotebookWithResourceConflictException() {
+               doThrow(new ResourceConflictException("Can not stop notebook 
because its status is CREATING or STARTING"))
+                               
.when(environmentService).stopExploratory(any(UserInfo.class), anyString(), 
anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/stop/projectName/explName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, 
response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).stopExploratory(new UserInfo(USER, 
TOKEN), USER, "projectName", "explName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void stopCluster() {
+               
doNothing().when(environmentService).stopComputational(any(UserInfo.class), 
anyString(), anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/stop/projectName/explName/compName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_OK, response.getStatus());
+               assertNull(response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).stopComputational(new UserInfo(USER, 
TOKEN), USER, "projectName", "explName", "compName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void stopClusterWithFailedAuth() throws AuthenticationException {
+               authFailSetup();
+               
doNothing().when(environmentService).stopComputational(any(UserInfo.class), 
anyString(), anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/stop/projectName/explName/compName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verifyZeroInteractions(environmentService);
+       }
+
+       @Test
+       public void stopClusterWithResourceConflictException() {
+               doThrow(new ResourceConflictException("Can not stop cluster 
because its status is CREATING or STARTING"))
+                               
.when(environmentService).stopComputational(any(UserInfo.class), anyString(), 
anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/stop/projectName/explName/compName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, 
response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).stopComputational(new UserInfo(USER, 
TOKEN), USER, "projectName", "explName", "compName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void terminateNotebook() {
+               
doNothing().when(environmentService).terminateExploratory(any(UserInfo.class), 
anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/terminate/projectName/explName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_OK, response.getStatus());
+               assertNull(response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).terminateExploratory(new 
UserInfo(USER, TOKEN), USER, "projectName", "explName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void terminateNotebookWithFailedAuth() throws 
AuthenticationException {
+               authFailSetup();
+               
doNothing().when(environmentService).terminateExploratory(any(UserInfo.class), 
anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/terminate/projectName/explName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verifyZeroInteractions(environmentService);
+       }
+
+       @Test
+       public void terminateNotebookWithResourceConflictException() {
+               doThrow(new ResourceConflictException("Can not terminate 
notebook because its status is CREATING or STARTING"))
+                               
.when(environmentService).terminateExploratory(any(UserInfo.class), 
anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/terminate/projectName/explName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, 
response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).terminateExploratory(new 
UserInfo(USER, TOKEN), USER, "projectName", "explName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void terminateCluster() {
+               
doNothing().when(environmentService).terminateComputational(any(UserInfo.class),
 anyString(), anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/terminate/projectName/explName/compName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_OK, response.getStatus());
+               assertNull(response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).terminateComputational(new 
UserInfo(USER, TOKEN), USER, "projectName", "explName", "compName");
+               verifyNoMoreInteractions(environmentService);
+       }
+
+       @Test
+       public void terminateClusterWithFailedAuth() throws 
AuthenticationException {
+               authFailSetup();
+               
doNothing().when(environmentService).terminateComputational(any(UserInfo.class),
 anyString(), anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/terminate/projectName/explName/compName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verifyZeroInteractions(environmentService);
+       }
+
+       @Test
+       public void terminateClusterWithResourceConflictException() {
+               doThrow(new ResourceConflictException("Can not terminate 
cluster because its status is CREATING or STARTING"))
+                               
.when(environmentService).terminateComputational(any(UserInfo.class), 
anyString(), anyString(), anyString(), anyString());
+               final Response response = resources.getJerseyTest()
+                               
.target("/environment/terminate/projectName/explName/compName")
+                               .request()
+                               .header("Authorization", "Bearer " + TOKEN)
+                               .post(Entity.text(USER));
+
+               assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, 
response.getStatus());
+               assertEquals(MediaType.APPLICATION_JSON, 
response.getHeaderString(HttpHeaders.CONTENT_TYPE));
+
+               verify(environmentService).terminateComputational(new 
UserInfo(USER, TOKEN), USER, "projectName", "explName", "compName");
+               verifyNoMoreInteractions(environmentService);
+       }
 }
diff --git 
a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java
 
b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java
index 536454e..dca6e0f 100644
--- 
a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java
+++ 
b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java
@@ -34,6 +34,7 @@ import com.epam.dlab.dto.UserInstanceDTO;
 import com.epam.dlab.dto.UserInstanceStatus;
 import com.epam.dlab.dto.base.edge.EdgeInfo;
 import com.epam.dlab.exceptions.DlabException;
+import com.epam.dlab.exceptions.ResourceConflictException;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -53,6 +54,7 @@ import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.anySet;
 import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.anyVararg;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
@@ -146,6 +148,52 @@ public class EnvironmentServiceImplTest {
                environmentService.getUserNames();
        }
 
+
+       @Test
+       public void stopEnvironment() {
+               final UserInfo userInfo = getUserInfo();
+               
when(exploratoryDAO.fetchRunningExploratoryFields(anyString())).thenReturn(getUserInstances());
+               when(exploratoryService.stop(any(UserInfo.class), anyString(), 
anyString())).thenReturn(UUID);
+
+               environmentService.stopEnvironment(userInfo, USER, 
PROJECT_NAME);
+
+               verify(exploratoryDAO).fetchRunningExploratoryFields(USER);
+               verify(exploratoryService).stop(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1));
+               verify(exploratoryService).stop(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_2));
+               
verify(exploratoryDAO).fetchUserExploratoriesWhereStatusIn(USER, 
Arrays.asList(UserInstanceStatus.CREATING,
+                               UserInstanceStatus.STARTING, 
UserInstanceStatus.CREATING_IMAGE),
+                               UserInstanceStatus.CREATING,
+                               UserInstanceStatus.STARTING, 
UserInstanceStatus.CREATING_IMAGE);
+               verifyNoMoreInteractions(exploratoryDAO, exploratoryService);
+       }
+
+       @Test
+       @SuppressWarnings("unchecked")
+       public void stopEnvironmentWithWrongResourceState() {
+               
when(exploratoryDAO.fetchUserExploratoriesWhereStatusIn(anyString(), 
any(List.class), anyVararg()))
+                               .thenReturn(getUserInstances());
+               expectedException.expect(ResourceConflictException.class);
+
+               environmentService.stopEnvironment(getUserInfo(), USER, 
PROJECT_NAME);
+       }
+
+       @Test
+       public void stopEnvironmentWithoutEdge() {
+               final UserInfo userInfo = getUserInfo();
+               
when(exploratoryDAO.fetchRunningExploratoryFields(anyString())).thenReturn(getUserInstances());
+               when(exploratoryService.stop(any(UserInfo.class), anyString(), 
anyString())).thenReturn(UUID);
+
+               environmentService.stopEnvironment(userInfo, USER, 
PROJECT_NAME);
+
+               verify(exploratoryDAO).fetchRunningExploratoryFields(USER);
+               verify(exploratoryService).stop(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1));
+               verify(exploratoryService).stop(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_2));
+               
verify(exploratoryDAO).fetchUserExploratoriesWhereStatusIn(USER, 
Arrays.asList(UserInstanceStatus.CREATING,
+                               UserInstanceStatus.STARTING, 
UserInstanceStatus.CREATING_IMAGE),
+                               UserInstanceStatus.CREATING, 
UserInstanceStatus.STARTING, UserInstanceStatus.CREATING_IMAGE);
+               verifyNoMoreInteractions(envDAO, exploratoryDAO, 
exploratoryService);
+       }
+
        @Test
        public void stopProjectEnvironment() {
                final UserInfo userInfo = getUserInfo();
@@ -171,6 +219,52 @@ public class EnvironmentServiceImplTest {
                verifyNoMoreInteractions(exploratoryDAO, exploratoryService, 
securityService, projectService);
        }
 
+       @Test
+       public void stopExploratory() {
+               final UserInfo userInfo = getUserInfo();
+               when(exploratoryService.stop(any(UserInfo.class), anyString(), 
anyString())).thenReturn(UUID);
+
+               environmentService.stopExploratory(new UserInfo(USER, TOKEN), 
USER, PROJECT_NAME, EXPLORATORY_NAME_1);
+
+               verify(exploratoryService).stop(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1));
+               verifyNoMoreInteractions(securityService, exploratoryService);
+       }
+
+       @Test
+       public void stopComputational() {
+               final UserInfo userInfo = getUserInfo();
+               
doNothing().when(computationalService).stopSparkCluster(any(UserInfo.class), 
anyString(), anyString(), anyString());
+
+               environmentService.stopComputational(userInfo, USER, 
PROJECT_NAME, EXPLORATORY_NAME_1, "compName");
+
+               verify(computationalService).stopSparkCluster(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1), eq("compName"));
+               verifyNoMoreInteractions(securityService, computationalService);
+       }
+
+       @Test
+       public void terminateExploratory() {
+               final UserInfo userInfo = getUserInfo();
+               when(exploratoryService.terminate(any(UserInfo.class), 
anyString(), anyString())).thenReturn(UUID);
+
+               environmentService.terminateExploratory(userInfo, USER, 
PROJECT_NAME, EXPLORATORY_NAME_1);
+
+               verify(exploratoryService).terminate(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1));
+               verifyNoMoreInteractions(securityService, exploratoryService);
+       }
+
+       @Test
+       public void terminateComputational() {
+               final UserInfo userInfo = getUserInfo();
+               doNothing().when(computationalService)
+                               .terminateComputational(any(UserInfo.class), 
anyString(), anyString(), anyString());
+
+               environmentService.terminateComputational(userInfo, USER, 
PROJECT_NAME, EXPLORATORY_NAME_1, "compName");
+
+               verify(computationalService)
+                               .terminateComputational(refEq(userInfo), 
eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1), eq("compName"));
+               verifyNoMoreInteractions(securityService, computationalService);
+       }
+
        private UserInfo getUserInfo() {
                return new UserInfo(USER, TOKEN);
        }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@dlab.apache.org
For additional commands, e-mail: commits-h...@dlab.apache.org

Reply via email to