|
| 1 | +/* |
| 2 | + * Copyright 2008-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.domain.support; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.data.jpa.domain.sample.AuditableEmbeddable; |
| 28 | +import org.springframework.data.jpa.domain.sample.AuditableEntity; |
| 29 | +import org.springframework.data.jpa.repository.sample.AuditableEntityRepository; |
| 30 | +import org.springframework.test.context.ContextConfiguration; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 32 | + |
| 33 | +/** |
| 34 | + * Integration test for {@link AuditingEntityListener}. |
| 35 | + * |
| 36 | + * @author Greg Turnquist |
| 37 | + */ |
| 38 | +@ExtendWith(SpringExtension.class) |
| 39 | +@ContextConfiguration("classpath:auditing/auditing-entity-with-embeddable-listener.xml") |
| 40 | +public class AuditingEntityWithEmbeddableListenerTests { |
| 41 | + |
| 42 | + @Autowired AuditableEntityRepository repository; |
| 43 | + |
| 44 | + private AuditableEntity entity; |
| 45 | + private AuditableEmbeddable auditDetails; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + |
| 50 | + entity = new AuditableEntity(); |
| 51 | + entity.setId(1L); |
| 52 | + entity.setData("original value"); |
| 53 | + |
| 54 | + auditDetails = new AuditableEmbeddable(); |
| 55 | + entity.setAuditDetails(auditDetails); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void auditsEmbeddedCorrectly() { |
| 60 | + |
| 61 | + // when |
| 62 | + repository.saveAndFlush(entity); |
| 63 | + Optional<AuditableEntity> optionalEntity = repository.findById(1L); |
| 64 | + |
| 65 | + // then |
| 66 | + assertThat(optionalEntity).isNotEmpty(); |
| 67 | + |
| 68 | + AuditableEntity auditableEntity = optionalEntity.get(); |
| 69 | + assertThat(auditableEntity.getData()).isEqualTo("original value"); |
| 70 | + |
| 71 | + assertThat(auditableEntity.getAuditDetails().getDateCreated()).isNotNull(); |
| 72 | + assertThat(auditableEntity.getAuditDetails().getDateUpdated()).isNotNull(); |
| 73 | + |
| 74 | + Instant originalCreationDate = auditableEntity.getAuditDetails().getDateCreated(); |
| 75 | + Instant originalDateUpdated = auditableEntity.getAuditDetails().getDateUpdated(); |
| 76 | + |
| 77 | + auditableEntity.setData("updated value"); |
| 78 | + |
| 79 | + repository.saveAndFlush(auditableEntity); |
| 80 | + |
| 81 | + Optional<AuditableEntity> optionalRevisedEntity = repository.findById(1L); |
| 82 | + |
| 83 | + assertThat(optionalRevisedEntity).isNotEmpty(); |
| 84 | + |
| 85 | + AuditableEntity revisedEntity = optionalRevisedEntity.get(); |
| 86 | + assertThat(revisedEntity.getData()).isEqualTo("updated value"); |
| 87 | + |
| 88 | + assertThat(revisedEntity.getAuditDetails().getDateCreated()).isEqualTo(originalCreationDate); |
| 89 | + assertThat(revisedEntity.getAuditDetails().getDateUpdated()).isAfter(originalDateUpdated); |
| 90 | + } |
| 91 | +} |
0 commit comments