Skip to content

Commit

Permalink
adds spotless plugin and applies Google Java styleguide (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
jejking-tw authored Apr 27, 2024
1 parent 10c5fd0 commit 8470c54
Show file tree
Hide file tree
Showing 22 changed files with 667 additions and 596 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Run Tests and Build
run: ./gradlew clean build
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Run Linter
run: ./gradlew spotlessCheck
- name: Run Build
run: ./gradlew build
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
id("org.springframework.boot")
id("io.spring.dependency-management")
id("com.github.ben-manes.versions")
id("com.diffplug.spotless")
}

java {
Expand Down Expand Up @@ -92,4 +93,11 @@ tasks.withType<DependencyUpdatesTask> {
isNonStable(candidate.version)
}
gradleReleaseChannel="current"
}

spotless {
java {
googleJavaFormat()
formatAnnotations()
}
}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
versions_version=0.51.0
spring_boot_plugin_version=3.2.5
spring_dependency_management_plugin_version=1.1.4
spring_dependency_management_plugin_version=1.1.4
spotless_version=6.25.0
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ pluginManagement {
val versions_version: String by settings
val spring_boot_plugin_version: String by settings
val spring_dependency_management_plugin_version: String by settings
val spotless_version: String by settings
plugins {
id("io.spring.dependency-management") version spring_dependency_management_plugin_version
id("org.springframework.boot") version spring_boot_plugin_version
id("com.github.ben-manes.versions") version versions_version
id("com.diffplug.spotless") version spotless_version
}
}
124 changes: 65 additions & 59 deletions src/functional-test/java/uk/tw/energy/EndpointTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.tw.energy;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
Expand All @@ -14,69 +16,73 @@
import uk.tw.energy.builders.MeterReadingsBuilder;
import uk.tw.energy.domain.MeterReadings;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class)
public class EndpointTest {

@Autowired
private TestRestTemplate restTemplate;
@Autowired
private ObjectMapper mapper;

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

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

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

@Test
public void givenMeterIdShouldReturnAMeterReadingAssociatedWithMeterId() throws JsonProcessingException {
String smartMeterId = "bob";
populateMeterReadingsForMeter(smartMeterId);

ResponseEntity<String> response = restTemplate.getForEntity("/readings/read/" + smartMeterId, String.class);
@Autowired private TestRestTemplate restTemplate;
@Autowired private ObjectMapper mapper;

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Test
public void shouldStoreReadings() throws JsonProcessingException {
MeterReadings meterReadings = new MeterReadingsBuilder().generateElectricityReadings().build();
HttpEntity<String> entity = getStringHttpEntity(meterReadings);

@Test
public void shouldCalculateAllPrices() throws JsonProcessingException {
String smartMeterId = "bob";
populateMeterReadingsForMeter(smartMeterId);

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

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

@Test
public void givenMeterIdAndLimitShouldReturnRecommendedCheapestPricePlans() throws JsonProcessingException {
String smartMeterId = "bob";
populateMeterReadingsForMeter(smartMeterId);

ResponseEntity<String> response =
restTemplate.getForEntity("/price-plans/recommend/" + smartMeterId + "?limit=2", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}

private HttpEntity<String> getStringHttpEntity(Object object) throws JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String jsonMeterData = mapper.writeValueAsString(object);
return (HttpEntity<String>) new HttpEntity(jsonMeterData, headers);
}

private void populateMeterReadingsForMeter(String smartMeterId) throws JsonProcessingException {
MeterReadings readings = new MeterReadingsBuilder().setSmartMeterId(smartMeterId)
.generateElectricityReadings(20)
.build();

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

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

@Test
public void givenMeterIdShouldReturnAMeterReadingAssociatedWithMeterId()
throws JsonProcessingException {
String smartMeterId = "bob";
populateMeterReadingsForMeter(smartMeterId);

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

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

@Test
public void shouldCalculateAllPrices() throws JsonProcessingException {
String smartMeterId = "bob";
populateMeterReadingsForMeter(smartMeterId);

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

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

@Test
public void givenMeterIdAndLimitShouldReturnRecommendedCheapestPricePlans()
throws JsonProcessingException {
String smartMeterId = "bob";
populateMeterReadingsForMeter(smartMeterId);

ResponseEntity<String> response =
restTemplate.getForEntity(
"/price-plans/recommend/" + smartMeterId + "?limit=2", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}

private HttpEntity<String> getStringHttpEntity(Object object) throws JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String jsonMeterData = mapper.writeValueAsString(object);
return (HttpEntity<String>) new HttpEntity(jsonMeterData, headers);
}

private void populateMeterReadingsForMeter(String smartMeterId) throws JsonProcessingException {
MeterReadings readings =
new MeterReadingsBuilder()
.setSmartMeterId(smartMeterId)
.generateElectricityReadings(20)
.build();

HttpEntity<String> entity = getStringHttpEntity(readings);
restTemplate.postForEntity("/readings/store", entity, String.class);
}
}
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);
}
}
96 changes: 51 additions & 45 deletions src/main/java/uk/tw/energy/SeedingApplicationDataConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package uk.tw.energy;

import static java.util.Collections.emptyList;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
Expand All @@ -10,56 +17,55 @@
import uk.tw.energy.domain.PricePlan;
import uk.tw.energy.generator.ElectricityReadingsGenerator;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyList;

@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;
}
}
Loading

0 comments on commit 8470c54

Please sign in to comment.