Dev 19389 (#13745)
* Java-19389 : commits made for adding two new test cases for UUID use: * Java-19389 : commits made for adding two new test cases for UUID use: * JAVA-19389: Changes made for incorporating review comments
This commit is contained in:
parent
9e5fe2d7ac
commit
9f179d687e
|
@ -1,8 +1,11 @@
|
|||
package com.baeldung.entity;
|
||||
|
||||
import org.hibernate.annotations.Type;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
@ -13,11 +16,20 @@ public class Customer {
|
|||
private String name;
|
||||
private String email;
|
||||
|
||||
@Type(type = "org.hibernate.type.UUIDCharType")
|
||||
private UUID uuid;
|
||||
|
||||
public Customer(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Customer(String name, String email, UUID uuid) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -34,4 +46,7 @@ public class Customer {
|
|||
this.email = email;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return this.uuid;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.Query;
|
|||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
||||
|
||||
|
@ -16,4 +17,5 @@ public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
|||
@Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null or c.email = :email)")
|
||||
List<Customer> findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email);
|
||||
|
||||
List<Customer> findCustomerByUuid(@Param("uuid") UUID uuid);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -30,6 +31,7 @@ public class CustomerRepositoryIntegrationTest {
|
|||
entityManager.persist(new Customer("A", "A@example.com"));
|
||||
entityManager.persist(new Customer("D", null));
|
||||
entityManager.persist(new Customer("D", "D@example.com"));
|
||||
entityManager.persist(new Customer("C", null, UUID.fromString("c7c19ff4-8636-4b99-9591-c3327652f191")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -57,6 +59,20 @@ public class CustomerRepositoryIntegrationTest {
|
|||
assertEquals(2, customers.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUUIDIsPresent_whenQueryMethod_thenFetchedCorrectly() {
|
||||
List<Customer> customers = repository.findCustomerByUuid(UUID.fromString("c7c19ff4-8636-4b99-9591-c3327652f191"));
|
||||
|
||||
assertEquals(1, customers.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullUuid_whenQueryMethod_thenFetchedCorrectly() {
|
||||
List<Customer> customers = repository.findCustomerByUuid(null);
|
||||
|
||||
assertEquals(3, customers.size());
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
repository.deleteAll();
|
||||
|
|
Loading…
Reference in New Issue