From fc7e718b21e85ea1988684b69adca8592ded222f Mon Sep 17 00:00:00 2001 From: cugurlu Date: Mon, 25 Mar 2019 13:33:38 +0100 Subject: [PATCH] Add new tests --- .../src/test/java/InputValidatorTest.java | 59 +++++++++ .../server/service/CalculatorServiceTest.java | 114 ++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 src/Server/src/test/java/InputValidatorTest.java create mode 100644 src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java diff --git a/src/Server/src/test/java/InputValidatorTest.java b/src/Server/src/test/java/InputValidatorTest.java new file mode 100644 index 0000000..e55018d --- /dev/null +++ b/src/Server/src/test/java/InputValidatorTest.java @@ -0,0 +1,59 @@ +import greenify.server.InputValidator; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class InputValidatorTest { + + @Test + public void validItemIdTest() { + InputValidator inputValidator = new InputValidator(); + Assert.assertEquals(inputValidator.isValidItem("transportation_num_vehicles"), true); + Assert.assertEquals(inputValidator.isValidItem("test"), false); + } + + @Test + public void validItemValueTest() { + Assert.assertEquals(true, InputValidator + .isValidItemValue("transportation_num_vehicles", "4")); + Assert.assertEquals(false, InputValidator + .isValidItemValue("transportation_num_vehicles", "3.5")); + Assert.assertEquals(false, InputValidator.isValidItemValue( "food_grains", "hello")); + Assert.assertEquals(true, InputValidator.isValidItemValue("food_grains", "5")); + Assert.assertEquals(true, InputValidator.isValidItemValue("food_grains", "3.5")); + } + + @Test + public void getDefaultValuesTest() { + Map map = new HashMap() {{ + put("input_size", "1"); + put("input_income", "40000"); + put("transportation_num_vehicles", "1"); + put("transportation_miles1", "16100"); + put("transportation_fuels1", "2"); + put("transportation_mpg1", null); + put("transportation_miles2", "13200"); + put("transportation_fuels2", "0"); + put("transportation_mpg2", "22"); + put("transportation_publicTrans", "436"); + put("transportation_air", "3900"); + put("housing_electricity_kwh_year", "12632"); + put("housing_cleanPercent", "0"); + put("housing_naturalGas_therms_year", "472"); + put("housing_heatingOil_gallons_year", "73"); + put("housing_square_feet", "1850"); + put("housing_water_sewage", "100"); + put("food_meat_fish_eggs", "2.4"); + put("food_grains", "4.1"); + put("food_dairy", "2.2"); + put("food_fruit_vegetables", "3.5"); + put("food_snacks_drinks", "3.4"); + put("shopping_goods", "1310"); + put("shopping_services", "2413"); + } + }; + Assert.assertEquals(InputValidator.getDefaultValues(), map); + } +} diff --git a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java new file mode 100644 index 0000000..9c9926f --- /dev/null +++ b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java @@ -0,0 +1,114 @@ +package greenify.server.service; + +import static org.springframework.test.web.client.match.MockRestRequestMatchers.header; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; + +import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; + +import greenify.server.data.model.User; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.client.ExpectedCount; +import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.client.RestTemplate; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; + +@RunWith(SpringRunner.class) +public class CalculatorServiceTest { + + @TestConfiguration + static class CalculatorServiceTestConfiguration { + + RestTemplate restTemplate = new RestTemplate(); + + @Bean + public CalculatorService calculatorService() { + CalculatorService calculatorService = new CalculatorService(); + return calculatorService; + } + + @Bean + public RestTemplate restTemplate() { + return restTemplate; + } + } + + @Autowired + private CalculatorService calculatorService; + + @Autowired + private RestTemplate restTemplate; + + private MockRestServiceServer mockServer; + + @Before + public void init() { + mockServer = MockRestServiceServer.createServer(restTemplate); + } + + @Test + public void calculateFootprintTest() throws URISyntaxException { + Map map = new HashMap() {{ + put("input_location_mode", "1"); + put("input_location", "48001"); + put("input_income", "1"); + } + }; + User user = new User(1L,"greenify", "password"); + user.setFootPrintInputs(map); + mockServer.expect(ExpectedCount.once(), + //requestTo(new URI("https://apis.berkeley.edu/coolclimate/footprint-sandbox"))) + requestTo(new URI("https://apis.berkeley.edu/coolclimate/footprint-sandbox?" + + "input_location=48001&input_location_mode=1&input_income=1"))) + .andExpect(method(HttpMethod.GET)) + .andExpect(header("app_id", "a98272e3")) + .andExpect(header("app_key", "b9167c4918cb2b3143614b595065d83b")) + .andRespond(withStatus(HttpStatus.OK) + .contentType(MediaType.APPLICATION_JSON) + .body("\n" + + "\n" + + " 5.0765\n" + + " 1.167595" + + "\n" + + " 2.481474\n" + + " 2.478352\n" + + " 19.259982\n" + + "") + ); + Float footPrint = calculatorService.calculateFootprint(user); + mockServer.verify(); + Assert.assertEquals(new Float(19.259982), footPrint); + } + + @Test + public void invokeExternalServiceTest() { + CalculatorService service = new CalculatorService(); + service.restTemplate = new RestTemplate(); + Map map = new HashMap() {{ + put("input_location_mode", "1"); + put("input_location", "48001"); + put("input_income", "1"); + put("input_size", "0"); + put("input_footprint_transportation_miles1", "3"); + put("input_footprint_transportation_mpg1", "5"); + put("input_footprint_transportation_fuel1", "0"); + } + }; + Float footPrint = service.invokeExternalService(map); + Assert.assertEquals(new Float(12.743548), footPrint); + } +}