Search in sources :

Example 1 with ResourceMeasureUnit

use of io.imunity.furms.domain.resource_types.ResourceMeasureUnit in project furms by unity-idm.

the class ResourceUsageJSONExporterTest method shouldGenerateValidDataForCommunityAllocCsv.

@Test
void shouldGenerateValidDataForCommunityAllocCsv() {
    String communityId = UUID.randomUUID().toString();
    String communityAllocationId = UUID.randomUUID().toString();
    String projectAllocationId1 = UUID.randomUUID().toString();
    String projectAllocationId2 = UUID.randomUUID().toString();
    String name = "name";
    ResourceMeasureUnit unit = ResourceMeasureUnit.KILO;
    LocalDate startDate = LocalDate.now();
    LocalDate endDate = startDate.plusDays(60);
    List<LocalDate> XTimeAxis = List.of(startDate, startDate.plusDays(3), startDate.plusDays(5), startDate.plusDays(10), startDate.plusDays(13), startDate.plusDays(18), startDate.plusDays(24), startDate.plusDays(25), startDate.plusDays(50));
    List<Double> projectAllocations1 = List.of(20D, 40D, 50D, 80D, 90D);
    List<Double> projectAllocations2 = List.of(30D, 60D, 70D, 90D, 95D);
    Set<ResourceUsage> communityAllocationUsage = new HashSet<>();
    Iterator<LocalDate> XTimeAxisIterator = XTimeAxis.iterator();
    Iterator<Double> projectAllocation1Iterator = projectAllocations1.iterator();
    Iterator<Double> projectAllocation2Iterator = projectAllocations2.iterator();
    simulateResourceUsageInTime(projectAllocationId1, projectAllocationId2, communityAllocationUsage, XTimeAxisIterator, projectAllocation1Iterator, projectAllocation2Iterator);
    when(communityAllocationRepository.findByIdWithRelatedObjects(communityAllocationId)).thenReturn(Optional.of(CommunityAllocationResolved.builder().name(name).amount(new BigDecimal(200)).resourceCredit(ResourceCredit.builder().utcStartTime(startDate.atStartOfDay()).utcEndTime(endDate.atStartOfDay()).build()).resourceType(ResourceType.builder().unit(unit).build()).build()));
    when(resourceUsageRepository.findResourceUsagesHistoryByCommunityAllocationId(UUID.fromString(communityAllocationId))).thenReturn(communityAllocationUsage);
    when(communityAllocationRepository.findById(communityAllocationId)).thenReturn(Optional.of(CommunityAllocation.builder().communityId(communityId).build()));
    String csvForCommunity = exporter.getCsvForCommunityAllocation(communityId, communityAllocationId).get();
    String header = "Allocation,Consumption until,Amount,Unit" + "\r\n";
    String row1 = name + "," + XTimeAxis.get(1).atStartOfDay() + ",50.0," + unit.getSuffix() + "\r\n";
    String row2 = name + "," + XTimeAxis.get(2).atStartOfDay() + ",70.0," + unit.getSuffix() + "\r\n";
    String row3 = name + "," + XTimeAxis.get(3).atStartOfDay() + ",100.0," + unit.getSuffix() + "\r\n";
    String row4 = name + "," + XTimeAxis.get(4).atStartOfDay() + ",115.0," + unit.getSuffix() + "\r\n";
    String row5 = name + "," + XTimeAxis.get(4).atStartOfDay().plusMinutes(30) + ",120.0," + unit.getSuffix() + "\r\n";
    String row6 = name + "," + XTimeAxis.get(5).atStartOfDay() + ",150.0," + unit.getSuffix() + "\r\n";
    String row7 = name + "," + XTimeAxis.get(6).atStartOfDay() + ",160.0," + unit.getSuffix() + "\r\n";
    String row8 = name + "," + XTimeAxis.get(7).atStartOfDay() + ",180.0," + unit.getSuffix() + "\r\n";
    String row9 = name + "," + XTimeAxis.get(8).atStartOfDay() + ",185.0," + unit.getSuffix();
    assertThat(csvForCommunity).isEqualTo((header + row1 + row2 + row3 + row4 + row5 + row6 + row7 + row8 + row9));
}
Also used : ResourceUsage(io.imunity.furms.domain.resource_usage.ResourceUsage) ResourceMeasureUnit(io.imunity.furms.domain.resource_types.ResourceMeasureUnit) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceMeasureUnit

use of io.imunity.furms.domain.resource_types.ResourceMeasureUnit in project furms by unity-idm.

