BAEL 7436: Updated improvement (#15674)

This commit is contained in:
Eugene Kovko 2024-01-19 01:43:24 +01:00 committed by GitHub
parent 9014d3007b
commit 7f1cb82f85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 14 additions and 19 deletions

View File

@ -32,4 +32,7 @@ public class Delivery {
public static Delivery freeDelivery() {
return new Delivery("Free delivery");
}
public static Delivery defaultDelivery() {
return new Delivery("Default delivery");
}
}

View File

@ -7,6 +7,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import com.baeldung.nullconversion.service.OnePersonExplicitDeliveryService;
import com.baeldung.nullconversion.service.OnePersonGuavaOptionalDeliveryService;
import com.baeldung.nullconversion.service.OnePersonOptionalDeliveryService;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
@ -27,13 +28,4 @@ class OnePersonDeliveryServiceUnitTest {
Delivery actual = deliveryService.calculateDeliveryForPerson(1L);
assertThat(actual).isEqualTo(expected);
}
@ParameterizedTest
@ArgumentsSource(NullReturningPersonChainProvider.class)
void givenMockDeliverServiceWhenNullValuesThenGuavaOptionalServiceThrowsException(Person person) {
DeliveryService deliveryService = new OnePersonGuavaOptionalDeliveryService(person);
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> deliveryService.calculateDeliveryForPerson(1L));
}
}

View File

@ -18,10 +18,10 @@ public class PersonProvider implements ArgumentsProvider {
return Stream.of(
Arguments.of(person, Delivery.freeDelivery()),
Arguments.of(cloneAndMutate(person, p -> p.getAddress().getZipCode().setCode("")), null),
Arguments.of(cloneAndMutate(person, p -> p.getAddress().setZipCode(null)), null),
Arguments.of(cloneAndMutate(person, p -> p.setAddress(null)), null),
Arguments.of(null, null)
Arguments.of(cloneAndMutate(person, p -> p.getAddress().getZipCode().setCode("")), Delivery.defaultDelivery()),
Arguments.of(cloneAndMutate(person, p -> p.getAddress().setZipCode(null)), Delivery.defaultDelivery()),
Arguments.of(cloneAndMutate(person, p -> p.setAddress(null)), Delivery.defaultDelivery()),
Arguments.of(null, Delivery.defaultDelivery())
);
}

View File

@ -23,7 +23,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic
String code = zipCode.getCode();
return calculateDeliveryForZipCode(code);
}
return null;
return Delivery.defaultDelivery();
}
public Delivery calculateDeliveryForPersonWithOptional(Long id) {
@ -32,7 +32,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic
.map(Address::getZipCode)
.map(ZipCode::getCode)
.map(this::calculateDeliveryForZipCode)
.orElse(null);
.orElse(Delivery.defaultDelivery());
}
protected Person getPersonById(Long id) {
@ -41,7 +41,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic
protected Delivery calculateDeliveryForZipCode(String zipCode) {
if (zipCode == null || zipCode.isEmpty()) {
return null;
return Delivery.defaultDelivery();
} else {
return Delivery.freeDelivery();
}

View File

@ -19,6 +19,6 @@ public class OnePersonExplicitDeliveryService extends MockOnePersonDeliveryServi
String code = zipCode.getCode();
return calculateDeliveryForZipCode(code);
}
return null;
return Delivery.defaultDelivery();
}
}

View File

@ -20,7 +20,7 @@ public class OnePersonGuavaOptionalDeliveryService extends MockOnePersonDelivery
.transform(Address::getZipCode)
.transform(ZipCode::getCode)
.transform(this::calculateDeliveryForZipCode)
.orNull();
.or(Delivery.defaultDelivery());
}
}

View File

@ -20,7 +20,7 @@ public class OnePersonOptionalDeliveryService extends MockOnePersonDeliveryServi
.map(Address::getZipCode)
.map(ZipCode::getCode)
.map(this::calculateDeliveryForZipCode)
.orElse(null);
.orElse(Delivery.defaultDelivery());
}
}