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 9435ba9 Created test for InfrastructureInfoService 9435ba9 is described below commit 9435ba953e5c908bf8528e76307f516253343c9e Author: Oleh Fuks <olegfuk...@gmail.com> AuthorDate: Mon Aug 31 17:04:50 2020 +0300 Created test for InfrastructureInfoService --- .../resources/InfrastructureInfoResource.java | 2 +- .../resources/dto/ProjectInfrastructureInfo.java | 4 + .../service/InfrastructureInfoService.java | 2 +- .../service/impl/BillingServiceImpl.java | 17 +- .../impl/InfrastructureInfoServiceImpl.java | 86 +++-- .../resources/InfrastructureInfoResourceTest.java | 19 +- .../service/impl/BillingServiceImplTest.java | 8 +- .../impl/InfrastructureInfoServiceImplTest.java | 387 +++++++++++++++++++++ 8 files changed, 452 insertions(+), 73 deletions(-) diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResource.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResource.java index c8952f3..deb9f96 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResource.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResource.java @@ -70,7 +70,7 @@ public class InfrastructureInfoResource { @Path("/status") public HealthStatusPageDTO status(@Auth UserInfo userInfo, @QueryParam("full") @DefaultValue("0") int fullReport) { - return infrastructureInfoService.getHeathStatus(userInfo, fullReport != 0); + return infrastructureInfoService.getHeathStatus(userInfo); } /** diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/dto/ProjectInfrastructureInfo.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/dto/ProjectInfrastructureInfo.java index e8585ac..ab3f7a8 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/dto/ProjectInfrastructureInfo.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/dto/ProjectInfrastructureInfo.java @@ -24,12 +24,16 @@ import com.epam.dlab.backendapi.domain.EndpointDTO; import com.epam.dlab.dto.UserInstanceDTO; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; import lombok.ToString; import java.util.List; import java.util.Map; @AllArgsConstructor +@Builder +@EqualsAndHashCode @ToString public class ProjectInfrastructureInfo { @JsonProperty diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/InfrastructureInfoService.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/InfrastructureInfoService.java index ffb3531..b8fe079 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/InfrastructureInfoService.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/InfrastructureInfoService.java @@ -29,7 +29,7 @@ import java.util.List; public interface InfrastructureInfoService { List<ProjectInfrastructureInfo> getUserResources(UserInfo user); - HealthStatusPageDTO getHeathStatus(UserInfo user, boolean fullReport); + HealthStatusPageDTO getHeathStatus(UserInfo user); InfrastructureMetaInfoDTO getInfrastructureMetaInfo(); } diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java index ae47d27..d5675f0 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java @@ -168,17 +168,12 @@ public class BillingServiceImpl implements BillingService { .stream() .collect(Collectors.toMap(e -> e, e -> getBillingData(userInfo, e))); - billingDataMap - .forEach((endpointDTO, billingData) -> { - log.info("Updating billing information for endpoint {}. Billing data {}", endpointDTO.getName(), billingData); - try { - if (!billingData.isEmpty()) { - updateBillingData(endpointDTO, billingData, endpoints); - } - } catch (Exception e) { - log.error("Something went wrong while trying to update billing for {}. {}", endpointDTO.getName(), e.getMessage(), e); - } - }); + billingDataMap.forEach((endpointDTO, billingData) -> { + log.info("Updating billing information for endpoint {}. Billing data {}", endpointDTO.getName(), billingData); + if (!billingData.isEmpty()) { + updateBillingData(endpointDTO, billingData, endpoints); + } + }); } @Override diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/InfrastructureInfoServiceImpl.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/InfrastructureInfoServiceImpl.java index 98c2f6d..a368b18 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/InfrastructureInfoServiceImpl.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/InfrastructureInfoServiceImpl.java @@ -42,7 +42,6 @@ import com.epam.dlab.dto.azure.edge.EdgeInfoAzure; import com.epam.dlab.dto.base.edge.EdgeInfo; import com.epam.dlab.dto.computational.UserComputationalResource; import com.epam.dlab.dto.gcp.edge.EdgeInfoGcp; -import com.epam.dlab.exceptions.DlabException; import com.google.inject.Inject; import com.jcabi.manifests.Manifests; import lombok.extern.slf4j.Slf4j; @@ -69,9 +68,8 @@ public class InfrastructureInfoServiceImpl implements InfrastructureInfoService private final BillingService billingService; @Inject - public InfrastructureInfoServiceImpl(ExploratoryDAO expDAO, SelfServiceApplicationConfiguration configuration, - ProjectService projectService, EndpointService endpointService, - BillingService billingService) { + public InfrastructureInfoServiceImpl(ExploratoryDAO expDAO, SelfServiceApplicationConfiguration configuration, ProjectService projectService, + EndpointService endpointService, BillingService billingService) { this.expDAO = expDAO; this.configuration = configuration; this.projectService = projectService; @@ -82,47 +80,41 @@ public class InfrastructureInfoServiceImpl implements InfrastructureInfoService @Override public List<ProjectInfrastructureInfo> getUserResources(UserInfo user) { log.debug("Loading list of provisioned resources for user {}", user); - try { - List<EndpointDTO> allEndpoints = endpointService.getEndpoints(); - return projectService.getUserProjects(user, false) - .stream() - .map(p -> { - List<UserInstanceDTO> exploratories = expDAO.findExploratories(user.getName(), p.getName()); - return new ProjectInfrastructureInfo(p.getName(), billingService.getBillingProjectQuoteUsed(p.getName()), - getSharedInfo(p.getName()), exploratories, getExploratoryBillingData(exploratories), - getEndpoints(allEndpoints, p)); - }) - .collect(Collectors.toList()); - } catch (Exception e) { - log.error("Could not load list of provisioned resources for user: {}", user, e); - throw new DlabException("Could not load list of provisioned resources for user: "); - } + List<EndpointDTO> allEndpoints = endpointService.getEndpoints(); + return projectService.getUserProjects(user, Boolean.FALSE) + .stream() + .map(p -> { + List<UserInstanceDTO> exploratories = expDAO.findExploratories(user.getName(), p.getName()); + return ProjectInfrastructureInfo.builder() + .project(p.getName()) + .billingQuoteUsed(billingService.getBillingProjectQuoteUsed(p.getName())) + .shared(getSharedInfo(p.getName())) + .exploratory(exploratories) + .exploratoryBilling(getExploratoryBillingData(exploratories)) + .endpoints(getEndpoints(allEndpoints, p)) + .build(); + }) + .collect(Collectors.toList()); } @Override - public HealthStatusPageDTO getHeathStatus(UserInfo userInfo, boolean fullReport) { - final String user = userInfo.getName(); - log.debug("Request the status of resources for user {}, report type {}", user, fullReport); - try { - return HealthStatusPageDTO.builder() - .status(HealthStatusEnum.OK.toString()) - .listResources(Collections.emptyList()) - .billingEnabled(configuration.isBillingSchedulerEnabled()) - .auditEnabled(configuration.isAuditEnabled()) - .projectAdmin(UserRoles.isProjectAdmin(userInfo)) - .admin(UserRoles.isAdmin(userInfo)) - .projectAssigned(projectService.isAnyProjectAssigned(userInfo)) - .bucketBrowser(HealthStatusPageDTO.BucketBrowser.builder() - .view(checkAccess(userInfo, PERMISSION_VIEW)) - .upload(checkAccess(userInfo, PERMISSION_UPLOAD)) - .download(checkAccess(userInfo, PERMISSION_DOWNLOAD)) - .delete(checkAccess(userInfo, PERMISSION_DELETE)) - .build()) - .build(); - } catch (Exception e) { - log.warn("Could not return status of resources for user {}: {}", user, e.getLocalizedMessage(), e); - throw new DlabException(e.getMessage(), e); - } + public HealthStatusPageDTO getHeathStatus(UserInfo userInfo) { + log.debug("Request the status of resources for user {}", userInfo.getName()); + return HealthStatusPageDTO.builder() + .status(HealthStatusEnum.OK.toString()) + .listResources(Collections.emptyList()) + .billingEnabled(configuration.isBillingSchedulerEnabled()) + .auditEnabled(configuration.isAuditEnabled()) + .projectAdmin(UserRoles.isProjectAdmin(userInfo)) + .admin(UserRoles.isAdmin(userInfo)) + .projectAssigned(projectService.isAnyProjectAssigned(userInfo)) + .bucketBrowser(HealthStatusPageDTO.BucketBrowser.builder() + .view(checkAccess(userInfo, PERMISSION_VIEW)) + .upload(checkAccess(userInfo, PERMISSION_UPLOAD)) + .download(checkAccess(userInfo, PERMISSION_DOWNLOAD)) + .delete(checkAccess(userInfo, PERMISSION_DELETE)) + .build()) + .build(); } @Override @@ -149,13 +141,17 @@ public class InfrastructureInfoServiceImpl implements InfrastructureInfoService } private List<EndpointDTO> getEndpoints(List<EndpointDTO> allEndpoints, ProjectDTO projectDTO) { - return allEndpoints.stream().filter(endpoint -> projectDTO.getEndpoints().stream() - .anyMatch(endpoint1 -> endpoint1.getName().equals(endpoint.getName()))) + return allEndpoints + .stream() + .filter(endpoint -> projectDTO.getEndpoints() + .stream() + .anyMatch(endpoint1 -> endpoint1.getName().equals(endpoint.getName()))) .collect(Collectors.toList()); } private Map<String, Map<String, String>> getSharedInfo(String name) { - return projectService.get(name).getEndpoints().stream() + return projectService.get(name).getEndpoints() + .stream() .collect(Collectors.toMap(ProjectEndpointDTO::getName, this::getSharedInfo)); } diff --git a/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResourceTest.java b/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResourceTest.java index 0f63cb9..d6a150a 100644 --- a/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResourceTest.java +++ b/services/self-service/src/test/java/com/epam/dlab/backendapi/resources/InfrastructureInfoResourceTest.java @@ -38,10 +38,7 @@ import javax.ws.rs.core.Response; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.any; -import static org.mockito.Mockito.anyBoolean; -import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.refEq; import static org.mockito.Mockito.verify; @@ -94,7 +91,7 @@ public class InfrastructureInfoResourceTest extends TestBase { @Test public void healthStatus() { HealthStatusPageDTO hspDto = getHealthStatusPageDTO(); - when(infrastructureInfoService.getHeathStatus(any(UserInfo.class), anyBoolean())).thenReturn(hspDto); + when(infrastructureInfoService.getHeathStatus(any(UserInfo.class))).thenReturn(hspDto); final Response response = resources.getJerseyTest() .target("/infrastructure/status") .queryParam("full", "1") @@ -106,7 +103,7 @@ public class InfrastructureInfoResourceTest extends TestBase { assertEquals(hspDto.getStatus(), response.readEntity(HealthStatusPageDTO.class).getStatus()); assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString(HttpHeaders.CONTENT_TYPE)); - verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo()), eq(true)); + verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo())); verifyNoMoreInteractions(infrastructureInfoService); } @@ -114,7 +111,7 @@ public class InfrastructureInfoResourceTest extends TestBase { public void healthStatusWithFailedAuth() throws AuthenticationException { authFailSetup(); HealthStatusPageDTO hspDto = getHealthStatusPageDTO(); - when(infrastructureInfoService.getHeathStatus(any(UserInfo.class), anyBoolean())).thenReturn(hspDto); + when(infrastructureInfoService.getHeathStatus(any(UserInfo.class))).thenReturn(hspDto); final Response response = resources.getJerseyTest() .target("/infrastructure/status") .queryParam("full", "1") @@ -126,14 +123,14 @@ public class InfrastructureInfoResourceTest extends TestBase { assertEquals(hspDto.getStatus(), response.readEntity(HealthStatusPageDTO.class).getStatus()); assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString(HttpHeaders.CONTENT_TYPE)); - verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo()), eq(true)); + verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo())); verifyNoMoreInteractions(infrastructureInfoService); } @Test public void healthStatusWithDefaultQueryParam() { HealthStatusPageDTO hspDto = getHealthStatusPageDTO(); - when(infrastructureInfoService.getHeathStatus(any(UserInfo.class), anyBoolean())).thenReturn(hspDto); + when(infrastructureInfoService.getHeathStatus(any(UserInfo.class))).thenReturn(hspDto); final Response response = resources.getJerseyTest() .target("/infrastructure/status") .request() @@ -144,14 +141,14 @@ public class InfrastructureInfoResourceTest extends TestBase { assertEquals(hspDto.getStatus(), response.readEntity(HealthStatusPageDTO.class).getStatus()); assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString(HttpHeaders.CONTENT_TYPE)); - verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo()), eq(false)); + verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo())); verifyNoMoreInteractions(infrastructureInfoService); } @Test public void healthStatusWithException() { doThrow(new DlabException("Could not return status of resources for user")) - .when(infrastructureInfoService).getHeathStatus(any(UserInfo.class), anyBoolean()); + .when(infrastructureInfoService).getHeathStatus(any(UserInfo.class)); final Response response = resources.getJerseyTest() .target("/infrastructure/status") .request() @@ -161,7 +158,7 @@ public class InfrastructureInfoResourceTest extends TestBase { assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatus()); assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString(HttpHeaders.CONTENT_TYPE)); - verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo()), eq(false)); + verify(infrastructureInfoService).getHeathStatus(refEq(getUserInfo())); verifyNoMoreInteractions(infrastructureInfoService); } diff --git a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplTest.java b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplTest.java index 8553945..43439cf 100644 --- a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplTest.java +++ b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplTest.java @@ -187,22 +187,22 @@ public class BillingServiceImplTest extends TestBase { @Test public void getExploratoryBillingData() { - when(billingDAO.findBillingData(anyString(), anyString(), anyListOf(String.class))).thenReturn(getBillingReportLineWithDifferentCurrency()); + when(billingDAO.findBillingData(anyString(), anyString(), anyListOf(String.class))).thenReturn(getBillingReportLineWithCost()); BillingReport actualReport = billingService.getExploratoryBillingData(PROJECT, ENDPOINT, EXPLORATORY_NAME, Arrays.asList(COMPUTE_NAME, COMPUTE_NAME_2)); - assertEquals("reports should be equal", getReportWithNullCurrency(), actualReport); + assertEquals("reports should be equal", getReport(), actualReport); verify(billingDAO).findBillingData(PROJECT, ENDPOINT, Arrays.asList(COMPUTE_NAME, COMPUTE_NAME_2, EXPLORATORY_NAME)); verifyNoMoreInteractions(billingDAO); } @Test public void getExploratoryBillingDataWithNullCurrency() { - when(billingDAO.findBillingData(anyString(), anyString(), anyListOf(String.class))).thenReturn(getBillingReportLineWithCost()); + when(billingDAO.findBillingData(anyString(), anyString(), anyListOf(String.class))).thenReturn(getBillingReportLineWithDifferentCurrency()); BillingReport actualReport = billingService.getExploratoryBillingData(PROJECT, ENDPOINT, EXPLORATORY_NAME, Arrays.asList(COMPUTE_NAME, COMPUTE_NAME_2)); - assertEquals("reports should be equal", getReport(), actualReport); + assertEquals("reports should be equal", getReportWithNullCurrency(), actualReport); verify(billingDAO).findBillingData(PROJECT, ENDPOINT, Arrays.asList(COMPUTE_NAME, COMPUTE_NAME_2, EXPLORATORY_NAME)); verifyNoMoreInteractions(billingDAO); } diff --git a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/InfrastructureInfoServiceImplTest.java b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/InfrastructureInfoServiceImplTest.java new file mode 100644 index 0000000..5a3caa3 --- /dev/null +++ b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/InfrastructureInfoServiceImplTest.java @@ -0,0 +1,387 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.epam.dlab.backendapi.service.impl; + +import com.epam.dlab.auth.UserInfo; +import com.epam.dlab.backendapi.conf.SelfServiceApplicationConfiguration; +import com.epam.dlab.backendapi.dao.ExploratoryDAO; +import com.epam.dlab.backendapi.domain.BillingReport; +import com.epam.dlab.backendapi.domain.BillingReportLine; +import com.epam.dlab.backendapi.domain.ProjectDTO; +import com.epam.dlab.backendapi.domain.ProjectEndpointDTO; +import com.epam.dlab.backendapi.resources.TestBase; +import com.epam.dlab.backendapi.resources.dto.HealthStatusPageDTO; +import com.epam.dlab.backendapi.resources.dto.ProjectInfrastructureInfo; +import com.epam.dlab.backendapi.service.BillingService; +import com.epam.dlab.backendapi.service.EndpointService; +import com.epam.dlab.backendapi.service.ProjectService; +import com.epam.dlab.dto.InfrastructureMetaInfoDTO; +import com.epam.dlab.dto.UserInstanceDTO; +import com.epam.dlab.dto.UserInstanceStatus; +import com.epam.dlab.dto.aws.edge.EdgeInfoAws; +import com.epam.dlab.dto.azure.edge.EdgeInfoAzure; +import com.epam.dlab.dto.base.DataEngineType; +import com.epam.dlab.dto.billing.BillingResourceType; +import com.epam.dlab.dto.computational.UserComputationalResource; +import com.epam.dlab.dto.gcp.edge.EdgeInfoGcp; +import com.jcabi.manifests.Manifests; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.anyListOf; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class InfrastructureInfoServiceImplTest extends TestBase { + + private static final String PROJECT = "project"; + private static final String EXPLORATORY_NAME = "exploratoryName"; + private static final String COMPUTE_NAME = "computeName"; + private static final String CURRENCY = "currency"; + + @Mock + private ExploratoryDAO expDAO; + @Mock + private SelfServiceApplicationConfiguration configuration; + @Mock + private ProjectService projectService; + @Mock + private EndpointService endpointService; + @Mock + private BillingService billingService; + + @InjectMocks + private InfrastructureInfoServiceImpl infoService; + + @Test + public void getUserResources() { + when(endpointService.getEndpoints()).thenReturn(Collections.singletonList(getEndpointDTO())); + when(projectService.getUserProjects(any(UserInfo.class), anyBoolean())).thenReturn(Collections.singletonList(getProjectDTO())); + when(expDAO.findExploratories(anyString(), anyString())).thenReturn(getUserInstanceDTOs()); + when(billingService.getBillingProjectQuoteUsed(anyString())).thenReturn(10); + when(projectService.get(anyString())).thenReturn(getProjectDTO()); + when(billingService.getExploratoryBillingData(anyString(), anyString(), anyString(), anyListOf(String.class))).thenReturn(getReport()); + + List<ProjectInfrastructureInfo> actualUserResources = infoService.getUserResources(getUserInfo()); + + assertEquals("resources should be equal", getProjectInfrastructureInfo(), actualUserResources); + verify(endpointService).getEndpoints(); + verify(projectService).getUserProjects(getUserInfo(), Boolean.FALSE); + verify(expDAO).findExploratories(USER.toLowerCase(), PROJECT); + verify(billingService).getBillingProjectQuoteUsed(PROJECT); + verify(projectService).get(PROJECT); + verify(billingService).getExploratoryBillingData(PROJECT, ENDPOINT_NAME, EXPLORATORY_NAME, Collections.singletonList(COMPUTE_NAME)); + verifyNoMoreInteractions(endpointService, projectService, expDAO, billingService); + } + + @Test + public void getAwsUserResources() { + when(endpointService.getEndpoints()).thenReturn(Collections.singletonList(getEndpointDTO())); + when(projectService.getUserProjects(any(UserInfo.class), anyBoolean())).thenReturn(Collections.singletonList(getAwsProjectDTO())); + when(expDAO.findExploratories(anyString(), anyString())).thenReturn(getUserInstanceDTOs()); + when(billingService.getBillingProjectQuoteUsed(anyString())).thenReturn(10); + when(projectService.get(anyString())).thenReturn(getAwsProjectDTO()); + when(billingService.getExploratoryBillingData(anyString(), anyString(), anyString(), anyListOf(String.class))).thenReturn(getReport()); + + List<ProjectInfrastructureInfo> actualUserResources = infoService.getUserResources(getUserInfo()); + + assertEquals("resources should be equal", getAwsProjectInfrastructureInfo(), actualUserResources); + verify(endpointService).getEndpoints(); + verify(projectService).getUserProjects(getUserInfo(), Boolean.FALSE); + verify(expDAO).findExploratories(USER.toLowerCase(), PROJECT); + verify(billingService).getBillingProjectQuoteUsed(PROJECT); + verify(projectService).get(PROJECT); + verify(billingService).getExploratoryBillingData(PROJECT, ENDPOINT_NAME, EXPLORATORY_NAME, Collections.singletonList(COMPUTE_NAME)); + verifyNoMoreInteractions(endpointService, projectService, expDAO, billingService); + } + + @Test + public void getAzureUserResources() { + when(endpointService.getEndpoints()).thenReturn(Collections.singletonList(getEndpointDTO())); + when(projectService.getUserProjects(any(UserInfo.class), anyBoolean())).thenReturn(Collections.singletonList(getAzureProjectDTO())); + when(expDAO.findExploratories(anyString(), anyString())).thenReturn(getUserInstanceDTOs()); + when(billingService.getBillingProjectQuoteUsed(anyString())).thenReturn(10); + when(projectService.get(anyString())).thenReturn(getAzureProjectDTO()); + when(billingService.getExploratoryBillingData(anyString(), anyString(), anyString(), anyListOf(String.class))).thenReturn(getReport()); + + List<ProjectInfrastructureInfo> actualUserResources = infoService.getUserResources(getUserInfo()); + + assertEquals("resources should be equal", getAzureProjectInfrastructureInfo(), actualUserResources); + verify(endpointService).getEndpoints(); + verify(projectService).getUserProjects(getUserInfo(), Boolean.FALSE); + verify(expDAO).findExploratories(USER.toLowerCase(), PROJECT); + verify(billingService).getBillingProjectQuoteUsed(PROJECT); + verify(projectService).get(PROJECT); + verify(billingService).getExploratoryBillingData(PROJECT, ENDPOINT_NAME, EXPLORATORY_NAME, Collections.singletonList(COMPUTE_NAME)); + verifyNoMoreInteractions(endpointService, projectService, expDAO, billingService); + } + + @Test + public void getGcpUserResources() { + when(endpointService.getEndpoints()).thenReturn(Collections.singletonList(getEndpointDTO())); + when(projectService.getUserProjects(any(UserInfo.class), anyBoolean())).thenReturn(Collections.singletonList(getGcpProjectDTO())); + when(expDAO.findExploratories(anyString(), anyString())).thenReturn(getUserInstanceDTOs()); + when(billingService.getBillingProjectQuoteUsed(anyString())).thenReturn(10); + when(projectService.get(anyString())).thenReturn(getGcpProjectDTO()); + when(billingService.getExploratoryBillingData(anyString(), anyString(), anyString(), anyListOf(String.class))).thenReturn(getReport()); + + List<ProjectInfrastructureInfo> actualUserResources = infoService.getUserResources(getUserInfo()); + + assertEquals("resources should be equal", getGcpProjectInfrastructureInfo(), actualUserResources); + verify(endpointService).getEndpoints(); + verify(projectService).getUserProjects(getUserInfo(), Boolean.FALSE); + verify(expDAO).findExploratories(USER.toLowerCase(), PROJECT); + verify(billingService).getBillingProjectQuoteUsed(PROJECT); + verify(projectService).get(PROJECT); + verify(billingService).getExploratoryBillingData(PROJECT, ENDPOINT_NAME, EXPLORATORY_NAME, Collections.singletonList(COMPUTE_NAME)); + verifyNoMoreInteractions(endpointService, projectService, expDAO, billingService); + } + + @Test + public void getHeathStatus() { + when(configuration.isBillingSchedulerEnabled()).thenReturn(Boolean.TRUE); + when(configuration.isAuditEnabled()).thenReturn(Boolean.TRUE); + when(projectService.isAnyProjectAssigned(any(UserInfo.class))).thenReturn(Boolean.TRUE); + + HealthStatusPageDTO actualHeathStatus = infoService.getHeathStatus(getUserInfo()); + + assertEquals("HealthStatusPageDTO should be equal", getHealthStatusPageDTO(), actualHeathStatus); + verify(projectService).isAnyProjectAssigned(getUserInfo()); + verify(configuration).isBillingSchedulerEnabled(); + verify(configuration).isAuditEnabled(); + verifyNoMoreInteractions(configuration, projectService); + } + + @Test + public void getInfrastructureMetaInfo() { + Manifests.DEFAULT.put("GIT-Branch", "branch"); + Manifests.DEFAULT.put("GIT-Commit", "commit"); + Manifests.DEFAULT.put("DLab-Version", "version"); + + InfrastructureMetaInfoDTO actualInfrastructureMetaInfo = infoService.getInfrastructureMetaInfo(); + + assertEquals("InfrastructureMetaInfoDTO should be equal", getInfrastructureMetaInfoDTO(), actualInfrastructureMetaInfo); + } + + private InfrastructureMetaInfoDTO getInfrastructureMetaInfoDTO() { + return InfrastructureMetaInfoDTO.builder() + .branch("branch") + .commit("commit") + .version("version") + .releaseNotes("https://github.com/apache/incubator-dlab/blob/branch/RELEASE_NOTES.md") + .build(); + } + + private HealthStatusPageDTO getHealthStatusPageDTO() { + return HealthStatusPageDTO.builder() + .status("ok") + .listResources(Collections.emptyList()) + .billingEnabled(Boolean.TRUE) + .auditEnabled(Boolean.TRUE) + .projectAdmin(Boolean.FALSE) + .admin(Boolean.FALSE) + .projectAssigned(Boolean.TRUE) + .bucketBrowser(HealthStatusPageDTO.BucketBrowser.builder() + .view(Boolean.TRUE) + .upload(Boolean.TRUE) + .download(Boolean.TRUE) + .delete(Boolean.TRUE) + .build()) + .build(); + } + + private List<ProjectInfrastructureInfo> getProjectInfrastructureInfo() { + List<ProjectInfrastructureInfo> objects = new ArrayList<>(); + objects.add(ProjectInfrastructureInfo.builder() + .project(PROJECT) + .billingQuoteUsed(10) + .shared(Collections.singletonMap(ENDPOINT_NAME, Collections.emptyMap())) + .exploratory(getUserInstanceDTOs()) + .exploratoryBilling(Collections.singletonList(getReport())) + .endpoints(Collections.singletonList(getEndpointDTO())) + .build()); + return objects; + } + + private List<ProjectInfrastructureInfo> getAwsProjectInfrastructureInfo() { + List<ProjectInfrastructureInfo> objects = new ArrayList<>(); + objects.add(ProjectInfrastructureInfo.builder() + .project(PROJECT) + .billingQuoteUsed(10) + .shared(Collections.singletonMap(ENDPOINT_NAME, getAwsEdgeInfo())) + .exploratory(getUserInstanceDTOs()) + .exploratoryBilling(Collections.singletonList(getReport())) + .endpoints(Collections.singletonList(getEndpointDTO())) + .build()); + return objects; + } + + private List<ProjectInfrastructureInfo> getAzureProjectInfrastructureInfo() { + List<ProjectInfrastructureInfo> objects = new ArrayList<>(); + objects.add(ProjectInfrastructureInfo.builder() + .project(PROJECT) + .billingQuoteUsed(10) + .shared(Collections.singletonMap(ENDPOINT_NAME, getAzureEdgeInfo())) + .exploratory(getUserInstanceDTOs()) + .exploratoryBilling(Collections.singletonList(getReport())) + .endpoints(Collections.singletonList(getEndpointDTO())) + .build()); + return objects; + } + + private List<ProjectInfrastructureInfo> getGcpProjectInfrastructureInfo() { + List<ProjectInfrastructureInfo> objects = new ArrayList<>(); + objects.add(ProjectInfrastructureInfo.builder() + .project(PROJECT) + .billingQuoteUsed(10) + .shared(Collections.singletonMap(ENDPOINT_NAME, getGcpEdgeInfo())) + .exploratory(getUserInstanceDTOs()) + .exploratoryBilling(Collections.singletonList(getReport())) + .endpoints(Collections.singletonList(getEndpointDTO())) + .build()); + return objects; + } + + private Map<String, String> getAwsEdgeInfo() { + HashMap<String, String> edge = new HashMap<>(); + edge.put("status", "running"); + edge.put("edge_node_ip", "publicIp"); + edge.put("user_own_bicket_name", "ownBucketName"); + edge.put("shared_bucket_name", "sharedBucketName"); + return edge; + } + + private Map<String, String> getAzureEdgeInfo() { + HashMap<String, String> edge = new HashMap<>(); + edge.put("status", "running"); + edge.put("edge_node_ip", "publicIp"); + edge.put("user_container_name", "userContainerName"); + edge.put("shared_container_name", "sharedContainerName"); + edge.put("user_storage_account_name", "userStorageAccountName"); + edge.put("shared_storage_account_name", "sharedStorageAccountName"); + edge.put("datalake_name", "dataLakeName"); + edge.put("datalake_user_directory_name", "dataLakeDirectoryName"); + edge.put("datalake_shared_directory_name", "dataLakeSharedDirectoryName"); + return edge; + } + + private Map<String, String> getGcpEdgeInfo() { + HashMap<String, String> edge = new HashMap<>(); + edge.put("status", "running"); + edge.put("edge_node_ip", "publicIp"); + edge.put("user_own_bucket_name", "ownBucketName"); + edge.put("shared_bucket_name", "sharedBucketName"); + return edge; + } + + private ProjectDTO getProjectDTO() { + return ProjectDTO.builder() + .name(PROJECT) + .endpoints(Collections.singletonList(new ProjectEndpointDTO(ENDPOINT_NAME, UserInstanceStatus.RUNNING, null))) + .build(); + } + + private ProjectDTO getAwsProjectDTO() { + EdgeInfoAws edgeInfoAws = new EdgeInfoAws(); + edgeInfoAws.setPublicIp("publicIp"); + edgeInfoAws.setUserOwnBucketName("ownBucketName"); + edgeInfoAws.setSharedBucketName("sharedBucketName"); + + return ProjectDTO.builder() + .name(PROJECT) + .endpoints(Collections.singletonList(new ProjectEndpointDTO(ENDPOINT_NAME, UserInstanceStatus.RUNNING, edgeInfoAws))) + .build(); + } + + private ProjectDTO getAzureProjectDTO() { + EdgeInfoAzure edgeInfoAzure = new EdgeInfoAzure(); + edgeInfoAzure.setPublicIp("publicIp"); + edgeInfoAzure.setUserContainerName("userContainerName"); + edgeInfoAzure.setSharedContainerName("sharedContainerName"); + edgeInfoAzure.setUserStorageAccountName("userStorageAccountName"); + edgeInfoAzure.setSharedStorageAccountName("sharedStorageAccountName"); + edgeInfoAzure.setDataLakeName("dataLakeName"); + edgeInfoAzure.setDataLakeDirectoryName("dataLakeDirectoryName"); + edgeInfoAzure.setDataLakeSharedDirectoryName("dataLakeSharedDirectoryName"); + + return ProjectDTO.builder() + .name(PROJECT) + .endpoints(Collections.singletonList(new ProjectEndpointDTO(ENDPOINT_NAME, UserInstanceStatus.RUNNING, edgeInfoAzure))) + .build(); + } + + private ProjectDTO getGcpProjectDTO() { + EdgeInfoGcp edgeInfoGcp = new EdgeInfoGcp(); + edgeInfoGcp.setPublicIp("publicIp"); + edgeInfoGcp.setUserOwnBucketName("ownBucketName"); + edgeInfoGcp.setSharedBucketName("sharedBucketName"); + + return ProjectDTO.builder() + .name(PROJECT) + .endpoints(Collections.singletonList(new ProjectEndpointDTO(ENDPOINT_NAME, UserInstanceStatus.RUNNING, edgeInfoGcp))) + .build(); + } + + private List<UserInstanceDTO> getUserInstanceDTOs() { + return Collections.singletonList( + new UserInstanceDTO().withUser(USER).withProject(PROJECT).withExploratoryName(EXPLORATORY_NAME).withEndpoint(ENDPOINT_NAME) + .withResources(Collections.singletonList(getCompute())) + ); + } + + private UserComputationalResource getCompute() { + UserComputationalResource resource = new UserComputationalResource(); + resource.setComputationalName(COMPUTE_NAME); + resource.setImageName(DataEngineType.SPARK_STANDALONE.getName()); + + return resource; + } + + private BillingReport getReport() { + BillingReportLine line1 = BillingReportLine.builder().cost(1.0).user(USER).resourceType(BillingResourceType.EXPLORATORY).project(PROJECT).endpoint(ENDPOINT_NAME) + .resourceName(EXPLORATORY_NAME).currency(CURRENCY).build(); + BillingReportLine line2 = BillingReportLine.builder().cost(1.0).user(USER).resourceType(BillingResourceType.COMPUTATIONAL).project(PROJECT).endpoint(ENDPOINT_NAME) + .resourceName(COMPUTE_NAME).exploratoryName(EXPLORATORY_NAME).currency(CURRENCY).build(); + List<BillingReportLine> billingReportLines = Arrays.asList(line1, line2); + + return BillingReport.builder() + .name(EXPLORATORY_NAME) + .reportLines(billingReportLines) + .totalCost(2.0) + .currency(CURRENCY) + .build(); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@dlab.apache.org For additional commands, e-mail: commits-h...@dlab.apache.org