BAEL-5922 User for Optional in Java (#13051)
* repositories. model class. initial tests * tests declarative api * removing from module java 11 * copy to optional module * fix assertEquals import
This commit is contained in:
parent
b82aabffc0
commit
790974461f
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.optionaluses;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
public User(String id, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.optionaluses;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UserRepositoryWithNull {
|
||||||
|
|
||||||
|
private final List<User> dbUsers = Arrays.asList(new User("1", "John"), new User("2", "Maria"), new User("3", "Daniel"));
|
||||||
|
|
||||||
|
public User findById(String id) {
|
||||||
|
|
||||||
|
for (User u : dbUsers) {
|
||||||
|
if (u.getId().equals(id)) {
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.optionaluses;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class UserRepositoryWithOptional {
|
||||||
|
|
||||||
|
private final List<User> dbUsers = Arrays.asList(new User("1", "John"), new User("2", "Maria"), new User("3", "Daniel"));
|
||||||
|
|
||||||
|
public Optional<User> findById(String id) {
|
||||||
|
|
||||||
|
for (User u : dbUsers) {
|
||||||
|
if (u.getId().equals(id)) {
|
||||||
|
return Optional.of(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.baeldung.optionaluses;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class UsesForOptionalUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonExistentUserId_whenSearchForUser_andNoNullCheck_thenThrowException() {
|
||||||
|
|
||||||
|
UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull();
|
||||||
|
String nonExistentUserId = "4";
|
||||||
|
|
||||||
|
assertThrows(NullPointerException.class, () -> {
|
||||||
|
System.out.println("User name: " + userRepositoryWithNull.findById(nonExistentUserId)
|
||||||
|
.getName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonExistentUserId_whenSearchForUser_thenOptionalShouldBeTreatedProperly() {
|
||||||
|
|
||||||
|
UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional();
|
||||||
|
String nonExistentUserId = "4";
|
||||||
|
|
||||||
|
String userName = userRepositoryWithOptional.findById(nonExistentUserId)
|
||||||
|
.orElse(new User("0", "admin"))
|
||||||
|
.getName();
|
||||||
|
|
||||||
|
assertEquals("admin", userName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingNull_thenNameShouldBeUpperCased() {
|
||||||
|
|
||||||
|
UserRepositoryWithNull userRepositoryWithNull = new UserRepositoryWithNull();
|
||||||
|
|
||||||
|
User user = userRepositoryWithNull.findById("2");
|
||||||
|
String upperCasedName = "";
|
||||||
|
|
||||||
|
if (user != null) {
|
||||||
|
if (user.getName().startsWith("M")) {
|
||||||
|
upperCasedName = user.getName().toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("MARIA", upperCasedName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExistentUserId_whenFoundUserWithNameStartingWithMInRepositoryUsingOptional_thenNameShouldBeUpperCased() {
|
||||||
|
|
||||||
|
UserRepositoryWithOptional userRepositoryWithOptional = new UserRepositoryWithOptional();
|
||||||
|
|
||||||
|
String upperCasedName = userRepositoryWithOptional.findById("2")
|
||||||
|
.filter(u -> u.getName().startsWith("M"))
|
||||||
|
.map(u -> u.getName().toUpperCase())
|
||||||
|
.orElse("");
|
||||||
|
|
||||||
|
assertEquals("MARIA", upperCasedName);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user