the class ResourceUsageCSVExporterTest method shouldGenerateValidDataForCommunityAllocJson.

@Test
void shouldGenerateValidDataForCommunityAllocJson() throws IOException {
    String communityId = UUID.randomUUID().toString();
    String communityAllocationId = UUID.randomUUID().toString();
    String projectAllocationId1 = UUID.randomUUID().toString();
    String projectAllocationId2 = UUID.randomUUID().toString();
    String name = "name";
    ResourceMeasureUnit unit = ResourceMeasureUnit.KILO;
    LocalDate startDate = LocalDate.now();
    LocalDate endDate = startDate.plusDays(60);
    List<LocalDate> XTimeAxis = List.of(startDate, startDate.plusDays(3), startDate.plusDays(5), startDate.plusDays(10), startDate.plusDays(13), startDate.plusDays(18), startDate.plusDays(24), startDate.plusDays(25), startDate.plusDays(50));
    List<Double> projectAllocations1 = List.of(20D, 40D, 50D, 80D, 90D);
    List<Double> projectAllocations2 = List.of(30D, 60D, 70D, 90D, 95D);
    Set<ResourceUsage> communityAllocationUsage = new HashSet<>();
    Iterator<LocalDate> XTimeAxisIterator = XTimeAxis.iterator();
    Iterator<Double> projectAllocation1Iterator = projectAllocations1.iterator();
    Iterator<Double> projectAllocation2Iterator = projectAllocations2.iterator();
    simulateResourceUsageInTime(projectAllocationId1, projectAllocationId2, communityAllocationUsage, XTimeAxisIterator, projectAllocation1Iterator, projectAllocation2Iterator);
    when(communityAllocationRepository.findByIdWithRelatedObjects(communityAllocationId)).thenReturn(Optional.of(CommunityAllocationResolved.builder().name(name).amount(new BigDecimal(200)).resourceCredit(ResourceCredit.builder().utcStartTime(startDate.atStartOfDay()).utcEndTime(endDate.atStartOfDay()).build()).resourceType(ResourceType.builder().unit(unit).build()).build()));
    when(resourceUsageRepository.findResourceUsagesHistoryByCommunityAllocationId(UUID.fromString(communityAllocationId))).thenReturn(communityAllocationUsage);
    when(communityAllocationRepository.findById(communityAllocationId)).thenReturn(Optional.of(CommunityAllocation.builder().communityId(communityId).build()));
    String jsonForCommunity = exporter.getJsonForCommunityAllocation(communityId, communityAllocationId).get();
    ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
    CommunityResourceUsage communityResourceUsage = objectMapper.readValue(jsonForCommunity, CommunityResourceUsage.class);
    assertThat(communityResourceUsage.consumption).isEqualTo(List.of(new Consumption(XTimeAxis.get(1).atStartOfDay(), new BigDecimal("50.0")), new Consumption(XTimeAxis.get(2).atStartOfDay(), new BigDecimal("70.0")), new Consumption(XTimeAxis.get(3).atStartOfDay(), new BigDecimal("100.0")), new Consumption(XTimeAxis.get(4).atStartOfDay(), new BigDecimal("115.0")), new Consumption(XTimeAxis.get(4).atStartOfDay().plusMinutes(30), new BigDecimal("120.0")), new Consumption(XTimeAxis.get(5).atStartOfDay(), new BigDecimal("150.0")), new Consumption(XTimeAxis.get(6).atStartOfDay(), new BigDecimal("160.0")), new Consumption(XTimeAxis.get(7).atStartOfDay(), new BigDecimal("180.0")), new Consumption(XTimeAxis.get(8).atStartOfDay(), new BigDecimal("185.0"))));
}
Also used : ResourceUsage(io.imunity.furms.domain.resource_usage.ResourceUsage) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) ResourceMeasureUnit(io.imunity.furms.domain.resource_types.ResourceMeasureUnit) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceMeasureUnit

use of io.imunity.furms.domain.resource_types.ResourceMeasureUnit in project furms by unity-idm.

the class AllocationDetailsComponentFactory method create.

