FIX:: checkstyle fixes

This commit is contained in:
mlwauben
2019-03-26 10:55:12 +01:00
parent d8ced45a67
commit 9e345ee353
15 changed files with 96 additions and 77 deletions

View File

@@ -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) {

View File

@@ -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() {

View File

@@ -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 {

View File

@@ -1,4 +1,5 @@
import static org.junit.Assert.assertEquals;
import greenify.common.ErrorResponse;
import org.junit.Test;

View File

@@ -1,4 +1,5 @@
import static org.junit.Assert.assertEquals;
import greenify.common.UserDto;
import org.junit.Test;

View File

@@ -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<InputItem> 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<String, String> getDefaultValues() {
Map<String,String> map = new HashMap<String, String>(){};
for (int i = 0; i < inputItems.size(); i++) {
map.put(inputItems.get(i).getName(), inputItems.get(i).getDefaultValue());
Map<String, String> map = new HashMap<String, String>() { };
for (InputItem inputItem : inputItems) {
map.put(inputItem.getName(), inputItem.getDefaultValue());
}
return map;
}

View File

@@ -52,7 +52,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() {
@@ -68,7 +68,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() {
@@ -84,7 +84,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() {
@@ -100,7 +100,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() {
@@ -116,7 +116,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<String, String> getFootPrintInputs() {
@@ -132,7 +132,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
@@ -157,7 +157,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

View File

@@ -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());
}
}

View File

@@ -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.

View File

@@ -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<String, String> 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<String, String> 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("<result_grand_total>")
+ 20, response.getBody().indexOf("</result_grand_total>"));
// 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());
}
}

View File

@@ -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.
@@ -81,7 +81,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
@@ -96,7 +96,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
*/
@@ -106,7 +106,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")

View File

@@ -8,6 +8,6 @@ import org.springframework.test.context.junit4.SpringRunner;
public class ApplicationTest {
@Test
public void contextLoads() throws Exception{ }
public void contextLoads() { }
}

View File

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

View File

@@ -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() {
//
//}
}

View File

@@ -37,8 +37,7 @@ public class CalculatorServiceTest {
@Bean
public CalculatorService calculatorService() {
CalculatorService calculatorService = new CalculatorService();
return calculatorService;
return new CalculatorService();
}
@Bean