Skip to content

Commit

Permalink
[Zhe] Update the response entity structure for prev week price endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhe-zhao committed Apr 6, 2021
1 parent 73955cd commit 6c907ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class PricePlanComparatorController {

public final static String PRICE_PLAN_ID_KEY = "pricePlanId";
public final static String PREV_WEEK_COST_KEY = "prevWeekCost";
public final static String PRICE_PLAN_COMPARISONS_KEY = "pricePlanComparisons";
private final PricePlanService pricePlanService;
private final AccountService accountService;
Expand Down Expand Up @@ -60,12 +61,12 @@ public ResponseEntity<Map<String, Object>> calculatedPrevNatualWeekCostForEachPr
return ResponseEntity.notFound().build();
}

Map<String, Object> pricePlanComparisons = new HashMap<>();
pricePlanComparisons.put(PRICE_PLAN_ID_KEY, pricePlanId);
pricePlanComparisons.put(PRICE_PLAN_COMPARISONS_KEY, consumptionsForPricePlans.get());
Map<String, Object> prevWeekCostSummary = new HashMap<>();
prevWeekCostSummary.put(PRICE_PLAN_ID_KEY, pricePlanId);
prevWeekCostSummary.put(PREV_WEEK_COST_KEY, consumptionsForPricePlans.get().get(pricePlanId).toString());

return consumptionsForPricePlans.isPresent()
? ResponseEntity.ok(pricePlanComparisons)
? ResponseEntity.ok(prevWeekCostSummary)
: ResponseEntity.notFound().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

import java.math.BigDecimal;
import java.time.Instant;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static uk.tw.energy.controller.PricePlanComparatorController.PREV_WEEK_COST_KEY;

public class PricePlanComparatorControllerTest {

Expand Down Expand Up @@ -74,17 +72,13 @@ public void shouldCalculateCostForMeterReadingsInPrevNatualWeek() {
int secondsInOneWeek = 3600 * 24 * 7;
ElectricityReading readingPrevWeek1 = new ElectricityReading(now.minusSeconds(secondsInOneWeek), BigDecimal.valueOf(3.0));
ElectricityReading readingPrevWeek2 = new ElectricityReading(now.minusSeconds(secondsInOneWeek + 3600), BigDecimal.valueOf(5.0));
meterReadingService.storeReadings(SMART_METER_ID, Arrays.asList(readingCurrentWeek1, readingCurrentWeek2, readingPrevWeek1, readingPrevWeek2));
meterReadingService
.storeReadings(SMART_METER_ID, Arrays.asList(readingCurrentWeek1, readingCurrentWeek2, readingPrevWeek1, readingPrevWeek2));

Map<String, BigDecimal> expectedPricePlanToCost = new HashMap<>();
expectedPricePlanToCost.put(PRICE_PLAN_1_ID, BigDecimal.valueOf(40.0).setScale(3));
expectedPricePlanToCost.put(PRICE_PLAN_2_ID, BigDecimal.valueOf(4.0).setScale(3));
expectedPricePlanToCost.put(PRICE_PLAN_3_ID, BigDecimal.valueOf(8.0).setScale(3));

Map<String, Object> expected = new HashMap<>();
expected.put(PricePlanComparatorController.PRICE_PLAN_ID_KEY, PRICE_PLAN_1_ID);
expected.put(PricePlanComparatorController.PRICE_PLAN_COMPARISONS_KEY, expectedPricePlanToCost);
assertThat(controller.calculatedPrevNatualWeekCostForEachPricePlan(SMART_METER_ID).getBody()).isEqualTo(expected);
Map<String, Object> expected = Stream.of(new String[][]{
{PricePlanComparatorController.PRICE_PLAN_ID_KEY, PRICE_PLAN_1_ID},
{PREV_WEEK_COST_KEY, BigDecimal.valueOf(40.0).setScale(3).toString()}
}).collect(Collectors.toMap(data -> data[0], data -> data[1]));
assertEquals(expected, controller.calculatedPrevNatualWeekCostForEachPricePlan((SMART_METER_ID)).getBody());
}

Expand Down

0 comments on commit 6c907ad

Please sign in to comment.