public static Component create(Set<ProjectAllocationChunk> allocationChunks, ResourceMeasureUnit unit) {
    ZoneId browserZoneId = UIContext.getCurrent().getZone();
    Element tableElement = new Element("table");
    tableElement.getStyle().set("width", "90%");
    tableElement.getStyle().set("text-align", "left");
    Element thead = new Element("thead");
    Tr theadRow = new Tr();
    Th amountHeader = new Th();
    amountHeader.setText(getTranslation("view.community-admin.project-allocation.inner.table.1"));
    Th receivedHeader = new Th();
    receivedHeader.setText(getTranslation("view.community-admin.project-allocation.inner.table.2"));
    Th validFromHeader = new Th();
    validFromHeader.setText(getTranslation("view.community-admin.project-allocation.inner.table.3"));
    Th validToHeader = new Th();
    validToHeader.setText(getTranslation("view.community-admin.project-allocation.inner.table.4"));
    theadRow.appendChild(amountHeader, receivedHeader, validFromHeader, validToHeader);
    thead.appendChild(theadRow);
    TBody tbody = new TBody();
    List<ProjectAllocationChunk> orderedChunks = allocationChunks.stream().sorted(Comparator.comparing((ProjectAllocationChunk chunk) -> chunk.validFrom).thenComparing(chunk -> chunk.validTo)).collect(Collectors.toList());
    for (ProjectAllocationChunk chunk : orderedChunks) {
        Tr row = new Tr();
        Td amountField = new Td();
        amountField.setText(Optional.ofNullable(chunk.amount).map(a -> new AmountWithUnit(a, unit).toString()).orElse(""));
        Td receivedField = new Td();
        receivedField.setText(Optional.ofNullable(chunk.receivedTime).map(t -> convertToZoneTime(t, browserZoneId)).map(t -> t.format(dateTimeFormatter)).orElse(""));
        Td validFrom = new Td();
        validFrom.setText(Optional.ofNullable(chunk.validFrom).map(t -> convertToZoneTime(t, browserZoneId)).map(t -> t.format(dateTimeFormatter)).orElse(""));
        Td validTo = new Td();
        validTo.setText(Optional.ofNullable(chunk.validTo).map(t -> convertToZoneTime(t, browserZoneId)).map(t -> t.format(dateTimeFormatter)).orElse(""));
        row.appendChild(amountField, receivedField, validFrom, validTo);
        tbody.appendChild(row);
    }
    tableElement.appendChild(thead, tbody);
    Div div = new AllocationDetails();
    div.getElement().appendChild(tableElement);
    return div;
}
Also used : VaadinTranslator.getTranslation(io.imunity.furms.ui.utils.VaadinTranslator.getTranslation) UTCTimeUtils.convertToZoneTime(io.imunity.furms.utils.UTCTimeUtils.convertToZoneTime) UIContext(io.imunity.furms.ui.user_context.UIContext) Component(com.vaadin.flow.component.Component) CssImport(com.vaadin.flow.component.dependency.CssImport) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) Set(java.util.Set) ProjectAllocationChunk(io.imunity.furms.domain.project_allocation_installation.ProjectAllocationChunk) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) AmountWithUnit(io.imunity.furms.domain.resource_types.AmountWithUnit) List(java.util.List) ResourceMeasureUnit(io.imunity.furms.domain.resource_types.ResourceMeasureUnit) Element(com.vaadin.flow.dom.Element) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) Optional(java.util.Optional) Comparator(java.util.Comparator) Div(com.vaadin.flow.component.html.Div) ZoneId(java.time.ZoneId) Element(com.vaadin.flow.dom.Element) ProjectAllocationChunk(io.imunity.furms.domain.project_allocation_installation.ProjectAllocationChunk) AmountWithUnit(io.imunity.furms.domain.resource_types.AmountWithUnit)

Aggregations

ResourceMeasureUnit (io.imunity.furms.domain.resource_types.ResourceMeasureUnit)3 ResourceUsage (io.imunity.furms.domain.resource_usage.ResourceUsage)2 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 HashSet (java.util.HashSet)2 Test (org.junit.jupiter.api.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Component (com.vaadin.flow.component.Component)1 CssImport (com.vaadin.flow.component.dependency.CssImport)1 Div (com.vaadin.flow.component.html.Div)1 Element (com.vaadin.flow.dom.Element)1 ProjectAllocationChunk (io.imunity.furms.domain.project_allocation_installation.ProjectAllocationChunk)1 AmountWithUnit (io.imunity.furms.domain.resource_types.AmountWithUnit)1 UIContext (io.imunity.furms.ui.user_context.UIContext)1 VaadinTranslator.getTranslation (io.imunity.furms.ui.utils.VaadinTranslator.getTranslation)1 UTCTimeUtils.convertToZoneTime (io.imunity.furms.utils.UTCTimeUtils.convertToZoneTime)1 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Comparator (java.util.Comparator)1