From 128e033a9aa354f522e56e9b77fe848e7fd0bdde Mon Sep 17 00:00:00 2001 From: Shanmuga Krishnan S M Date: Thu, 20 Aug 2026 10:14:29 +0530 Subject: [PATCH] fix(backend): add @Builder.Default to entity fields and add unit test suite --- .../rit/portal/entity/CommunityAnswer.java | 3 + .../rit/portal/entity/CommunityQuestion.java | 4 ++ .../java/com/rit/portal/entity/NotePyq.java | 2 + .../rit/portal/BusLocationServiceTest.java | 58 ++++++++++++++++++ .../com/rit/portal/EntityMappingTest.java | 60 +++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 backend/src/test/java/com/rit/portal/BusLocationServiceTest.java create mode 100644 backend/src/test/java/com/rit/portal/EntityMappingTest.java diff --git a/backend/src/main/java/com/rit/portal/entity/CommunityAnswer.java b/backend/src/main/java/com/rit/portal/entity/CommunityAnswer.java index 2c49cad..f82f11f 100644 --- a/backend/src/main/java/com/rit/portal/entity/CommunityAnswer.java +++ b/backend/src/main/java/com/rit/portal/entity/CommunityAnswer.java @@ -30,11 +30,14 @@ public class CommunityAnswer { private String author; @Column(name = "upvotes") + @Builder.Default private Integer upvotes = 0; @Column(name = "is_accepted") + @Builder.Default private Boolean isAccepted = false; @Column(name = "created_at") + @Builder.Default private LocalDateTime createdAt = LocalDateTime.now(); } diff --git a/backend/src/main/java/com/rit/portal/entity/CommunityQuestion.java b/backend/src/main/java/com/rit/portal/entity/CommunityQuestion.java index 0bf8012..f8d03fe 100644 --- a/backend/src/main/java/com/rit/portal/entity/CommunityQuestion.java +++ b/backend/src/main/java/com/rit/portal/entity/CommunityQuestion.java @@ -29,15 +29,19 @@ public class CommunityQuestion { private String author; @Column(name = "upvotes") + @Builder.Default private Integer upvotes = 0; @Column(name = "tags", columnDefinition = "text[]") + @Builder.Default private List tags = new ArrayList<>(); @Column(name = "is_answered") + @Builder.Default private Boolean isAnswered = false; @Column(name = "created_at") + @Builder.Default private LocalDateTime createdAt = LocalDateTime.now(); @OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.EAGER) diff --git a/backend/src/main/java/com/rit/portal/entity/NotePyq.java b/backend/src/main/java/com/rit/portal/entity/NotePyq.java index 9fb586a..94718a5 100644 --- a/backend/src/main/java/com/rit/portal/entity/NotePyq.java +++ b/backend/src/main/java/com/rit/portal/entity/NotePyq.java @@ -39,8 +39,10 @@ public class NotePyq { private String fileSize; @Column(name = "downloads_count") + @Builder.Default private Integer downloadsCount = 0; @Column(name = "uploaded_at") + @Builder.Default private LocalDateTime uploadedAt = LocalDateTime.now(); } diff --git a/backend/src/test/java/com/rit/portal/BusLocationServiceTest.java b/backend/src/test/java/com/rit/portal/BusLocationServiceTest.java new file mode 100644 index 0000000..e1bea2b --- /dev/null +++ b/backend/src/test/java/com/rit/portal/BusLocationServiceTest.java @@ -0,0 +1,58 @@ +package com.rit.portal; + +import com.rit.portal.model.BusLocation; +import com.rit.portal.service.BusLocationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class BusLocationServiceTest { + + private BusLocationService busLocationService; + + @BeforeEach + void setUp() { + busLocationService = new BusLocationService(); + } + + @Test + void testUpdateAndGetLocation() { + busLocationService.updateLocation("24A", 13.0827, 80.2707); + + BusLocation loc = busLocationService.getLocation("24A"); + assertNotNull(loc); + assertEquals("24A", loc.getRouteNumber()); + assertEquals(13.0827, loc.getLatitude()); + assertEquals(80.2707, loc.getLongitude()); + assertFalse(loc.isStopped()); + assertNotNull(loc.getLastUpdated()); + } + + @Test + void testStopLocation() { + busLocationService.updateLocation("15B", 12.9716, 77.5946); + busLocationService.stopLocation("15B"); + + BusLocation loc = busLocationService.getLocation("15B"); + assertNotNull(loc); + assertTrue(loc.isStopped()); + } + + @Test + void testGetNonExistentLocationReturnsNull() { + BusLocation loc = busLocationService.getLocation("NON_EXISTENT"); + assertNull(loc); + } + + @Test + void testGetActiveLocations() { + busLocationService.updateLocation("R1", 13.0, 80.0); + busLocationService.updateLocation("R2", 13.1, 80.1); + + List active = busLocationService.getActiveLocations(); + assertEquals(2, active.size()); + } +} diff --git a/backend/src/test/java/com/rit/portal/EntityMappingTest.java b/backend/src/test/java/com/rit/portal/EntityMappingTest.java new file mode 100644 index 0000000..49e3d16 --- /dev/null +++ b/backend/src/test/java/com/rit/portal/EntityMappingTest.java @@ -0,0 +1,60 @@ +package com.rit.portal; + +import com.rit.portal.entity.CommunityAnswer; +import com.rit.portal.entity.CommunityQuestion; +import com.rit.portal.entity.NotePyq; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.*; + +public class EntityMappingTest { + + @Test + void testNotePyqBuilderDefaults() { + NotePyq note = NotePyq.builder() + .title("Data Structures Notes") + .subject("Data Structures") + .department("CSE") + .semester(3) + .fileType("notes") + .downloadUrl("https://example.com/ds.pdf") + .fileSize("2.4 MB") + .build(); + + assertNotNull(note); + assertEquals(0, note.getDownloadsCount()); + assertNotNull(note.getUploadedAt()); + assertTrue(note.getUploadedAt().isBefore(LocalDateTime.now().plusSeconds(1))); + } + + @Test + void testCommunityQuestionBuilderDefaults() { + CommunityQuestion question = CommunityQuestion.builder() + .title("How to prepare for CAT-1?") + .body("Looking for reference materials and past questions.") + .author("Student123") + .build(); + + assertNotNull(question); + assertEquals(0, question.getUpvotes()); + assertFalse(question.getIsAnswered()); + assertNotNull(question.getTags()); + assertNotNull(question.getAnswers()); + assertNotNull(question.getCreatedAt()); + } + + @Test + void testCommunityAnswerBuilderDefaults() { + CommunityAnswer answer = CommunityAnswer.builder() + .body("Focus on Unit 1 and Unit 2 standard questions.") + .author("Senior456") + .build(); + + assertNotNull(answer); + assertEquals(0, answer.getUpvotes()); + assertFalse(answer.getIsAccepted()); + assertNotNull(answer.getCreatedAt()); + } +}