Skip to content

Commit c9770e7

Browse files
Identity Field Pattern
1 parent bc540f0 commit c9770e7

File tree

9 files changed

+122
-19
lines changed

9 files changed

+122
-19
lines changed

Identity_Field/pom.xml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@
2828
<artifactId>java-design-patterns</artifactId>
2929
<version>1.26.0-SNAPSHOT</version>
3030
</parent>
31-
3231
<artifactId>Identity_Field</artifactId>
3332

33+
<repositories>
34+
<repository>
35+
<id>jitpack.io</id>
36+
<url>https://jitpack.io</url>
37+
</repository>
38+
</repositories>
3439

3540
<properties>
3641
<maven.compiler.source>17</maven.compiler.source>
3742
<maven.compiler.target>17</maven.compiler.target>
3843
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
44+
<java.version>17</java.version>
3945
</properties>
4046
<dependencies>
4147
<dependency>
@@ -47,17 +53,34 @@
4753
<groupId>com.fasterxml.jackson.core</groupId>
4854
<artifactId>jackson-annotations</artifactId>
4955
</dependency>
50-
51-
5256
<dependency>
53-
<groupId>jakarta.persistence</groupId>
54-
<artifactId>jakarta.persistence-api</artifactId>
55-
<version>3.1.0</version>
57+
<groupId>com.h2database</groupId>
58+
<artifactId>h2</artifactId>
59+
<scope>runtime</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-web</artifactId>
5664
</dependency>
5765
<dependency>
5866
<groupId>javax.persistence</groupId>
5967
<artifactId>javax.persistence-api</artifactId>
6068
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.data</groupId>
71+
<artifactId>spring-data-jpa</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.springframework</groupId>
76+
<artifactId>spring-context</artifactId>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.springframework.data</groupId>
81+
<artifactId>spring-data-jpa</artifactId>
82+
<scope>test</scope>
83+
</dependency>
6184
</dependencies>
6285

6386
</project>

