Skip to content

Commit

Permalink
Merge pull request #114 from DTS-STN/GjB/fix-uuidgenerator
Browse files Browse the repository at this point in the history
Fix warning in UuidGenerator; improve test
  • Loading branch information
gregory-j-baker authored Oct 18, 2023
2 parents 964feb4 + c226ec6 commit ba368df
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @author Greg Baker (gregory.j.baker@hrsdc-rhdcc.gc.ca)
*/
@SuppressWarnings({ "serial" })
public class UuidGenerator implements IdentifierGenerator {

private final transient ValueGenerator valueGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.util.UUID;

import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.EventType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

Expand All @@ -22,30 +22,27 @@
@ExtendWith({ MockitoExtension.class })
class UuidGeneratorTests {

@Mock UuidGenerator.ValueGenerator valueGenerator;

UuidGenerator uuidGenerator;

final UUID id = UUID.randomUUID();

@BeforeEach
void setUp() {
this.uuidGenerator = new UuidGenerator(valueGenerator);
this.uuidGenerator = new UuidGenerator(() -> id);
}

@Test
void testGenerate_withId() {
final SharedSessionContractImplementor sharedSessionContractImplementor = mock(SharedSessionContractImplementor.class, Mockito.RETURNS_DEEP_STUBS);
when(sharedSessionContractImplementor.getEntityPersister(any(), any()).getIdentifier(any(), any(SharedSessionContractImplementor.class))).thenReturn(id.toString());
assertThat(uuidGenerator.generate(sharedSessionContractImplementor, new Object())).asString().isEqualTo(id.toString());
assertThat(uuidGenerator.generate(sharedSessionContractImplementor, null, new Object(), EventType.INSERT)).asString().isEqualTo(id.toString());
}

@Test
void testGenerate_withNullId() {
final SharedSessionContractImplementor sharedSessionContractImplementor = mock(SharedSessionContractImplementor.class, Mockito.RETURNS_DEEP_STUBS);
when(sharedSessionContractImplementor.getEntityPersister(any(), any()).getIdentifier(any(), any(SharedSessionContractImplementor.class))).thenReturn(null);
when(valueGenerator.generateUuid()).thenReturn(id);
assertThat(uuidGenerator.generate(sharedSessionContractImplementor, new Object())).asString().isEqualTo(id.toString());
assertThat(uuidGenerator.generate(sharedSessionContractImplementor, null, new Object(), EventType.INSERT)).asString().isEqualTo(id.toString());
}

}

0 comments on commit ba368df

Please sign in to comment.