From 00b5d3afefe896098abb5d44e294c670b6163de5 Mon Sep 17 00:00:00 2001 From: viktorKhan Date: Sun, 14 Apr 2024 17:28:06 +0200 Subject: [PATCH] Fixed error mapping enum by ordinal instead of string --- .../price/sync/SyncDailyPricesService.java | 10 ++-- .../price/sync/SyncDailyPricesJob.java | 11 ++--- .../GetEnergyStationRepositoryDatabase.java | 4 +- .../production/plant/PlantEntity.java | 25 +++++----- .../create/CreatePlantRepositoryDatabase.java | 16 +++---- .../domain/production/plant/PlantMother.java | 16 ++++++- ...etEnergyStationRepositoryDatabaseTest.java | 47 +++++++++++++++++++ .../create/CreatePlantControllerTest.java | 15 +++--- 8 files changed, 98 insertions(+), 46 deletions(-) create mode 100644 src/test/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabaseTest.java diff --git a/src/main/java/org/lucoenergia/conluz/domain/price/sync/SyncDailyPricesService.java b/src/main/java/org/lucoenergia/conluz/domain/price/sync/SyncDailyPricesService.java index 97161fd..af83fdc 100644 --- a/src/main/java/org/lucoenergia/conluz/domain/price/sync/SyncDailyPricesService.java +++ b/src/main/java/org/lucoenergia/conluz/domain/price/sync/SyncDailyPricesService.java @@ -2,7 +2,7 @@ import org.lucoenergia.conluz.domain.price.PriceByHour; import org.lucoenergia.conluz.domain.price.persist.PersistOmiePricesRepository; -import org.lucoenergia.conluz.infrastructure.price.omie.get.GetPriceRepositoryInflux; +import org.lucoenergia.conluz.infrastructure.price.omie.get.GetPriceRepositoryRest; import org.springframework.stereotype.Service; import java.time.OffsetDateTime; @@ -11,16 +11,16 @@ @Service public class SyncDailyPricesService { - private final GetPriceRepositoryInflux getPriceRepository; + private final GetPriceRepositoryRest getPriceRepositoryRest; private final PersistOmiePricesRepository persistOmiePricesRepository; - public SyncDailyPricesService(GetPriceRepositoryInflux getPriceRepository, PersistOmiePricesRepository persistOmiePricesRepository) { - this.getPriceRepository = getPriceRepository; + public SyncDailyPricesService(GetPriceRepositoryRest getPriceRepositoryRest, PersistOmiePricesRepository persistOmiePricesRepository) { + this.getPriceRepositoryRest = getPriceRepositoryRest; this.persistOmiePricesRepository = persistOmiePricesRepository; } public void syncDailyPrices(OffsetDateTime day) { - List prices = getPriceRepository.getPricesByDay(day); + List prices = getPriceRepositoryRest.getPricesByDay(day); persistOmiePricesRepository.persistPrices(prices); } } diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/price/sync/SyncDailyPricesJob.java b/src/main/java/org/lucoenergia/conluz/infrastructure/price/sync/SyncDailyPricesJob.java index b8c579d..3c5cef9 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/price/sync/SyncDailyPricesJob.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/price/sync/SyncDailyPricesJob.java @@ -24,16 +24,11 @@ public SyncDailyPricesJob(TimeConfiguration timeConfiguration, SyncDailyPricesSe } /** - * Aggregate instant Shelly consumptions by hour every hour. - * The first field is for seconds. '0' here means at the beginning of the minute. - * The second field is for the minute field. '0' here means the cron job gets executed every time the minute is '0', or, in other words, at the beginning of each hour. - * The third field is for the hour. - * The fourth field is for the day of the month. A '*' means "every day". - * The fifth field is for the month. A '*' means "every month". - * The sixth and final field is for the day of the week. A '*' means "every day of the week". + * This method is executed based on a cron expression to retrieve OMIE prices daily. + * It retrieves the current date and time and passes it to synchronize the daily prices. */ @Override - @Scheduled(cron = "0 6 * * * *") + @Scheduled(cron = "0 0 6 * * * ") public void run() { LOGGER.info("OMIE prices daily retrieval started..."); diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabase.java b/src/main/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabase.java index feb59bd..5ab28ad 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabase.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabase.java @@ -26,7 +26,7 @@ public List findAll() { List entities = plantRepository.findAll(); return entities.stream() .map(this::mapEntityToDomain) - .collect(Collectors.toList()); + .toList(); } @Override @@ -34,7 +34,7 @@ public List findAllByInverterProvider(InverterProvider provider) { List entities = plantRepository.findAllByInverterProvider(provider); return entities.stream() .map(this::mapEntityToDomain) - .collect(Collectors.toList()); + .toList(); } private Plant mapEntityToDomain(PlantEntity entity) { diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/PlantEntity.java b/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/PlantEntity.java index 3b1b6d1..7d21c21 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/PlantEntity.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/PlantEntity.java @@ -1,14 +1,10 @@ package org.lucoenergia.conluz.infrastructure.production.plant; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.Id; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; import org.lucoenergia.conluz.domain.production.InverterProvider; import org.lucoenergia.conluz.infrastructure.admin.user.UserEntity; import java.time.LocalDate; -import java.time.OffsetDateTime; import java.util.Objects; import java.util.UUID; @@ -23,6 +19,7 @@ public class PlantEntity { private UserEntity user; private String address; private String description; + @Enumerated(EnumType.STRING) private InverterProvider inverterProvider; /** * Represented using kWp @@ -127,47 +124,47 @@ public static class Builder { private Double totalPower; private LocalDate connectionDate; - public Builder setId(UUID id) { + public Builder withId(UUID id) { this.id = id; return this; } - public Builder setName(String name) { + public Builder withName(String name) { this.name = name; return this; } - public Builder setCode(String code) { + public Builder withCode(String code) { this.code = code; return this; } - public Builder setUser(UserEntity user) { + public Builder withUser(UserEntity user) { this.user = user; return this; } - public Builder setAddress(String address) { + public Builder withAddress(String address) { this.address = address; return this; } - public Builder setDescription(String description) { + public Builder withDescription(String description) { this.description = description; return this; } - public Builder setInverterProvider(InverterProvider inverterProvider) { + public Builder withInverterProvider(InverterProvider inverterProvider) { this.inverterProvider = inverterProvider; return this; } - public Builder setTotalPower(Double totalPower) { + public Builder withTotalPower(Double totalPower) { this.totalPower = totalPower; return this; } - public Builder setConnectionDate(LocalDate connectionDate) { + public Builder withConnectionDate(LocalDate connectionDate) { this.connectionDate = connectionDate; return this; } diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantRepositoryDatabase.java b/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantRepositoryDatabase.java index a039e86..42a6da5 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantRepositoryDatabase.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantRepositoryDatabase.java @@ -47,14 +47,14 @@ public Plant create(Plant plant, UserId id) { UserEntity userEntity = result.get(); PlantEntity plantEntity = new PlantEntity.Builder() - .setId(UUID.randomUUID()) - .setCode(plant.getCode()) - .setName(plant.getName()) - .setAddress(plant.getAddress()) - .setDescription(plant.getDescription()) - .setInverterProvider(plant.getInverterProvider()) - .setTotalPower(plant.getTotalPower()) - .setConnectionDate(plant.getConnectionDate()) + .withId(UUID.randomUUID()) + .withCode(plant.getCode()) + .withName(plant.getName()) + .withAddress(plant.getAddress()) + .withDescription(plant.getDescription()) + .withInverterProvider(plant.getInverterProvider()) + .withTotalPower(plant.getTotalPower()) + .withConnectionDate(plant.getConnectionDate()) .build(); userEntity.addPlant(plantEntity); diff --git a/src/test/java/org/lucoenergia/conluz/domain/production/plant/PlantMother.java b/src/test/java/org/lucoenergia/conluz/domain/production/plant/PlantMother.java index 3a7a0ca..6cc807d 100644 --- a/src/test/java/org/lucoenergia/conluz/domain/production/plant/PlantMother.java +++ b/src/test/java/org/lucoenergia/conluz/domain/production/plant/PlantMother.java @@ -1,10 +1,10 @@ package org.lucoenergia.conluz.domain.production.plant; import org.apache.commons.lang3.RandomStringUtils; -import org.lucoenergia.conluz.domain.admin.supply.Supply; import org.lucoenergia.conluz.domain.admin.user.User; import org.lucoenergia.conluz.domain.admin.user.UserMother; import org.lucoenergia.conluz.domain.production.InverterProvider; +import org.lucoenergia.conluz.infrastructure.production.plant.PlantEntity; import java.time.LocalDate; import java.util.Random; @@ -28,4 +28,18 @@ public static Plant.Builder random(User user) { .withAddress(RandomStringUtils.random(20, true, true)) .withUser(user); } + + + public static PlantEntity.Builder randomPlantEntity() { + return new PlantEntity.Builder() + .withId(UUID.randomUUID()) + .withCode(RandomStringUtils.random(20, true, true)) + .withName(RandomStringUtils.random(10, true, false)) + .withDescription(RandomStringUtils.random(30, true, false)) + .withTotalPower(new Random().nextDouble()) + .withInverterProvider(InverterProvider.HUAWEI) + .withConnectionDate(LocalDate.now()) + .withAddress(RandomStringUtils.random(20, true, true)) + .withUser(UserMother.randomUserEntity()); + } } diff --git a/src/test/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabaseTest.java b/src/test/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabaseTest.java new file mode 100644 index 0000000..a3712f6 --- /dev/null +++ b/src/test/java/org/lucoenergia/conluz/infrastructure/production/get/GetEnergyStationRepositoryDatabaseTest.java @@ -0,0 +1,47 @@ +package org.lucoenergia.conluz.infrastructure.production.get; + +import org.lucoenergia.conluz.domain.admin.user.UserMother; +import org.lucoenergia.conluz.domain.production.plant.Plant; +import org.lucoenergia.conluz.domain.production.InverterProvider; +import org.lucoenergia.conluz.domain.production.plant.PlantMother; +import org.lucoenergia.conluz.infrastructure.admin.user.UserEntity; +import org.lucoenergia.conluz.infrastructure.admin.user.UserRepository; +import org.lucoenergia.conluz.infrastructure.production.plant.PlantEntity; +import org.lucoenergia.conluz.infrastructure.production.plant.PlantRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.lucoenergia.conluz.infrastructure.shared.BaseIntegrationTest; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +@Transactional +class GetEnergyStationRepositoryDatabaseTest extends BaseIntegrationTest { + + @Autowired + private GetEnergyStationRepositoryDatabase getEnergyStationRepositoryDatabase; + @Autowired + private PlantRepository plantRepository; + @Autowired + private UserRepository userRepository; + + @Test + void testFindAllByInverterProvider() { + UserEntity user = userRepository.save(UserMother.randomUserEntity()); + PlantEntity plantEntity1 = plantRepository.save(PlantMother.randomPlantEntity().withUser(user).build()); + PlantEntity plantEntity2 = plantRepository.save(PlantMother.randomPlantEntity().withUser(user).build()); + + List expectedPlants = getEnergyStationRepositoryDatabase.findAllByInverterProvider(InverterProvider.HUAWEI); + + Assertions.assertEquals(2, expectedPlants.size()); + Assertions.assertEquals(plantEntity1.getId(), expectedPlants.get(0).getId()); + Assertions.assertEquals(plantEntity2.getId(), expectedPlants.get(1).getId()); + } +} \ No newline at end of file diff --git a/src/test/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantControllerTest.java b/src/test/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantControllerTest.java index e189a01..1f32c57 100644 --- a/src/test/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantControllerTest.java +++ b/src/test/java/org/lucoenergia/conluz/infrastructure/production/plant/create/CreatePlantControllerTest.java @@ -4,7 +4,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.lucoenergia.conluz.domain.admin.user.User; import org.lucoenergia.conluz.domain.admin.user.UserMother; import org.lucoenergia.conluz.domain.production.InverterProvider; import org.lucoenergia.conluz.infrastructure.admin.user.UserEntity; @@ -277,13 +276,13 @@ void testWithDuplicatedPlant() throws Exception { String plantCode = "PS-456798"; plantRepository.save(new PlantEntity.Builder() - .setCode(plantCode) - .setTotalPower(23D) - .setAddress("Fake Street 123") - .setInverterProvider(InverterProvider.HUAWEI) - .setId(UUID.randomUUID()) - .setName("A name") - .setUser(user) + .withCode(plantCode) + .withTotalPower(23D) + .withAddress("Fake Street 123") + .withInverterProvider(InverterProvider.HUAWEI) + .withId(UUID.randomUUID()) + .withName("A name") + .withUser(user) .build()); String body = String.format("""