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:
pedrolopes 2022-11-27 09:08:38 -03:00 committed by GitHub
parent b82aabffc0
commit 790974461f
4 changed files with 126 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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