Skip to content

Commit

Permalink
Apply the Palantir format instead of Google, to be closer to the Inte…
Browse files Browse the repository at this point in the history
…lliJ default
  • Loading branch information
xpmatteo authored and jejking-tw committed May 31, 2024
1 parent 4c8d3c6 commit 1b19aa4
Show file tree
Hide file tree
Showing 17 changed files with 549 additions and 592 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ tasks.withType<DependencyUpdatesTask> {

spotless {
java {
googleJavaFormat()
palantirJavaFormat()
formatAnnotations()
}
}
160 changes: 77 additions & 83 deletions src/functional-test/java/uk/tw/energy/EndpointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,89 +23,83 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class)
public class EndpointTest {

@Autowired private TestRestTemplate restTemplate;

private static HttpEntity<MeterReadings> toHttpEntity(MeterReadings meterReadings) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(meterReadings, headers);
}

@Test
public void shouldStoreReadings() {
MeterReadings meterReadings = new MeterReadingsBuilder().generateElectricityReadings().build();
HttpEntity<MeterReadings> entity = toHttpEntity(meterReadings);

ResponseEntity<String> response =
@Autowired
private TestRestTemplate restTemplate;

private static HttpEntity<MeterReadings> toHttpEntity(MeterReadings meterReadings) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(meterReadings, headers);
}

@Test
public void shouldStoreReadings() {
MeterReadings meterReadings =
new MeterReadingsBuilder().generateElectricityReadings().build();
HttpEntity<MeterReadings> entity = toHttpEntity(meterReadings);

ResponseEntity<String> response = restTemplate.postForEntity("/readings/store", entity, String.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}

@SuppressWarnings("DataFlowIssue")
@Test
public void givenMeterIdShouldReturnAMeterReadingAssociatedWithMeterId() {
String smartMeterId = "alice";
List<ElectricityReading> data = List.of(
new ElectricityReading(Instant.parse("2024-04-26T00:00:10.00Z"), new BigDecimal(10)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:20.00Z"), new BigDecimal(20)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:30.00Z"), new BigDecimal(30)));
populateReadingsForMeter(smartMeterId, data);

ResponseEntity<ElectricityReading[]> response =
restTemplate.getForEntity("/readings/read/" + smartMeterId, ElectricityReading[].class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(Arrays.asList(response.getBody())).isEqualTo(data);
}

@Test
public void shouldCalculateAllPrices() {
String smartMeterId = "bob";
List<ElectricityReading> data = List.of(
new ElectricityReading(Instant.parse("2024-04-26T00:00:10.00Z"), new BigDecimal(10)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:20.00Z"), new BigDecimal(20)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:30.00Z"), new BigDecimal(30)));
populateReadingsForMeter(smartMeterId, data);

ResponseEntity<CompareAllResponse> response =
restTemplate.getForEntity("/price-plans/compare-all/" + smartMeterId, CompareAllResponse.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody())
.isEqualTo(new CompareAllResponse(
Map.of("price-plan-0", 36000, "price-plan-1", 7200, "price-plan-2", 3600), null));
}

@SuppressWarnings("rawtypes")
@Test
public void givenMeterIdAndLimitShouldReturnRecommendedCheapestPricePlans() {
String smartMeterId = "jane";
List<ElectricityReading> data = List.of(
new ElectricityReading(Instant.parse("2024-04-26T00:00:10.00Z"), new BigDecimal(10)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:20.00Z"), new BigDecimal(20)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:30.00Z"), new BigDecimal(30)));
populateReadingsForMeter(smartMeterId, data);

ResponseEntity<Map[]> response =
restTemplate.getForEntity("/price-plans/recommend/" + smartMeterId + "?limit=2", Map[].class);

assertThat(response.getBody()).containsExactly(Map.of("price-plan-2", 3600), Map.of("price-plan-1", 7200));
}

private void populateReadingsForMeter(String smartMeterId, List<ElectricityReading> data) {
MeterReadings readings = new MeterReadings(smartMeterId, data);

HttpEntity<MeterReadings> entity = toHttpEntity(readings);
restTemplate.postForEntity("/readings/store", entity, String.class);
}

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}

@SuppressWarnings("DataFlowIssue")
@Test
public void givenMeterIdShouldReturnAMeterReadingAssociatedWithMeterId() {
String smartMeterId = "alice";
List<ElectricityReading> data =
List.of(
new ElectricityReading(Instant.parse("2024-04-26T00:00:10.00Z"), new BigDecimal(10)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:20.00Z"), new BigDecimal(20)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:30.00Z"), new BigDecimal(30)));
populateReadingsForMeter(smartMeterId, data);