Identity_Field/src/main/java/com.iluwater.fieild/Book.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
package com.iluwater.fieild;
2727
import lombok.Getter;
2828
import lombok.Setter;
29+
import javax.persistence.Entity;
30+
2931
@Setter
3032
@Getter
33+
@Entity
3134
public class Book extends DomainObject {
3235
private String title;
3336
private String author;

Identity_Field/src/main/java/com.iluwater.fieild/Client.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import java.util.regex.Pattern;
2727
import lombok.Getter;
2828
import lombok.Setter;
29+
import javax.persistence.Entity;
2930

3031
@Setter
3132
@Getter
33+
@Entity
3234
public class Client extends DomainObject {
3335
private static final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
3436
private String name;

Identity_Field/src/main/java/com.iluwater.fieild/DomainObject.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,30 @@
2323
* THE SOFTWARE.
2424
*/
2525
package com.iluwater.fieild;
26+
/*
27+
* The DomainObject class provides a base for domain entities
28+
* that require unique identification within the application.
29+
*
30+
* <p>All child classes inherit the unique identifier field
31+
* and associated functionality.</p>
32+
*/
33+
import javax.persistence.Entity;
2634
import javax.persistence.GeneratedValue;
2735
import javax.persistence.GenerationType;
2836
import javax.persistence.Id;
2937
import javax.persistence.MappedSuperclass;
3038
import lombok.Getter;
3139
import lombok.Setter;
32-
/*
33-
* The DomainObject class provides a base for domain entities
34-
* that require unique identification within the application.
35-
*
36-
* <p>All child classes inherit the unique identifier field
37-
* and associated functionality.</p>
38-
*/
40+
3941
@Getter
4042
@Setter
4143
@MappedSuperclass
44+
@Entity
4245
public abstract class DomainObject {
4346
@Id
4447
@GeneratedValue(strategy = GenerationType.IDENTITY) // automatically generate unique values for primary key columns
48+
private Long id;
4549
//strategy = GenerationType.IDENTITY generate the primary key value by the database itself using the auto-increment column option
46-
private Integer id;
50+
4751

4852
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.iluwater.fieild;
2+
import org.springframework.data.jpa.repository.JpaRepository;
3+
import org.springframework.stereotype.Repository;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Objects;
7+
8+
@Repository
9+
public class BookRepository extends JpaRepository<Book, Long> {
10+
List<Book> BookList =new ArrayList<>();
11+
12+
public Book createBook(String title,String author) {
13+
Book book = new Book(title, author);
14+
BookList.add(book);
15+
return book;
16+
}
17+
public Book getBookById(Long id) {
18+
for (Book book: BookList )
19+
{
20+
if(Objects.equals(book.getId(), id))
21+
return book;
22+
}
23+
return null;
24+
}
25+
}

Identity_Field/src/test/java/com/iluwater/fieild/BookTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,37 @@
3030
import static org.junit.jupiter.api.Assertions.*;
3131

3232
class BookTest {
33+
BookRepository bookRepository;
3334
@Test
3435
void checkIdNotNull()
3536
{
36-
Book book= new Book("Design patterns","someone");
37+
Book book = bookRepository.createBook("Design patterns","someone");
3738
assertNotNull(book.getId());
3839
}
40+
@Test
3941
void checkTwoIdsNotEqual()
4042
{
41-
Book book= new Book("Design patterns","someone");
42-
Book book2= new Book("Head first","someone");
43+
Book book = bookRepository.createBook("Design patterns","someone");
44+
Book book2 = bookRepository.createBook("Head first","someone");
4345
assertNotEquals(book.getId(),book2.getId());
4446
}
47+
@Test
4548
void checkTitleNotNull()
4649
{
47-
Book book= new Book("Design patterns","someone");
50+
Book book = bookRepository.createBook("Design patterns","someone");
4851
assertNotNull(book.getTitle());
4952
}
53+
@Test
5054
void checkAuthorNotNull()
5155
{
52-
Book book= new Book("Design patterns","someone");
56+
Book book = bookRepository.createBook("Design patterns","someone");
5357
assertNotNull(book.getAuthor());
5458
}
59+
void checkSearch()
60+
{
61+
Book book = bookRepository.createBook("Design patterns","someone");
62+
assertNotNull(bookRepository.getBookById(book.getId()));
63+
64+
}
5565

5666
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwater.fieild;
2+
3+
import javax.persistence.Persistence;
4+
import javax.persistence.EntityManagerFactory;
5+
import javax.persistence.EntityManager;
6+
7+
public class BookTestApp {
8+
private BookRepository bookRepository;
9+
public static void main(String... args) {
10+
11+
BookTest bt=new BookTest();
12+
bt.checkAuthorNotNull();
13+
bt.checkIdNotNull();
14+
bt.checkTitleNotNull();
15+
bt.checkTwoIdsNotEqual();
16+
17+
}
18+
}

Identity_Field/src/test/java/com/iluwater/fieild/ClientTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,32 @@
2424
*/
2525
package com.iluwater.fieild;
2626

27+
import org.junit.jupiter.api.Test;
28+
2729
import static org.junit.jupiter.api.Assertions.*;
2830

2931
class ClientTest {
32+
@Test
3033
void checkIdNotNull()
3134
{
3235
Client client= new Client("John","[email protected]");
3336
assertNotNull(client.getId());
3437
}
38+
@Test
3539
void checkTwoIdsNotEqual()
3640
{
3741
Client client= new Client("John","[email protected]");
3842
Client client2= new Client("Sara","[email protected]");
3943
assertNotEquals(client.getId(),client2.getId());
4044
}
45+
@Test
4146
void checkEmails()
4247
{
4348
Client client= new Client("John","[email protected]");
4449
Client client2= new Client("Sara","[email protected]");
4550
assertNotEquals(client.getEmail(),client2.getEmail());
4651
}
52+
@Test
4753
void checkEmailNotNull()
4854
{
4955
Client client= new Client("John","[email protected]");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwater.fieild;
2+
3+
public class ClientTestApp {
4+
public static void main(String... args) {
5+
ClientTest c=new ClientTest();
6+
c.checkEmailNotNull();
7+
c.checkEmails();
8+
c.checkIdNotNull();
9+
c.checkTwoIdsNotEqual();
10+
System.out.println("End of testing");
11+
}
12+
}

0 commit comments

Comments
 (0)