BAEL 7436: Updated improvement (#15674)
This commit is contained in:
parent
9014d3007b
commit
7f1cb82f85
|
@ -32,4 +32,7 @@ public class Delivery {
|
||||||
public static Delivery freeDelivery() {
|
public static Delivery freeDelivery() {
|
||||||
return new Delivery("Free delivery");
|
return new Delivery("Free delivery");
|
||||||
}
|
}
|
||||||
|
public static Delivery defaultDelivery() {
|
||||||
|
return new Delivery("Default delivery");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
import com.baeldung.nullconversion.service.OnePersonExplicitDeliveryService;
|
import com.baeldung.nullconversion.service.OnePersonExplicitDeliveryService;
|
||||||
import com.baeldung.nullconversion.service.OnePersonGuavaOptionalDeliveryService;
|
import com.baeldung.nullconversion.service.OnePersonGuavaOptionalDeliveryService;
|
||||||
import com.baeldung.nullconversion.service.OnePersonOptionalDeliveryService;
|
import com.baeldung.nullconversion.service.OnePersonOptionalDeliveryService;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.ArgumentsSource;
|
import org.junit.jupiter.params.provider.ArgumentsSource;
|
||||||
|
|
||||||
|
@ -27,13 +28,4 @@ class OnePersonDeliveryServiceUnitTest {
|
||||||
Delivery actual = deliveryService.calculateDeliveryForPerson(1L);
|
Delivery actual = deliveryService.calculateDeliveryForPerson(1L);
|
||||||
assertThat(actual).isEqualTo(expected);
|
assertThat(actual).isEqualTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@ArgumentsSource(NullReturningPersonChainProvider.class)
|
|
||||||
void givenMockDeliverServiceWhenNullValuesThenGuavaOptionalServiceThrowsException(Person person) {
|
|
||||||
DeliveryService deliveryService = new OnePersonGuavaOptionalDeliveryService(person);
|
|
||||||
assertThatExceptionOfType(NullPointerException.class)
|
|
||||||
.isThrownBy(() -> deliveryService.calculateDeliveryForPerson(1L));
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -18,10 +18,10 @@ public class PersonProvider implements ArgumentsProvider {
|
||||||
|
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
Arguments.of(person, Delivery.freeDelivery()),
|
Arguments.of(person, Delivery.freeDelivery()),
|
||||||
Arguments.of(cloneAndMutate(person, p -> p.getAddress().getZipCode().setCode("")), null),
|
Arguments.of(cloneAndMutate(person, p -> p.getAddress().getZipCode().setCode("")), Delivery.defaultDelivery()),
|
||||||
Arguments.of(cloneAndMutate(person, p -> p.getAddress().setZipCode(null)), null),
|
Arguments.of(cloneAndMutate(person, p -> p.getAddress().setZipCode(null)), Delivery.defaultDelivery()),
|
||||||
Arguments.of(cloneAndMutate(person, p -> p.setAddress(null)), null),
|
Arguments.of(cloneAndMutate(person, p -> p.setAddress(null)), Delivery.defaultDelivery()),
|
||||||
Arguments.of(null, null)
|
Arguments.of(null, Delivery.defaultDelivery())
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic
|
||||||
String code = zipCode.getCode();
|
String code = zipCode.getCode();
|
||||||
return calculateDeliveryForZipCode(code);
|
return calculateDeliveryForZipCode(code);
|
||||||
}
|
}
|
||||||
return null;
|
return Delivery.defaultDelivery();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Delivery calculateDeliveryForPersonWithOptional(Long id) {
|
public Delivery calculateDeliveryForPersonWithOptional(Long id) {
|
||||||
|
@ -32,7 +32,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic
|
||||||
.map(Address::getZipCode)
|
.map(Address::getZipCode)
|
||||||
.map(ZipCode::getCode)
|
.map(ZipCode::getCode)
|
||||||
.map(this::calculateDeliveryForZipCode)
|
.map(this::calculateDeliveryForZipCode)
|
||||||
.orElse(null);
|
.orElse(Delivery.defaultDelivery());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Person getPersonById(Long id) {
|
protected Person getPersonById(Long id) {
|
||||||
|
@ -41,7 +41,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic
|
||||||
|
|
||||||
protected Delivery calculateDeliveryForZipCode(String zipCode) {
|
protected Delivery calculateDeliveryForZipCode(String zipCode) {
|
||||||
if (zipCode == null || zipCode.isEmpty()) {
|
if (zipCode == null || zipCode.isEmpty()) {
|
||||||
return null;
|
return Delivery.defaultDelivery();
|
||||||
} else {
|
} else {
|
||||||
return Delivery.freeDelivery();
|
return Delivery.freeDelivery();
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,6 @@ public class OnePersonExplicitDeliveryService extends MockOnePersonDeliveryServi
|
||||||
String code = zipCode.getCode();
|
String code = zipCode.getCode();
|
||||||
return calculateDeliveryForZipCode(code);
|
return calculateDeliveryForZipCode(code);
|
||||||
}
|
}
|
||||||
return null;
|
return Delivery.defaultDelivery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class OnePersonGuavaOptionalDeliveryService extends MockOnePersonDelivery
|
||||||
.transform(Address::getZipCode)
|
.transform(Address::getZipCode)
|
||||||
.transform(ZipCode::getCode)
|
.transform(ZipCode::getCode)
|
||||||
.transform(this::calculateDeliveryForZipCode)
|
.transform(this::calculateDeliveryForZipCode)
|
||||||
.orNull();
|
.or(Delivery.defaultDelivery());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class OnePersonOptionalDeliveryService extends MockOnePersonDeliveryServi
|
||||||
.map(Address::getZipCode)
|
.map(Address::getZipCode)
|
||||||
.map(ZipCode::getCode)
|
.map(ZipCode::getCode)
|
||||||
.map(this::calculateDeliveryForZipCode)
|
.map(this::calculateDeliveryForZipCode)
|
||||||
.orElse(null);
|
.orElse(Delivery.defaultDelivery());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue