Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo db integration #13

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions back_end/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
// Add-on for MongoDB
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
testImplementation("org.springframework.boot:spring-boot-starter-test")
// JUnit Jupiter for testing
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
// Spring Boot test starters
testImplementation("org.springframework.boot:spring-boot-starter-test")
// Mocking framework
testImplementation("org.mockito:mockito-core:4.0.0")

// Lombok to reduce repetitive code such as getters and setters
compileOnly("org.projectlombok:lombok:1.18.32")
annotationProcessor("org.projectlombok:lombok:1.18.32")

testCompileOnly("org.projectlombok:lombok:1.18.32")
testAnnotationProcessor("org.projectlombok:lombok:1.18.32")
}

tasks.withType<Test> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
public class Cmpe202FinalApplication {

public static void main(String[] args) {
SpringApplication.run(Cmpe202FinalApplication.class, args);
}


@RestController
public static class AdminController {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.example.cmpe202_final.controller;

import lombok.AllArgsConstructor;
import org.example.cmpe202_final.model.Notification;
import org.example.cmpe202_final.service.NotificationService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/notifications")
@AllArgsConstructor
public class NotificationController {
private final NotificationService notificationService;

@GetMapping
public List<Notification> fetchAllNotifications() {
return notificationService.getAllNotifications();
}

@PostMapping
public void insertNewNotification(Notification notification) {
notificationService.insertNotification(notification);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.example.cmpe202_final.model;

import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Document
public class Notification {
@Id
private String id;
private String courseName;
private String eventTitle;
private String eventId;
private LocalDateTime date;
private String type;

public Notification(String courseName, String eventTitle, String eventId, LocalDateTime date, String type) {
this.courseName = courseName;
this.eventTitle = eventTitle;
this.eventId = eventId;
this.date = date;
this.type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.example.cmpe202_final.repository;

import org.example.cmpe202_final.model.Notification;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface NotificationRepository extends MongoRepository<Notification, String> {
List<Notification> findAllByOrderByDateDesc();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example.cmpe202_final.service;

import lombok.AllArgsConstructor;
import org.example.cmpe202_final.model.Notification;
import org.example.cmpe202_final.repository.NotificationRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class NotificationService {
private final NotificationRepository repository;
public List<Notification> getAllNotifications() {
return repository.findAllByOrderByDateDesc();
}

public void insertNotification(Notification notification) {
repository.save(notification);
}
}
4 changes: 4 additions & 0 deletions back_end/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
spring.main.banner-mode=off

spring.data.mongodb.uri=mongodb+srv://general-user:[email protected]/
spring.data.mongodb.database=student_portal
spring.data.mongodb.auto-index-creation=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.example.cmpe202_final.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.cmpe202_final.model.Notification;
import org.example.cmpe202_final.service.NotificationService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(NotificationController.class)
public class NotificationControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private NotificationService notificationService;

@Autowired
private ObjectMapper objectMapper;

@BeforeEach
void setUp() {
// Set up any necessary test data here
}

@Test
void testFetchAllNotifications() throws Exception {
// Given
Notification notification1 = new Notification(
"1",
"CMPE 202",
"Quiz 1",
"1",
LocalDateTime.now(),
"ASSIGNMENT_CREATED"
);
Notification notification2 = new Notification(
"2",
"CMPE 202",
"Quiz 2",
"2",
LocalDateTime.now(),
"ASSIGNMENT_GRADED"
);
List<Notification> notifications = Arrays.asList(notification1, notification2);
given(notificationService.getAllNotifications()).willReturn(notifications);

// When & Then
mockMvc.perform(get("/notifications")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(objectMapper.writeValueAsString(notifications)));
}

@Test
void testInsertNewNotification() throws Exception {
// Given
Notification notification = new Notification(
"1",
"CMPE 202",
"Quiz 1",
"1",
LocalDateTime.now(),
"ASSIGNMENT_CREATED"
);
String notificationJson = objectMapper.writeValueAsString(notification);

// When & Then
mockMvc.perform(post("/notifications")
.contentType(MediaType.APPLICATION_JSON)
.content(notificationJson))
.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.example.cmpe202_final.model;

import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.*;

class NotificationTest {

@Test
void testConstructorAndGetter() {
LocalDateTime now = LocalDateTime.now();
Notification notification = new Notification("Math101", "Exam", "E1", now, "INFO");

assertAll("constructor",
() -> assertEquals("Math101", notification.getCourseName()),
() -> assertEquals("Exam", notification.getEventTitle()),
() -> assertEquals("E1", notification.getEventId()),
() -> assertEquals(now, notification.getDate()),
() -> assertEquals("INFO", notification.getType())
);
}

@Test
void testSetterAndGetter() {
LocalDateTime now = LocalDateTime.now();
Notification notification = new Notification();
notification.setCourseName("Math101");
notification.setEventTitle("Exam");
notification.setEventId("E1");
notification.setDate(now);
notification.setType("INFO");

assertAll("setter and getter",
() -> assertEquals("Math101", notification.getCourseName()),
() -> assertEquals("Exam", notification.getEventTitle()),
() -> assertEquals("E1", notification.getEventId()),
() -> assertEquals(now, notification.getDate()),
() -> assertEquals("INFO", notification.getType())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.example.cmpe202_final.repository;

import org.example.cmpe202_final.model.Notification;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@DataMongoTest
public class NotificationRepositoryTest {

@Autowired
private NotificationRepository notificationRepository;

@BeforeEach
void setUp() {
// Ensure the repository is empty before each test
notificationRepository.deleteAll();

// Prepare test data
Notification notification1 = new Notification("1", "Course 1", "Event 1", "1", LocalDateTime.now().minusDays(1), "Type A");
Notification notification2 = new Notification("2", "Course 2", "Event 2", "2", LocalDateTime.now(), "Type B");
Notification notification3 = new Notification("3", "Course 3", "Event 3", "3", LocalDateTime.now().minusDays(2), "Type A");

// Save test notifications
notificationRepository.save(notification1);
notificationRepository.save(notification2);
notificationRepository.save(notification3);
}

@Test
void testFindAllByOrderByDate() {
List<Notification> notifications = notificationRepository.findAllByOrderByDateDesc();

assertThat(notifications).isNotNull();
assertThat(notifications.size()).isEqualTo(3);

// Verify the order is correct based on date
LocalDateTime previousDate = LocalDateTime.MAX;
for (Notification notification : notifications) {
assertThat(notification.getDate()).isBeforeOrEqualTo(previousDate);
previousDate = notification.getDate();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.example.cmpe202_final.service;

import org.example.cmpe202_final.model.Notification;
import org.example.cmpe202_final.repository.NotificationRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.assertThat;

public class NotificationServiceTest {

@Mock
private NotificationRepository notificationRepository;

@InjectMocks
private NotificationService notificationService;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
public void testGetAllNotifications() {
Notification notification1 = new Notification("Course 1", "Event 1", "1", LocalDateTime.now().minusDays(1), "Type A");
Notification notification2 = new Notification("Course 2", "Event 2", "2", LocalDateTime.now(), "Type B");
List<Notification> expectedNotifications = Arrays.asList(notification2, notification1);

when(notificationRepository.findAllByOrderByDateDesc()).thenReturn(expectedNotifications);

List<Notification> actualNotifications = notificationService.getAllNotifications();

assertThat(actualNotifications).isEqualTo(expectedNotifications);
verify(notificationRepository).findAllByOrderByDateDesc();
}

@Test
public void testInsertNotification() {
Notification newNotification = new Notification("Course 3", "Event 3", "3", LocalDateTime.now(), "Type C");

notificationService.insertNotification(newNotification);

verify(notificationRepository).save(newNotification);
}
}
Empty file.
Loading