-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for notes 88% Coverage. Finishing up some notes stuff Daily Notes
- Loading branch information
Showing
3 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
backend/omniplanner/src/test/java/com/main/omniplanner/NotesTests/NotesControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.main.omniplanner.NotesTests; | ||
|
||
import com.main.omniplanner.meals.MealEvents; | ||
import com.main.omniplanner.meals.MealEventsController; | ||
import com.main.omniplanner.meals.MealEventsService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.sql.Date; | ||
import java.sql.Time; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
class MealEventsControllerTest { | ||
|
||
@Mock | ||
private MealEventsService mealEventsService; | ||
|
||
private MealEventsController mealEventsController; | ||
MealEvents event1; | ||
MealEvents event2; | ||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
mealEventsController = new MealEventsController(mealEventsService); | ||
event1 = new MealEvents(); | ||
event1.setId(1); | ||
event1.setUserId(1); | ||
event1.setTitle("Event 1"); | ||
event1.setEvent_date(Date.valueOf("2023-10-01")); | ||
event1.setEvent_time(Time.valueOf("10:00:00")); | ||
|
||
event2 = new MealEvents(); | ||
event2.setId(2); | ||
event2.setUserId(1); | ||
event2.setTitle("Event 2"); | ||
event2.setEvent_date(Date.valueOf("2023-10-02")); | ||
event2.setEvent_time(Time.valueOf("11:00:00")); | ||
} | ||
|
||
@Test | ||
void testGetEventsByUserId_Success() { | ||
List<MealEvents> events = Arrays.asList(event1, event2); | ||
|
||
when(mealEventsService.getEventsByUserId(1)).thenReturn(events); | ||
ResponseEntity<List<MealEvents>> response = mealEventsController.getEventsByUserId(1); | ||
|
||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(2, Objects.requireNonNull(response.getBody()).size()); | ||
// Assert for the first event | ||
assertEquals("Event 1", response.getBody().get(0).getTitle()); | ||
assertEquals(1, response.getBody().get(0).getUserId()); | ||
assertEquals(Date.valueOf("2023-10-01"), response.getBody().get(0).getEvent_date()); | ||
assertEquals(Time.valueOf("10:00:00"), response.getBody().get(0).getEvent_time()); | ||
|
||
// Assert for the second event | ||
assertEquals("Event 2", response.getBody().get(1).getTitle()); | ||
assertEquals(1, response.getBody().get(1).getUserId()); | ||
assertEquals(Date.valueOf("2023-10-02"), response.getBody().get(1).getEvent_date()); | ||
assertEquals(Time.valueOf("11:00:00"), response.getBody().get(1).getEvent_time()); | ||
} | ||
|
||
@Test | ||
void testGetEventsByUserId_EmptyList() { | ||
when(mealEventsService.getEventsByUserId(2)).thenReturn(Arrays.asList()); | ||
ResponseEntity<List<MealEvents>> response = mealEventsController.getEventsByUserId(2); | ||
|
||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(0, response.getBody().size()); | ||
} | ||
|
||
@Test | ||
void testGetEventsByUserId_NonExistingUser() { | ||
when(mealEventsService.getEventsByUserId(999)).thenReturn(Arrays.asList()); | ||
ResponseEntity<List<MealEvents>> response = mealEventsController.getEventsByUserId(999); | ||
|
||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(0, response.getBody().size()); | ||
} | ||
@Test | ||
void testAddEvent_Success() { | ||
// When | ||
when(mealEventsService.saveEvent(event1)).thenReturn(event1); | ||
|
||
// Act | ||
ResponseEntity<MealEvents> response = mealEventsController.addEvent(event1); | ||
|
||
// Then | ||
assertEquals(HttpStatus.CREATED, response.getStatusCode()); | ||
assertEquals(1, Objects.requireNonNull(response.getBody()).getId()); | ||
assertEquals("Event 1", response.getBody().getTitle()); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
backend/omniplanner/src/test/java/com/main/omniplanner/NotesTests/NotesServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.main.omniplanner.NotesTests; | ||
|
||
import com.main.omniplanner.meals.MealEvents; | ||
import com.main.omniplanner.meals.MealEventsRepository; | ||
import com.main.omniplanner.meals.MealEventsService; | ||
import com.main.omniplanner.user.EventService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.sql.Date; | ||
import java.sql.Time; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class MealEventsServiceTest { | ||
|
||
private MealEventsService mealEventsService; | ||
private MealEvents mealEvents; | ||
|
||
@Mock | ||
private MealEventsRepository mealEventsRepository; | ||
|
||
@Mock | ||
private EventService eventService; | ||
|
||
private Date date; | ||
private Time time; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
mealEventsService = new MealEventsService(mealEventsRepository, eventService); | ||
mealEvents = new MealEvents(); | ||
mealEvents.setId(0); | ||
mealEvents.setUserId(0); | ||
mealEvents.setTitle("Team Meeting"); | ||
date = Date.valueOf("2024-11-05"); | ||
mealEvents.setEvent_date(date); | ||
time = Time.valueOf("10:30:00"); | ||
mealEvents.setEvent_time(time); | ||
} | ||
|
||
@Test | ||
public void testGetSaveEvent() { | ||
when(mealEventsRepository.save(mealEvents)).thenReturn(mealEvents); | ||
when(mealEventsRepository.findUpcomingByUserId(eq(0), anyLong())) | ||
.thenReturn(Collections.singletonList(mealEvents)); | ||
|
||
mealEventsService.saveEvent(mealEvents); | ||
List<MealEvents> mealEventsList = mealEventsService.getEventsByUserId(0); | ||
|
||
assertFalse(mealEventsList.isEmpty(), "The list should not be empty"); | ||
MealEvents testMealEvents = mealEventsList.get(0); | ||
assertEquals(0, testMealEvents.getId()); | ||
assertEquals(0, testMealEvents.getUserId()); | ||
assertEquals("Team Meeting", testMealEvents.getTitle()); | ||
assertEquals(date, testMealEvents.getEvent_date()); | ||
assertEquals(time, testMealEvents.getEvent_time()); | ||
|
||
verify(mealEventsRepository).save(mealEvents); | ||
verify(mealEventsRepository).findUpcomingByUserId(eq(0), anyLong()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
backend/omniplanner/src/test/java/com/main/omniplanner/NotesTests/NotesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.main.omniplanner.NotesTests; | ||
|
||
import com.main.omniplanner.meals.MealEvents; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.Date; | ||
import java.sql.Time; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class MealEventsTest { | ||
|
||
private MealEvents mealEvents; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mealEvents = new MealEvents(); | ||
} | ||
|
||
@Test | ||
public void testGetSetId() { | ||
mealEvents.setId(0); | ||
assertEquals(0, mealEvents.getId()); | ||
} | ||
|
||
@Test | ||
public void testGetsetUserId() { | ||
mealEvents.setUserId(0); | ||
assertEquals(0, mealEvents.getUserId()); | ||
} | ||
|
||
@Test | ||
public void testGetSetTitle() { | ||
mealEvents.setTitle("Team Meeting"); | ||
assertEquals("Team Meeting", mealEvents.getTitle()); | ||
} | ||
|
||
@Test | ||
public void testGetSetEvent_date() { | ||
Date date = Date.valueOf("2024-11-05"); | ||
mealEvents.setEvent_date(date); | ||
assertEquals(date, mealEvents.getEvent_date()); | ||
} | ||
|
||
@Test | ||
public void testGetSetEvent_time() { | ||
Time time = Time.valueOf("10:30:00"); | ||
mealEvents.setEvent_time(time); | ||
assertEquals(time, mealEvents.getEvent_time()); | ||
} | ||
|
||
@Test | ||
public void testGetSetRepeat_timeline() { | ||
mealEvents.setRepeat_timeline("Weekly"); | ||
assertEquals("Weekly", mealEvents.getRepeat_timeline()); | ||
} | ||
|
||
@Test | ||
public void testGetSetRepeating() { | ||
mealEvents.setRepeating(true); | ||
assertTrue(mealEvents.isRepeating()); | ||
} | ||
|
||
@Test | ||
public void testGetSetIngredients() { | ||
mealEvents.setIngredients("Chicken, Rice, Broccoli"); | ||
assertEquals("Chicken, Rice, Broccoli", mealEvents.getIngredients()); | ||
} | ||
} |