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));
}
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"))));
}
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;
}
Aggregations