Skip to content

Commit

Permalink
Fixed error mapping enum by ordinal instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorKhan committed Apr 14, 2024
1 parent 99e698c commit 00b5d3a
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<PriceByHour> prices = getPriceRepository.getPricesByDay(day);
List<PriceByHour> prices = getPriceRepositoryRest.getPricesByDay(day);
persistOmiePricesRepository.persistPrices(prices);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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...");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public List<Plant> findAll() {
List<PlantEntity> entities = plantRepository.findAll();
return entities.stream()
.map(this::mapEntityToDomain)
.collect(Collectors.toList());
.toList();
}

@Override
public List<Plant> findAllByInverterProvider(InverterProvider provider) {
List<PlantEntity> entities = plantRepository.findAllByInverterProvider(provider);
return entities.stream()
.map(this::mapEntityToDomain)
.collect(Collectors.toList());
.toList();
}

private Plant mapEntityToDomain(PlantEntity entity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<Plant> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("""
Expand Down

0 comments on commit 00b5d3a

Please sign in to comment.