You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dlab.apache.org by of...@apache.org on 2020/09/04 10:18:00 UTC

[incubator-dlab] branch develop updated: Created test for EnvironmentServiceImpl

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

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


The following commit(s) were added to refs/heads/develop by this push:
     new e0aa7b3  Created test for EnvironmentServiceImpl
e0aa7b3 is described below

commit e0aa7b3653fac544f85d89a3d779f696f916a124
Author: Oleh Fuks <ol...@gmail.com>
AuthorDate: Fri Sep 4 13:17:19 2020 +0300

    Created test for EnvironmentServiceImpl
---
 .../service/impl/EnvironmentServiceImpl.java       |  25 ++-
 .../service/impl/EnvironmentServiceImplTest.java   | 195 ++++++++++++++++++---
 2 files changed, 185 insertions(+), 35 deletions(-)

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 dff0a0b..2ae828c 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
@@ -35,7 +35,6 @@ import com.epam.dlab.backendapi.service.ExploratoryService;
 import com.epam.dlab.backendapi.service.ProjectService;
 import com.epam.dlab.backendapi.service.SecurityService;
 import com.epam.dlab.dto.UserInstanceDTO;
-import com.epam.dlab.dto.UserInstanceStatus;
 import com.epam.dlab.exceptions.ResourceConflictException;
 import com.epam.dlab.model.ResourceEnum;
 import com.google.inject.Inject;
@@ -50,6 +49,10 @@ import java.util.stream.Stream;
 
 import static com.epam.dlab.backendapi.resources.dto.UserDTO.Status.ACTIVE;
 import static com.epam.dlab.backendapi.resources.dto.UserDTO.Status.NOT_ACTIVE;
+import static com.epam.dlab.dto.UserInstanceStatus.CREATING;
+import static com.epam.dlab.dto.UserInstanceStatus.CREATING_IMAGE;
+import static com.epam.dlab.dto.UserInstanceStatus.RUNNING;
+import static com.epam.dlab.dto.UserInstanceStatus.STARTING;
 import static com.epam.dlab.rest.contracts.ComputationalAPI.AUDIT_MESSAGE;
 import static java.util.stream.Collectors.toList;
 
@@ -133,8 +136,8 @@ public class EnvironmentServiceImpl implements EnvironmentService {
 				.forEach(this::stopNotebookWithServiceAccount);
 
 		projectService.get(project).getEndpoints()
-                .stream()
-                .filter(e -> UserInstanceStatus.RUNNING == e.getStatus())
+				.stream()
+				.filter(e -> RUNNING == e.getStatus())
                 .forEach(endpoint -> projectService.stop(securityService.getServiceAccountInfo(DLAB_SYSTEM_USER),
                         endpoint.getName(), project, AUDIT_QUOTA_MESSAGE));
 	}
