diff --git a/src/Common/src/main/java/greenify/common/ApplicationException.java b/src/Common/src/main/java/greenify/common/ApplicationException.java index f64b9fd..d748ea6 100644 --- a/src/Common/src/main/java/greenify/common/ApplicationException.java +++ b/src/Common/src/main/java/greenify/common/ApplicationException.java @@ -2,7 +2,7 @@ package greenify.common; public class ApplicationException extends RuntimeException { /** - * This method returns an application exception message. + * This method sends an application exception message. * @param message the exception message */ public ApplicationException(String message) { diff --git a/src/Common/src/main/java/greenify/common/ErrorResponse.java b/src/Common/src/main/java/greenify/common/ErrorResponse.java index df449c6..72ab1b9 100644 --- a/src/Common/src/main/java/greenify/common/ErrorResponse.java +++ b/src/Common/src/main/java/greenify/common/ErrorResponse.java @@ -17,7 +17,7 @@ public class ErrorResponse { public ErrorResponse() { } /** - * This method returns the message. + * This method gets the message. * @return the message */ public String getMessage() { diff --git a/src/Common/src/main/java/greenify/common/UserDto.java b/src/Common/src/main/java/greenify/common/UserDto.java index 2b83014..d2b9d4f 100644 --- a/src/Common/src/main/java/greenify/common/UserDto.java +++ b/src/Common/src/main/java/greenify/common/UserDto.java @@ -6,6 +6,7 @@ resorting to remote interfaces (e.g., web services), where each call is an expensive operation. */ + package greenify.common; public class UserDto { diff --git a/src/Common/src/test/java/ErrorResponseTest.java b/src/Common/src/test/java/ErrorResponseTest.java index 05892e1..9cce02b 100644 --- a/src/Common/src/test/java/ErrorResponseTest.java +++ b/src/Common/src/test/java/ErrorResponseTest.java @@ -1,4 +1,5 @@ import static org.junit.Assert.assertEquals; + import greenify.common.ErrorResponse; import org.junit.Test; diff --git a/src/Common/src/test/java/UserDtoTest.java b/src/Common/src/test/java/UserDtoTest.java index 565ca0f..90514ae 100644 --- a/src/Common/src/test/java/UserDtoTest.java +++ b/src/Common/src/test/java/UserDtoTest.java @@ -1,4 +1,5 @@ import static org.junit.Assert.assertEquals; + import greenify.common.UserDto; import org.junit.Test; diff --git a/src/Server/src/main/java/greenify/server/InputValidator.java b/src/Server/src/main/java/greenify/server/InputValidator.java index a17b575..9b3f0dc 100644 --- a/src/Server/src/main/java/greenify/server/InputValidator.java +++ b/src/Server/src/main/java/greenify/server/InputValidator.java @@ -4,68 +4,69 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; public class InputValidator { private static final List inputItems = Arrays.asList( new InputItem("input_size", false, "1"), new InputItem("input_income", false, "40000"), - new InputItem("transportation_num_vehicles", false, "1" ), + new InputItem("transportation_num_vehicles", false, "1"), new InputItem("transportation_miles1", false, "16100", false), - new InputItem("transportation_fuels1", false, "2" , false), - new InputItem("transportation_mpg1", false, null, false ), - new InputItem("transportation_miles2", false, "13200", false ), - new InputItem("transportation_fuels2", false, "0" , false), - new InputItem("transportation_mpg2", false, "22" ,false), - new InputItem("transportation_publicTrans", false, "436" ), - new InputItem("transportation_air", false, "3900" ), - new InputItem("housing_electricity_kwh_year", false, "12632" ), - new InputItem("housing_cleanPercent", false, "0" ), - new InputItem("housing_naturalGas_therms_year", false, "472" ), - new InputItem("housing_heatingOil_gallons_year", false, "73" ), - new InputItem("housing_square_feet", false, "1850" ), - new InputItem("housing_water_sewage", false, "100" ), - new InputItem("food_meat_fish_eggs", true, "2.4" ), - new InputItem("food_grains", true, "4.1" ), - new InputItem("food_dairy", true, "2.2" ), - new InputItem("food_fruit_vegetables", true, "3.5" ), - new InputItem("food_snacks_drinks", true, "3.4" ), - new InputItem("shopping_goods", false, "1310" ), - new InputItem("shopping_services", false, "2413" ) + new InputItem("transportation_fuels1", false, "2", false), + new InputItem("transportation_mpg1", false, null, false), + new InputItem("transportation_miles2", false, "13200", false), + new InputItem("transportation_fuels2", false, "0", false), + new InputItem("transportation_mpg2", false, "22", false), + new InputItem("transportation_publicTrans", false, "436"), + new InputItem("transportation_air", false, "3900"), + new InputItem("housing_electricity_kwh_year", false, "12632"), + new InputItem("housing_cleanPercent", false, "0"), + new InputItem("housing_naturalGas_therms_year", false, "472"), + new InputItem("housing_heatingOil_gallons_year", false, "73"), + new InputItem("housing_square_feet", false, "1850"), + new InputItem("housing_water_sewage", false, "100"), + new InputItem("food_meat_fish_eggs", true, "2.4"), + new InputItem("food_grains", true, "4.1"), + new InputItem("food_dairy", true, "2.2"), + new InputItem("food_fruit_vegetables", true, "3.5"), + new InputItem("food_snacks_drinks", true, "3.4"), + new InputItem("shopping_goods", false, "1310"), + new InputItem("shopping_services", false, "2413") ); /** - * The method checks that the id is valid or not. + * The method checks whether the id is valid or not. * @param inputName the name of input * @return true or false */ public static Boolean isValidItem(String inputName) { - return inputItems.stream().filter(i -> i.getName() == inputName).findAny().isPresent(); + return inputItems.stream().anyMatch(i -> i.getName().equals(inputName)); } /** - * The method checks that the item value is valid or not. + * The method checks whether the item value is valid or not. * @param inputName the name of input * @param value the value of item * @return true or false */ public static boolean isValidItemValue(String inputName, String value) { InputItem item = null; - for (int i = 0; i < inputItems.size(); i++) { - if (inputItems.get(i).getName() == inputName) { - item = inputItems.get(i); + for (InputItem inputItem : inputItems) { + if (inputItem.getName().equals(inputName)) { + item = inputItem; } } - if (item.getFloat()) { + if (Objects.requireNonNull(item).getFloat()) { try { - Float number = Float.parseFloat(value); + Float.parseFloat(value); } catch (NumberFormatException | NullPointerException nfe) { return false; } return true; } else { try { - Integer number = Integer.parseInt(value); + Integer.parseInt(value); } catch (NumberFormatException | NullPointerException nfe) { return false; } @@ -74,13 +75,13 @@ public class InputValidator { } /** - * getter for default values. + * This method gets default values. * @return the map of default values */ public static Map getDefaultValues() { - Map map = new HashMap(){}; - for (int i = 0; i < inputItems.size(); i++) { - map.put(inputItems.get(i).getName(), inputItems.get(i).getDefaultValue()); + Map map = new HashMap() { }; + for (InputItem inputItem : inputItems) { + map.put(inputItem.getName(), inputItem.getDefaultValue()); } return map; } diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index 595947b..d126aeb 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -57,7 +57,7 @@ public class User { } /** - * This method returns the ID of the user. + * This method gets the ID of the user. * @return the id of the user */ public Long getId() { @@ -73,7 +73,7 @@ public class User { } /** - * This method returns the name of the user. + * This method gets the name of the user. * @return the name of the user */ public String getName() { @@ -89,7 +89,7 @@ public class User { } /** - * This method returns the password of the user. + * This method gets the password of the user. * @return the password of the user */ public String getPassword() { @@ -105,7 +105,7 @@ public class User { } /** - * This method returns the footPrint of user. + * This method gets the footPrint of user. * @return the footprint of the user */ public Float getFootPrint() { @@ -121,7 +121,7 @@ public class User { } /** - * This method returns the footprint inputs of the user. + * This method gets the footprint inputs of the user. * @return footprint inputs of the user */ public Map getFootPrintInputs() { @@ -155,7 +155,7 @@ public class User { } /** - * This method returns a human readable (JSON) object. + * This method gets a human readable (JSON) object. * @return the JSON form of the object. */ @Override @@ -195,7 +195,7 @@ public class User { } /** - * This method returns the hashcode of a user. + * This method gets the hashcode of a user. * @return hashcode of a user */ @Override diff --git a/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java b/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java index d4657cc..ff5d444 100644 --- a/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java +++ b/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java @@ -7,12 +7,14 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; -//class that handles exceptions for the rest server +/** + * This class handles exceptions for the REST server. + */ @RestControllerAdvice -public class RestExceptionHandler { +class RestExceptionHandler { @ExceptionHandler(ApplicationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public static ErrorResponse applicationException(ApplicationException ex) { + static ErrorResponse applicationException(ApplicationException ex) { return new ErrorResponse(ex.getMessage()); } } diff --git a/src/Server/src/main/java/greenify/server/rest/UserController.java b/src/Server/src/main/java/greenify/server/rest/UserController.java index 05ed9db..2d7d3c7 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController; public class UserController { @Autowired private - UserService userService; + UserService userService; /** * This method registers the user. diff --git a/src/Server/src/main/java/greenify/server/service/CalculatorService.java b/src/Server/src/main/java/greenify/server/service/CalculatorService.java index 7ad64b4..dbf9a65 100644 --- a/src/Server/src/main/java/greenify/server/service/CalculatorService.java +++ b/src/Server/src/main/java/greenify/server/service/CalculatorService.java @@ -16,21 +16,27 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import java.util.Map; +import java.util.Objects; @Service -public class CalculatorService { - Logger logger = LoggerFactory.getLogger(UserService.class); - +class CalculatorService { @Autowired RestTemplate restTemplate; + private Logger logger = LoggerFactory.getLogger(UserService.class); + @Bean RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } - protected Float invokeExternalService(Map map) { - /** + /** + * This method invokes the external service that calculates a footprint. + * @param map used variables to calculate a footprint + * @return a footprint + */ + Float invokeExternalService(Map map) { + /* * curl -X GET "https://apis.berkeley.edu/coolclimate/footprint-sandbox?input_location_mode=1 * &input_location=48001&input_income=1&input_size=0&input_footprint_transportation_miles1=3 * &input_footprint_transportation_mpg1=5&input_footprint_transportation_fuel1=0" @@ -52,7 +58,7 @@ public class CalculatorService { entity, String.class); logger.info(response.getStatusCode().toString()); logger.info(response.getBody()); - String result = response.getBody().substring(response.getBody() + String result = response.getBody().substring(Objects.requireNonNull(response.getBody()) .indexOf("") + 20, response.getBody().indexOf("")); // to do: in not HTTP 200 or exception case throws exception @@ -60,7 +66,12 @@ public class CalculatorService { return Float.parseFloat(result); } - public Float calculateFootprint(User user) { + /** + * The method calculates a users footprint. + * @param user the user + * @return the footprint of the user + */ + Float calculateFootprint(User user) { return invokeExternalService(user.getFootPrintInputs()); } } diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 0f1ec9a..901a0ea 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -14,13 +14,13 @@ import org.springframework.web.bind.annotation.ResponseBody; @Service public class UserService { - private Logger logger = LoggerFactory.getLogger(UserService.class); + @Autowired + CalculatorService calculatorService; @Autowired UserRepository userRepository; - @Autowired - CalculatorService calculatorService; + private Logger logger = LoggerFactory.getLogger(UserService.class); /** * This method registers the user. @@ -107,7 +107,7 @@ public class UserService { } /** - * This method returns the input value of an input. + * This method gets the input value of an input. * @param name of the user * @param inputName name of the input * @return input value @@ -122,7 +122,7 @@ public class UserService { } /** - * This method returns the footprint of a user. + * This method gets the footprint of a user. * @param name name of the user * @return footprint of the user */ @@ -132,7 +132,7 @@ public class UserService { } /** - * This method returns a JSON of XML with all users. + * This method gets a JSON of XML with all users. * @return JSON/XML of all users */ @GetMapping(path = "/all") diff --git a/src/Server/src/test/java/ApplicationTest.java b/src/Server/src/test/java/ApplicationTest.java index 3e39948..caba9de 100644 --- a/src/Server/src/test/java/ApplicationTest.java +++ b/src/Server/src/test/java/ApplicationTest.java @@ -8,6 +8,6 @@ import org.springframework.test.context.junit4.SpringRunner; public class ApplicationTest { @Test - public void contextLoads() throws Exception{ } + public void contextLoads() { } } diff --git a/src/Server/src/test/java/InputValidatorTest.java b/src/Server/src/test/java/InputValidatorTest.java index e55018d..d80794a 100644 --- a/src/Server/src/test/java/InputValidatorTest.java +++ b/src/Server/src/test/java/InputValidatorTest.java @@ -1,5 +1,8 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import greenify.server.InputValidator; -import org.junit.Assert; import org.junit.Test; import java.util.HashMap; @@ -9,20 +12,20 @@ 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); + new InputValidator(); + assertEquals(InputValidator.isValidItem("transportation_num_vehicles"), true); + assertEquals(InputValidator.isValidItem("test"), false); } @Test public void validItemValueTest() { - Assert.assertEquals(true, InputValidator + assertTrue(InputValidator .isValidItemValue("transportation_num_vehicles", "4")); - Assert.assertEquals(false, InputValidator + assertFalse(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")); + assertFalse(InputValidator.isValidItemValue("food_grains", "hello")); + assertTrue(InputValidator.isValidItemValue("food_grains", "5")); + assertTrue(InputValidator.isValidItemValue("food_grains", "3.5")); } @Test @@ -54,6 +57,6 @@ public class InputValidatorTest { put("shopping_services", "2413"); } }; - Assert.assertEquals(InputValidator.getDefaultValues(), map); + assertEquals(InputValidator.getDefaultValues(), map); } } diff --git a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java index d0784ba..b139f95 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -55,8 +55,8 @@ public class UserControllerTest { .andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}")); } - @Test - public void setInputTest() { - - } + //@Test + //public void setInputTest() { + // + //} } diff --git a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java index 9c9926f..47fd3fb 100644 --- a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java @@ -37,8 +37,7 @@ public class CalculatorServiceTest { @Bean public CalculatorService calculatorService() { - CalculatorService calculatorService = new CalculatorService(); - return calculatorService; + return new CalculatorService(); } @Bean