mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-03-04 21:46:43 +03:00
fix humidity to handle unexpected data (#62)
Co-authored-by: «pmartinot» <«pmartinot@provatis.com»>
This commit is contained in:
parent
a47386fd8f
commit
ec4f6af6b6
@ -44,15 +44,13 @@ public class Humidity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates {@link Humidity} object with correctness check.
|
* Creates {@link Humidity} object with safeguard to have min 0 and max 100 as value
|
||||||
* @param value humidity
|
* @param value humidity
|
||||||
* @return created {@link Humidity} object
|
* @return created {@link Humidity} object
|
||||||
*/
|
*/
|
||||||
public static Humidity withValue(byte value) {
|
public static Humidity withValue(byte value) {
|
||||||
if (value < 0 || value > 100) {
|
int v = Math.max(0, Math.min(100, value));
|
||||||
throw new IllegalArgumentException("Humidity value must be in [0, 100] range.");
|
return new Humidity((byte) v);
|
||||||
}
|
|
||||||
return new Humidity(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,13 +66,10 @@ public class Humidity {
|
|||||||
* Sets humidity percentage value.
|
* Sets humidity percentage value.
|
||||||
*
|
*
|
||||||
* @param value new humidity value.
|
* @param value new humidity value.
|
||||||
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
|
* Rounds to 0 or 100 if provided value isn't in allowed range.
|
||||||
*/
|
*/
|
||||||
public void setValue(int value) {
|
public void setValue(int value) {
|
||||||
if (value < 0 || value > 100) {
|
this.value = Math.max(0, Math.min(100, value));
|
||||||
throw new IllegalArgumentException("Humidity value must be in [0, 100] range.");
|
|
||||||
}
|
|
||||||
this.value = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -37,13 +37,15 @@ public class HumidityUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreateHumidityByConstructorWithInvalidDataAboveHundred_thenThrowAnException() {
|
public void whenCreateHumidityByConstructorWithInvalidDataAboveHundred_thenValueIsMaxHundred() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> Humidity.withValue((byte) 112));
|
Humidity h = Humidity.withValue((byte) 112);
|
||||||
|
assertEquals(100, h.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreateHumidityByConstructorWithInvalidDataNegative_thenThrowAnException() {
|
public void whenCreateHumidityByConstructorWithInvalidDataNegative_thenValueIsMinZero() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> Humidity.withValue((byte) -33));
|
Humidity h = Humidity.withValue((byte) -33);
|
||||||
|
assertEquals(0, h.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -59,15 +61,17 @@ public class HumidityUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreateHumidityAndSetInvalidDataAboveHundred_thenThrowAnException() {
|
public void whenCreateHumidityAndSetInvalidDataAboveHundred_thenValueIsMaxHundred() {
|
||||||
Humidity humidity = Humidity.withValue((byte) 12);
|
Humidity humidity = Humidity.withValue((byte) 12);
|
||||||
assertThrows(IllegalArgumentException.class, () -> humidity.setValue((byte) 112));
|
humidity.setValue(112);
|
||||||
|
assertEquals(100, humidity.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreateHumidityAndSetInvalidDataNegative_thenThrowAnException() {
|
public void whenCreateHumidityAndSetInvalidDataNegative_thenValueIsMinZero() {
|
||||||
Humidity humidity = Humidity.withValue((byte) 88);
|
Humidity humidity = Humidity.withValue((byte) 88);
|
||||||
assertThrows(IllegalArgumentException.class, () -> humidity.setValue((byte) -89));
|
humidity.setValue(-89);
|
||||||
|
assertEquals(0, humidity.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user