fix(backend): add @Builder.Default to entity fields and add unit test suite
This commit is contained in:
@@ -30,11 +30,14 @@ public class CommunityAnswer {
|
|||||||
private String author;
|
private String author;
|
||||||
|
|
||||||
@Column(name = "upvotes")
|
@Column(name = "upvotes")
|
||||||
|
@Builder.Default
|
||||||
private Integer upvotes = 0;
|
private Integer upvotes = 0;
|
||||||
|
|
||||||
@Column(name = "is_accepted")
|
@Column(name = "is_accepted")
|
||||||
|
@Builder.Default
|
||||||
private Boolean isAccepted = false;
|
private Boolean isAccepted = false;
|
||||||
|
|
||||||
@Column(name = "created_at")
|
@Column(name = "created_at")
|
||||||
|
@Builder.Default
|
||||||
private LocalDateTime createdAt = LocalDateTime.now();
|
private LocalDateTime createdAt = LocalDateTime.now();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,15 +29,19 @@ public class CommunityQuestion {
|
|||||||
private String author;
|
private String author;
|
||||||
|
|
||||||
@Column(name = "upvotes")
|
@Column(name = "upvotes")
|
||||||
|
@Builder.Default
|
||||||
private Integer upvotes = 0;
|
private Integer upvotes = 0;
|
||||||
|
|
||||||
@Column(name = "tags", columnDefinition = "text[]")
|
@Column(name = "tags", columnDefinition = "text[]")
|
||||||
|
@Builder.Default
|
||||||
private List<String> tags = new ArrayList<>();
|
private List<String> tags = new ArrayList<>();
|
||||||
|
|
||||||
@Column(name = "is_answered")
|
@Column(name = "is_answered")
|
||||||
|
@Builder.Default
|
||||||
private Boolean isAnswered = false;
|
private Boolean isAnswered = false;
|
||||||
|
|
||||||
@Column(name = "created_at")
|
@Column(name = "created_at")
|
||||||
|
@Builder.Default
|
||||||
private LocalDateTime createdAt = LocalDateTime.now();
|
private LocalDateTime createdAt = LocalDateTime.now();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
|
|||||||
@@ -39,8 +39,10 @@ public class NotePyq {
|
|||||||
private String fileSize;
|
private String fileSize;
|
||||||
|
|
||||||
@Column(name = "downloads_count")
|
@Column(name = "downloads_count")
|
||||||
|
@Builder.Default
|
||||||
private Integer downloadsCount = 0;
|
private Integer downloadsCount = 0;
|
||||||
|
|
||||||
@Column(name = "uploaded_at")
|
@Column(name = "uploaded_at")
|
||||||
|
@Builder.Default
|
||||||
private LocalDateTime uploadedAt = LocalDateTime.now();
|
private LocalDateTime uploadedAt = LocalDateTime.now();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<BusLocation> active = busLocationService.getActiveLocations();
|
||||||
|
assertEquals(2, active.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
60
backend/src/test/java/com/rit/portal/EntityMappingTest.java
Normal file
60
backend/src/test/java/com/rit/portal/EntityMappingTest.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user