ResponseEntity<ElectricityReading[]> response =
restTemplate.getForEntity("/readings/read/" + smartMeterId, ElectricityReading[].class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(Arrays.asList(response.getBody())).isEqualTo(data);
}

@Test
public void shouldCalculateAllPrices() {
String smartMeterId = "bob";
List<ElectricityReading> data =
List.of(
new ElectricityReading(Instant.parse("2024-04-26T00:00:10.00Z"), new BigDecimal(10)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:20.00Z"), new BigDecimal(20)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:30.00Z"), new BigDecimal(30)));
populateReadingsForMeter(smartMeterId, data);

ResponseEntity<CompareAllResponse> response =
restTemplate.getForEntity(
"/price-plans/compare-all/" + smartMeterId, CompareAllResponse.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody())
.isEqualTo(
new CompareAllResponse(
Map.of("price-plan-0", 36000, "price-plan-1", 7200, "price-plan-2", 3600), null));
}

@SuppressWarnings("rawtypes")
@Test
public void givenMeterIdAndLimitShouldReturnRecommendedCheapestPricePlans() {
String smartMeterId = "jane";
List<ElectricityReading> data =
List.of(
new ElectricityReading(Instant.parse("2024-04-26T00:00:10.00Z"), new BigDecimal(10)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:20.00Z"), new BigDecimal(20)),
new ElectricityReading(Instant.parse("2024-04-26T00:00:30.00Z"), new BigDecimal(30)));
populateReadingsForMeter(smartMeterId, data);

ResponseEntity<Map[]> response =
restTemplate.getForEntity(
"/price-plans/recommend/" + smartMeterId + "?limit=2", Map[].class);

assertThat(response.getBody())
.containsExactly(Map.of("price-plan-2", 3600), Map.of("price-plan-1", 7200));
}

private void populateReadingsForMeter(String smartMeterId, List<ElectricityReading> data) {
MeterReadings readings = new MeterReadings(smartMeterId, data);

HttpEntity<MeterReadings> entity = toHttpEntity(readings);
restTemplate.postForEntity("/readings/store", entity, String.class);
}

record CompareAllResponse(Map<String, Integer> pricePlanComparisons, String pricePlanId) {}
record CompareAllResponse(Map<String, Integer> pricePlanComparisons, String pricePlanId) {}
}
6 changes: 3 additions & 3 deletions src/main/java/uk/tw/energy/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class);
}
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
81 changes: 37 additions & 44 deletions src/main/java/uk/tw/energy/SeedingApplicationDataConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,45 @@
@Configuration
public class SeedingApplicationDataConfiguration {

private static final String MOST_EVIL_PRICE_PLAN_ID = "price-plan-0";
private static final String RENEWABLES_PRICE_PLAN_ID = "price-plan-1";
private static final String STANDARD_PRICE_PLAN_ID = "price-plan-2";
private static final String MOST_EVIL_PRICE_PLAN_ID = "price-plan-0";
private static final String RENEWABLES_PRICE_PLAN_ID = "price-plan-1";
private static final String STANDARD_PRICE_PLAN_ID = "price-plan-2";

@Bean
public List<PricePlan> pricePlans() {
final List<PricePlan> pricePlans = new ArrayList<>();
pricePlans.add(
new PricePlan(
MOST_EVIL_PRICE_PLAN_ID, "Dr Evil's Dark Energy", BigDecimal.TEN, emptyList()));
pricePlans.add(
new PricePlan(
RENEWABLES_PRICE_PLAN_ID, "The Green Eco", BigDecimal.valueOf(2), emptyList()));
pricePlans.add(
new PricePlan(STANDARD_PRICE_PLAN_ID, "Power for Everyone", BigDecimal.ONE, emptyList()));
return pricePlans;
}
@Bean
public List<PricePlan> pricePlans() {
final List<PricePlan> pricePlans = new ArrayList<>();
pricePlans.add(new PricePlan(MOST_EVIL_PRICE_PLAN_ID, "Dr Evil's Dark Energy", BigDecimal.TEN, emptyList()));
pricePlans.add(new PricePlan(RENEWABLES_PRICE_PLAN_ID, "The Green Eco", BigDecimal.valueOf(2), emptyList()));
pricePlans.add(new PricePlan(STANDARD_PRICE_PLAN_ID, "Power for Everyone", BigDecimal.ONE, emptyList()));
return pricePlans;
}

@Bean
public Map<String, List<ElectricityReading>> perMeterElectricityReadings() {
final Map<String, List<ElectricityReading>> readings = new HashMap<>();
final ElectricityReadingsGenerator electricityReadingsGenerator =
new ElectricityReadingsGenerator();
smartMeterToPricePlanAccounts()
.keySet()
.forEach(
smartMeterId -> readings.put(smartMeterId, electricityReadingsGenerator.generate(20)));
return readings;
}
@Bean
public Map<String, List<ElectricityReading>> perMeterElectricityReadings() {
final Map<String, List<ElectricityReading>> readings = new HashMap<>();
final ElectricityReadingsGenerator electricityReadingsGenerator = new ElectricityReadingsGenerator();
smartMeterToPricePlanAccounts()
.keySet()
.forEach(smartMeterId -> readings.put(smartMeterId, electricityReadingsGenerator.generate(20)));
return readings;
}

@Bean
public Map<String, String> smartMeterToPricePlanAccounts() {
final Map<String, String> smartMeterToPricePlanAccounts = new HashMap<>();
smartMeterToPricePlanAccounts.put("smart-meter-0", MOST_EVIL_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-1", RENEWABLES_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-2", MOST_EVIL_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-3", STANDARD_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-4", RENEWABLES_PRICE_PLAN_ID);
return smartMeterToPricePlanAccounts;
}
@Bean
public Map<String, String> smartMeterToPricePlanAccounts() {
final Map<String, String> smartMeterToPricePlanAccounts = new HashMap<>();
smartMeterToPricePlanAccounts.put("smart-meter-0", MOST_EVIL_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-1", RENEWABLES_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-2", MOST_EVIL_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-3", STANDARD_PRICE_PLAN_ID);
smartMeterToPricePlanAccounts.put("smart-meter-4", RENEWABLES_PRICE_PLAN_ID);
return smartMeterToPricePlanAccounts;
}

@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
}
53 changes: 26 additions & 27 deletions src/main/java/uk/tw/energy/controller/MeterReadingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,35 @@
@RequestMapping("/readings")
public class MeterReadingController {

private final MeterReadingService meterReadingService;
private final MeterReadingService meterReadingService;

public MeterReadingController(MeterReadingService meterReadingService) {
this.meterReadingService = meterReadingService;
}
public MeterReadingController(MeterReadingService meterReadingService) {
this.meterReadingService = meterReadingService;
}

@PostMapping("/store")
public ResponseEntity storeReadings(@RequestBody MeterReadings meterReadings) {
if (!isMeterReadingsValid(meterReadings)) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
@PostMapping("/store")
public ResponseEntity storeReadings(@RequestBody MeterReadings meterReadings) {
if (!isMeterReadingsValid(meterReadings)) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
meterReadingService.storeReadings(meterReadings.smartMeterId(), meterReadings.electricityReadings());
return ResponseEntity.ok().build();
}
meterReadingService.storeReadings(
meterReadings.smartMeterId(), meterReadings.electricityReadings());
return ResponseEntity.ok().build();
}

private boolean isMeterReadingsValid(MeterReadings meterReadings) {
String smartMeterId = meterReadings.smartMeterId();
List<ElectricityReading> electricityReadings = meterReadings.electricityReadings();
return smartMeterId != null
&& !smartMeterId.isEmpty()
&& electricityReadings != null
&& !electricityReadings.isEmpty();
}
private boolean isMeterReadingsValid(MeterReadings meterReadings) {
String smartMeterId = meterReadings.smartMeterId();
List<ElectricityReading> electricityReadings = meterReadings.electricityReadings();
return smartMeterId != null
&& !smartMeterId.isEmpty()
&& electricityReadings != null
&& !electricityReadings.isEmpty();
}

@GetMapping("/read/{smartMeterId}")
public ResponseEntity readReadings(@PathVariable String smartMeterId) {
Optional<List<ElectricityReading>> readings = meterReadingService.getReadings(smartMeterId);
return readings.isPresent()
? ResponseEntity.ok(readings.get())
: ResponseEntity.notFound().build();
}
@GetMapping("/read/{smartMeterId}")
public ResponseEntity readReadings(@PathVariable String smartMeterId) {
Optional<List<ElectricityReading>> readings = meterReadingService.getReadings(smartMeterId);
return readings.isPresent()
? ResponseEntity.ok(readings.get())
: ResponseEntity.notFound().build();
}
}
Loading

0 comments on commit 1b19aa4

Please sign in to comment.