@@ -169,12 +172,8 @@ public class EnvironmentServiceImpl implements EnvironmentService {
 	}
 
 	private void checkState(String user, String action) {
-		final List<UserInstanceDTO> userInstances = exploratoryDAO
-				.fetchUserExploratoriesWhereStatusIn(user,
-						Arrays.asList(UserInstanceStatus.CREATING,
-								UserInstanceStatus.STARTING, UserInstanceStatus.CREATING_IMAGE),
-						UserInstanceStatus.CREATING,
-						UserInstanceStatus.STARTING, UserInstanceStatus.CREATING_IMAGE);
+		final List<UserInstanceDTO> userInstances = exploratoryDAO.fetchUserExploratoriesWhereStatusIn(user, Arrays.asList(CREATING, STARTING, CREATING_IMAGE),
+				CREATING, STARTING, CREATING_IMAGE);
 		if (!userInstances.isEmpty()) {
 			log.error(String.format(ERROR_MSG_FORMAT, action));
 			throw new ResourceConflictException(String.format(ERROR_MSG_FORMAT, action));
@@ -221,11 +220,9 @@ public class EnvironmentServiceImpl implements EnvironmentService {
 	}
 
 	private void checkProjectResourceConditions(String project, String action) {
-		final List<UserInstanceDTO> userInstances = exploratoryDAO
-				.fetchProjectExploratoriesWhereStatusIn(project,
-						Arrays.asList(UserInstanceStatus.CREATING, UserInstanceStatus.STARTING,
-								UserInstanceStatus.CREATING_IMAGE),
-						UserInstanceStatus.CREATING, UserInstanceStatus.STARTING, UserInstanceStatus.CREATING_IMAGE);
+		final List<UserInstanceDTO> userInstances = exploratoryDAO.fetchProjectExploratoriesWhereStatusIn(project,
+				Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE);
+
 		if (!userInstances.isEmpty()) {
 			log.error(String.format(ERROR_MSG_FORMAT, action));
 			throw new ResourceConflictException(String.format(ERROR_MSG_FORMAT, action));
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 749a1ef..d9b97ef 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
@@ -26,6 +26,7 @@ import com.epam.dlab.backendapi.dao.UserSettingsDAO;
 import com.epam.dlab.backendapi.domain.ProjectDTO;
 import com.epam.dlab.backendapi.domain.ProjectEndpointDTO;
 import com.epam.dlab.backendapi.resources.dto.UserDTO;
+import com.epam.dlab.backendapi.resources.dto.UserResourceInfo;
 import com.epam.dlab.backendapi.service.ComputationalService;
 import com.epam.dlab.backendapi.service.ExploratoryService;
 import com.epam.dlab.backendapi.service.ProjectService;
@@ -34,6 +35,8 @@ 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 com.epam.dlab.model.ResourceEnum;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -47,7 +50,11 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
 
+import static com.epam.dlab.dto.UserInstanceStatus.CREATING;
+import static com.epam.dlab.dto.UserInstanceStatus.CREATING_IMAGE;
+import static com.epam.dlab.dto.UserInstanceStatus.STARTING;
 import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.anyListOf;
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.anySet;
 import static org.mockito.Mockito.anyString;
@@ -63,17 +70,19 @@ import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public class EnvironmentServiceImplTest {
-    private static final String AUDIT_QUOTA_MESSAGE = "Billing quota reached";
-    private static final String AUDIT_MESSAGE = "Notebook name %s";
-    private static final String DLAB_SYSTEM_USER = "DLab system user";
-    private static final String USER = "test";
-    private static final String EXPLORATORY_NAME_1 = "expName1";
-    private static final String EXPLORATORY_NAME_2 = "expName2";
-    private static final String TOKEN = "token";
-    private static final String UUID = "213-12312-321";
-    private static final String PROJECT_NAME = "projectName";
-    private static final String ENDPOINT_NAME = "endpointName";
-    private static final String ADMIN = "admin";
+
+	private static final String AUDIT_QUOTA_MESSAGE = "Billing quota reached";
+	private static final String AUDIT_MESSAGE = "Notebook name %s";
+	private static final String DLAB_SYSTEM_USER = "DLab system user";
+	private static final String DLAB_SYSTEM_USER_TOKEN = "token";
+	private static final String USER = "test";
+	private static final String EXPLORATORY_NAME_1 = "expName1";
+	private static final String EXPLORATORY_NAME_2 = "expName2";
+	private static final String TOKEN = "token";
+	private static final String UUID = "213-12312-321";
+	private static final String PROJECT_NAME = "projectName";
+	private static final String ENDPOINT_NAME = "endpointName";
+	private static final String SHAPE = "shape";
 
 	@Mock
 	private EnvDAO envDAO;
@@ -115,6 +124,114 @@ public class EnvironmentServiceImplTest {
 	}
 
 	@Test
+	public void getAllEnv() {
+		when(exploratoryDAO.getInstances()).thenReturn(getUserInstances());
+		when(projectService.getProjects(any(UserInfo.class))).thenReturn(Collections.singletonList(getProjectDTO()));
+
+		List<UserResourceInfo> actualAllEnv = environmentService.getAllEnv(getUserInfo());
+
+		List<UserResourceInfo> userResources = Arrays.asList(getUserResourceInfoEdge(), getUserResourceInfo(EXPLORATORY_NAME_1), getUserResourceInfo(EXPLORATORY_NAME_2));
+		assertEquals("lists are not equal", userResources, actualAllEnv);
+		verify(exploratoryDAO).getInstances();
+		verify(projectService).getProjects(getUserInfo());
+		verifyNoMoreInteractions(exploratoryDAO, projectService);
+	}
+
+	@Test
+	public void getAllEnvWithoutEdge() {
+		when(exploratoryDAO.getInstances()).thenReturn(getUserInstances());
+		when(projectService.getProjects(any(UserInfo.class))).thenReturn(Collections.singletonList(getProjectDTOWithoutEndpoint()));
+
+		List<UserResourceInfo> actualAllEnv = environmentService.getAllEnv(getUserInfo());
+
+		List<UserResourceInfo> userResources = Arrays.asList(getUserResourceInfo(EXPLORATORY_NAME_1), getUserResourceInfo(EXPLORATORY_NAME_2));
+		assertEquals("lists are not equal", userResources, actualAllEnv);
+		verify(exploratoryDAO).getInstances();
+		verify(projectService).getProjects(getUserInfo());
+		verifyNoMoreInteractions(exploratoryDAO, projectService);
+	}
+
+	@Test
+	public void stopAll() {
+		when(projectService.getProjects()).thenReturn(Collections.singletonList(getProjectDTO()));
+		when(exploratoryDAO.fetchProjectExploratoriesWhereStatusIn(anyString(), anyListOf(UserInstanceStatus.class))).thenReturn(Collections.emptyList());
+		when(exploratoryDAO.fetchRunningExploratoryFieldsForProject(anyString())).thenReturn(getUserInstances());
+		when(securityService.getServiceAccountInfo(anyString())).thenReturn(getDLabSystemUser());
+		when(projectService.get(anyString())).thenReturn(getProjectDTO());
+
+		environmentService.stopAll();
+
+		verify(projectService).getProjects();
+		verify(exploratoryDAO).fetchProjectExploratoriesWhereStatusIn(PROJECT_NAME, Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE);
+		verify(exploratoryDAO).fetchRunningExploratoryFieldsForProject(PROJECT_NAME);
+		verify(securityService, times(3)).getServiceAccountInfo(DLAB_SYSTEM_USER);
+		verify(exploratoryService).stop(getDLabSystemUser(), USER, PROJECT_NAME, EXPLORATORY_NAME_1, AUDIT_QUOTA_MESSAGE);
+		verify(exploratoryService).stop(getDLabSystemUser(), USER, PROJECT_NAME, EXPLORATORY_NAME_2, AUDIT_QUOTA_MESSAGE);
+		verify(projectService).get(PROJECT_NAME);
+		verify(projectService).stop(getDLabSystemUser(), ENDPOINT_NAME, PROJECT_NAME, AUDIT_QUOTA_MESSAGE);
+		verifyNoMoreInteractions(projectService, exploratoryDAO, securityService, exploratoryService);
+	}
+
+	@Test(expected = ResourceConflictException.class)
+	public void stopAllWithException() {
+		when(projectService.getProjects()).thenReturn(Collections.singletonList(getProjectDTO()));
+		when(exploratoryDAO.fetchProjectExploratoriesWhereStatusIn(PROJECT_NAME, Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE)).thenReturn(getUserInstances());
+
+		environmentService.stopAll();
+
+		verify(projectService).getProjects();
+		verify(exploratoryDAO).fetchProjectExploratoriesWhereStatusIn(PROJECT_NAME, Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE);
+		verifyNoMoreInteractions(projectService, exploratoryDAO, securityService, exploratoryService);
+	}
+
+	@Test
+	public void stopAllWithStoppedProject() {
+		when(projectService.getProjects()).thenReturn(Collections.singletonList(getProjectDTOWithStoppedEdge()));
+		when(exploratoryDAO.fetchProjectExploratoriesWhereStatusIn(anyString(), anyListOf(UserInstanceStatus.class))).thenReturn(Collections.emptyList());
+		when(exploratoryDAO.fetchRunningExploratoryFieldsForProject(anyString())).thenReturn(getUserInstances());
+		when(securityService.getServiceAccountInfo(anyString())).thenReturn(getDLabSystemUser());
+		when(projectService.get(anyString())).thenReturn(getProjectDTOWithStoppedEdge());
+
+		environmentService.stopAll();
+
+		verify(projectService).getProjects();
+		verify(exploratoryDAO).fetchProjectExploratoriesWhereStatusIn(PROJECT_NAME, Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE);
+		verify(exploratoryDAO).fetchRunningExploratoryFieldsForProject(PROJECT_NAME);
+		verify(securityService, times(2)).getServiceAccountInfo(DLAB_SYSTEM_USER);
+		verify(exploratoryService).stop(getDLabSystemUser(), USER, PROJECT_NAME, EXPLORATORY_NAME_1, AUDIT_QUOTA_MESSAGE);
+		verify(exploratoryService).stop(getDLabSystemUser(), USER, PROJECT_NAME, EXPLORATORY_NAME_2, AUDIT_QUOTA_MESSAGE);
+		verify(projectService).get(PROJECT_NAME);
+		verifyNoMoreInteractions(projectService, exploratoryDAO, securityService, exploratoryService);
+	}
+
+	@Test
+	public void stopEnvironmentWithServiceAccount() {
+		when(exploratoryDAO.fetchUserExploratoriesWhereStatusIn(anyString(), anyListOf(UserInstanceStatus.class))).thenReturn(Collections.emptyList());
+		when(exploratoryDAO.fetchRunningExploratoryFields(anyString())).thenReturn(getUserInstances());
+		when(securityService.getServiceAccountInfo(anyString())).thenReturn(getDLabSystemUser());
+
+		environmentService.stopEnvironmentWithServiceAccount(USER);
+
+		verify(exploratoryDAO).fetchUserExploratoriesWhereStatusIn(USER, Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE);
+		verify(exploratoryDAO).fetchRunningExploratoryFields(USER);
+		verify(securityService, times(2)).getServiceAccountInfo(DLAB_SYSTEM_USER);
+		verify(exploratoryService).stop(getDLabSystemUser(), USER, PROJECT_NAME, EXPLORATORY_NAME_1, AUDIT_QUOTA_MESSAGE);
+		verify(exploratoryService).stop(getDLabSystemUser(), USER, PROJECT_NAME, EXPLORATORY_NAME_2, AUDIT_QUOTA_MESSAGE);
+		verifyNoMoreInteractions(exploratoryDAO, securityService, exploratoryService);
+	}
+
+	@Test(expected = ResourceConflictException.class)
+	public void stopEnvironmentWithServiceAccountWithException() {
+		when(exploratoryDAO.fetchUserExploratoriesWhereStatusIn(USER, Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE))
+				.thenReturn(getUserInstances());
+
+		environmentService.stopEnvironmentWithServiceAccount(USER);
+
+		verify(exploratoryDAO).fetchUserExploratoriesWhereStatusIn(USER, Arrays.asList(CREATING, STARTING, CREATING_IMAGE), CREATING, STARTING, CREATING_IMAGE);
+		verifyNoMoreInteractions(exploratoryDAO, securityService, exploratoryService);
+	}
+
+	@Test
 	public void getActiveUsersWithException() {
 		doThrow(new DlabException("Users not found")).when(envDAO).fetchActiveEnvUsers();
 
@@ -124,7 +241,6 @@ public class EnvironmentServiceImplTest {
 		environmentService.getUsers();
 	}
 
-
 	@Test
 	public void stopProjectEnvironment() {
         final UserInfo userInfo = getUserInfo();
@@ -186,29 +302,66 @@ public class EnvironmentServiceImplTest {
 	@Test
 	public void terminateComputational() {
 		final UserInfo userInfo = getUserInfo();
-        doNothing().when(computationalService)
-                .terminateComputational(any(UserInfo.class), anyString(), anyString(), anyString(), anyString(), anyString());
+		doNothing().when(computationalService)
+				.terminateComputational(any(UserInfo.class), anyString(), anyString(), anyString(), anyString(), anyString());
 
 		environmentService.terminateComputational(userInfo, USER, PROJECT_NAME, EXPLORATORY_NAME_1, "compName");
 
-        verify(computationalService).terminateComputational(refEq(userInfo), eq(userInfo.getName()), eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1), eq("compName"),
-                eq(String.format(AUDIT_MESSAGE, EXPLORATORY_NAME_1)));
-        verifyNoMoreInteractions(securityService, computationalService);
+		verify(computationalService).terminateComputational(refEq(userInfo), eq(userInfo.getName()), eq(PROJECT_NAME), eq(EXPLORATORY_NAME_1), eq("compName"),
+				eq(String.format(AUDIT_MESSAGE, EXPLORATORY_NAME_1)));
+		verifyNoMoreInteractions(securityService, computationalService);
+	}
+
+	private UserResourceInfo getUserResourceInfoEdge() {
+		return UserResourceInfo.builder()
+				.resourceType(ResourceEnum.EDGE_NODE)
+				.resourceStatus("running")
+				.project(PROJECT_NAME)
+				.ip(null)
+				.build();
+	}
+
+	private UserResourceInfo getUserResourceInfo(String exploratoryName) {
+		return UserResourceInfo.builder()
+				.resourceType(ResourceEnum.NOTEBOOK)
+				.resourceName(exploratoryName)
+				.resourceShape(SHAPE)
+				.resourceStatus("running")
+				.computationalResources(Collections.emptyList())
+				.user(USER)
+				.project(PROJECT_NAME)
+				.cloudProvider("aws")
+				.exploratoryUrls(null)
+				.build();
 	}
 
 	private UserInfo getUserInfo() {
 		return new UserInfo(USER, TOKEN);
 	}
 
+	private UserInfo getDLabSystemUser() {
+		return new UserInfo(DLAB_SYSTEM_USER, DLAB_SYSTEM_USER_TOKEN);
+	}
+
 	private List<UserInstanceDTO> getUserInstances() {
 		return Arrays.asList(
-				new UserInstanceDTO().withExploratoryName(EXPLORATORY_NAME_1).withUser(USER).withProject(PROJECT_NAME),
-				new UserInstanceDTO().withExploratoryName(EXPLORATORY_NAME_2).withUser(USER).withProject(PROJECT_NAME));
+				new UserInstanceDTO().withExploratoryName(EXPLORATORY_NAME_1).withUser(USER).withProject(PROJECT_NAME).withShape(SHAPE).withStatus("running")
+						.withResources(Collections.emptyList()).withCloudProvider("aws"),
+				new UserInstanceDTO().withExploratoryName(EXPLORATORY_NAME_2).withUser(USER).withProject(PROJECT_NAME).withShape(SHAPE).withStatus("running")
+						.withResources(Collections.emptyList()).withCloudProvider("aws"));
 	}
 
 	private ProjectDTO getProjectDTO() {
 		return new ProjectDTO(PROJECT_NAME, Collections.emptySet(), "", "", null,
-				Collections.singletonList(new ProjectEndpointDTO(ENDPOINT_NAME, UserInstanceStatus.RUNNING,
-						new EdgeInfo())), true);
+				Collections.singletonList(new ProjectEndpointDTO(ENDPOINT_NAME, UserInstanceStatus.RUNNING, new EdgeInfo())), true);
+	}
+
+	private ProjectDTO getProjectDTOWithStoppedEdge() {
+		return new ProjectDTO(PROJECT_NAME, Collections.emptySet(), "", "", null,
+				Collections.singletonList(new ProjectEndpointDTO(ENDPOINT_NAME, UserInstanceStatus.STOPPED, new EdgeInfo())), true);
+	}
+
+	private ProjectDTO getProjectDTOWithoutEndpoint() {
+		return new ProjectDTO(PROJECT_NAME, Collections.emptySet(), "", "", null, null, true);
 	}
 }
\ No newline at end of file


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