commit
dd370f90bb
|
@ -31,10 +31,10 @@ public class UserServerUnitTest extends JUnitRouteTest {
|
|||
.assertStatusCode(404);
|
||||
|
||||
appRoute.run(HttpRequest.DELETE("/users/1"))
|
||||
.assertStatusCode(200);
|
||||
.assertStatusCode(405);
|
||||
|
||||
appRoute.run(HttpRequest.DELETE("/users/42"))
|
||||
.assertStatusCode(200);
|
||||
.assertStatusCode(405);
|
||||
|
||||
appRoute.run(HttpRequest.POST("/users")
|
||||
.withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, zaphod())))
|
||||
|
|
|
@ -24,9 +24,14 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.5.1</version>
|
||||
<version>${jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
|
|
|
@ -17,7 +17,6 @@ import org.springframework.mock.jndi.SimpleNamingContextBuilder;
|
|||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class JndiExceptionsUnitTest {
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
@Order(1)
|
||||
void givenNoContext_whenLookupObject_thenThrowNoInitialContext() {
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
<module>jackson-conversions-2</module>
|
||||
<module>jackson-custom-conversions</module>
|
||||
<module>jackson-exceptions</module>
|
||||
<module>jackson-simple</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -16,4 +16,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
|
||||
### NOTE:
|
||||
|
||||
Since this is a module tied to an e-book, it should **not** be used to store the code for any further article.
|
||||
Since this is a module tied to an e-book, it should **not** be moved or used to store the code for any further article.
|
|
@ -8,11 +8,18 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jackson-modules</artifactId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!--jackson for xml -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.lifecycleevents;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootLifecycleEventApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootLifecycleEventApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.lifecycleevents.model;
|
||||
|
||||
import javax.persistence.PostLoad;
|
||||
import javax.persistence.PostPersist;
|
||||
import javax.persistence.PostRemove;
|
||||
import javax.persistence.PostUpdate;
|
||||
import javax.persistence.PrePersist;
|
||||
import javax.persistence.PreRemove;
|
||||
import javax.persistence.PreUpdate;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class AuditTrailListener {
|
||||
private static Log log = LogFactory.getLog(AuditTrailListener.class);
|
||||
|
||||
@PrePersist
|
||||
@PreUpdate
|
||||
@PreRemove
|
||||
private void beforeAnyUpdate(User user) {
|
||||
if (user.getId() == 0) {
|
||||
log.info("[USER AUDIT] About to add a user");
|
||||
} else {
|
||||
log.info("[USER AUDIT] About to update/delete user: " + user.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@PostPersist
|
||||
@PostUpdate
|
||||
@PostRemove
|
||||
private void afterAnyUpdate(User user) {
|
||||
log.info("[USER AUDIT] add/update/delete complete for user: " + user.getId());
|
||||
}
|
||||
|
||||
@PostLoad
|
||||
private void afterLoad(User user) {
|
||||
log.info("[USER AUDIT] user loaded from database: " + user.getId());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package com.baeldung.lifecycleevents.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EntityListeners;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.PostLoad;
|
||||
import javax.persistence.PostPersist;
|
||||
import javax.persistence.PostRemove;
|
||||
import javax.persistence.PostUpdate;
|
||||
import javax.persistence.PrePersist;
|
||||
import javax.persistence.PreRemove;
|
||||
import javax.persistence.PreUpdate;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@Entity
|
||||
@EntityListeners(AuditTrailListener.class)
|
||||
public class User {
|
||||
private static Log log = LogFactory.getLog(User.class);
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
private String userName;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
@Transient
|
||||
private String fullName;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
@PrePersist
|
||||
public void logNewUserAttempt() {
|
||||
log.info("Attempting to add new user with username: " + userName);
|
||||
}
|
||||
|
||||
@PostPersist
|
||||
public void logNewUserAdded() {
|
||||
log.info("Added user '" + userName + "' with ID: " + id);
|
||||
}
|
||||
|
||||
@PreRemove
|
||||
public void logUserRemovalAttempt() {
|
||||
log.info("Attempting to delete user: " + userName);
|
||||
}
|
||||
|
||||
@PostRemove
|
||||
public void logUserRemoval() {
|
||||
log.info("Deleted user: " + userName);
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
public void logUserUpdateAttempt() {
|
||||
log.info("Attempting to update user: " + userName);
|
||||
}
|
||||
|
||||
@PostUpdate
|
||||
public void logUserUpdate() {
|
||||
log.info("Updated user: " + userName);
|
||||
}
|
||||
|
||||
@PostLoad
|
||||
public void logUserLoad() {
|
||||
fullName = firstName + " " + lastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.lifecycleevents.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.lifecycleevents.model.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
public User findByUserName(String userName);
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package lifecycleevents;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.lifecycleevents.SpringBootLifecycleEventApplication;
|
||||
import com.baeldung.lifecycleevents.model.User;
|
||||
import com.baeldung.lifecycleevents.repository.UserRepository;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringBootLifecycleEventApplication.class)
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
User user = new User();
|
||||
user.setFirstName("Jane");
|
||||
user.setLastName("Smith");
|
||||
user.setUserName("jsmith123");
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNewUserProvided_userIsAdded() {
|
||||
User user = new User();
|
||||
user.setFirstName("John");
|
||||
user.setLastName("Doe");
|
||||
user.setUserName("jdoe123");
|
||||
user = userRepository.save(user);
|
||||
assertTrue(user.getId() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUserNameProvided_userIsLoaded() {
|
||||
User user = userRepository.findByUserName("jsmith123");
|
||||
assertNotNull(user);
|
||||
assertEquals("jsmith123", user.getUserName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExistingUserProvided_userIsUpdated() {
|
||||
User user = userRepository.findByUserName("jsmith123");
|
||||
user.setFirstName("Joe");
|
||||
user = userRepository.save(user);
|
||||
assertEquals("Joe", user.getFirstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExistingUserDeleted_userIsDeleted() {
|
||||
User user = userRepository.findByUserName("jsmith123");
|
||||
userRepository.delete(user);
|
||||
user = userRepository.findByUserName("jsmith123");
|
||||
assertNull(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExistingUserLoaded_fullNameIsAvailable() {
|
||||
String expectedFullName = "Jane Smith";
|
||||
User user = userRepository.findByUserName("jsmith123");
|
||||
assertEquals(expectedFullName, user.getFullName());
|
||||
}
|
||||
}
|
6
pom.xml
6
pom.xml
|
@ -448,6 +448,7 @@
|
|||
<module>immutables</module>
|
||||
|
||||
<module>jackson-modules</module>
|
||||
<module>jackson-simple</module>
|
||||
<module>java-blockchain</module>
|
||||
|
||||
<module>java-collections-conversions</module>
|
||||
|
@ -663,7 +664,6 @@
|
|||
<module>spring-boot-libraries</module>
|
||||
<module>spring-boot-logging-log4j2</module>
|
||||
<module>spring-boot-mvc-2</module>
|
||||
<module>spring-boot-mvc-birt</module>
|
||||
<module>spring-boot-nashorn</module>
|
||||
<module>spring-boot-parent</module>
|
||||
<module>spring-boot-performance</module>
|
||||
|
@ -678,7 +678,6 @@
|
|||
<module>spring-boot-security</module>
|
||||
<module>spring-boot-springdoc</module>
|
||||
<module>spring-boot-testing</module>
|
||||
<module>spring-boot-vue</module>
|
||||
|
||||
<module>spring-caching</module>
|
||||
|
||||
|
@ -996,6 +995,7 @@
|
|||
<module>immutables</module>
|
||||
|
||||
<module>jackson-modules</module>
|
||||
<module>jackson-simple</module>
|
||||
<module>java-blockchain</module>
|
||||
|
||||
<module>java-collections-conversions</module>
|
||||
|
@ -1203,7 +1203,6 @@
|
|||
<module>spring-boot-logging-log4j2</module>
|
||||
<module>spring-boot-mvc</module>
|
||||
<module>spring-boot-mvc-2</module>
|
||||
<module>spring-boot-mvc-birt</module>
|
||||
<module>spring-boot-nashorn</module>
|
||||
<module>spring-boot-parent</module>
|
||||
<module>spring-boot-performance</module>
|
||||
|
@ -1218,7 +1217,6 @@
|
|||
<module>spring-boot-security</module>
|
||||
<module>spring-boot-springdoc</module>
|
||||
<module>spring-boot-testing</module>
|
||||
<module>spring-boot-vue</module>
|
||||
|
||||
<module>spring-caching</module>
|
||||
|
||||
|
|
|
@ -13,4 +13,9 @@
|
|||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>spring-boot-mvc-birt</module>
|
||||
<module>spring-boot-vue</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
|
@ -12,7 +12,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue