Skip to content

Commit

Permalink
Merge pull request #40 from Callhub-Connect/session-tests
Browse files Browse the repository at this point in the history
Added tests for session use case
  • Loading branch information
zjayee authored Dec 4, 2023
2 parents d0c0e82 + 6e8e749 commit 9d51b46
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 45 deletions.
8 changes: 8 additions & 0 deletions src/main/java/callhub/connect/entities/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Document("sessions")
public class Session {
Expand Down Expand Up @@ -51,4 +52,11 @@ public void setInactive(){
this.active = false;
}

public boolean getIsActive() {
return this.active;
}

public List<String> getDocuments() {
return this.documents;
}
}

This file was deleted.

95 changes: 95 additions & 0 deletions src/test/java/callhub/connect/session/SessionControllerTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package callhub.connect.session;

import callhub.connect.data_access.DocumentRepository;
import callhub.connect.data_access.SessionRepository;
import callhub.connect.entities.FileDocument;
import callhub.connect.entities.Session;
import callhub.connect.interface_adapter.session.SessionController;
import callhub.connect.use_case.session.SessionInputBoundary;
import callhub.connect.use_case.session.SessionInputData;
import callhub.connect.use_case.session.SessionInteractor;
import callhub.connect.use_case.session.*;
import org.bson.types.Binary;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

import java.time.LocalDate;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
public class SessionControllerTests {

@Autowired
MockMvc mockMvc;

@MockBean
public SessionRepository sessionRepository;


@Test
public void createSessionTest() throws Exception {
when(sessionRepository.save(any())).thenReturn(new Session(
true,
"ABCDEF"
));

RequestBuilder request = get("/session/new-session?code=ABCDEF");
mockMvc.perform(request).andExpect(status().isOk());
}

@Test
void testJoinSession() throws Exception {
Session mockSession = new Session(true, "ABCDEF");

when(sessionRepository.getSessionsByActiveAndCode(anyBoolean(), anyString())).thenReturn(mockSession);

// Perform the request and verify the response
mockMvc.perform(get("/session/join/ABCDEF"))
.andExpect(status().isOk());
}

@Test
void testEndSession() throws Exception {
Session mockSession = new Session(true, "ABCDEF");
when(sessionRepository.getSessionsByActiveAndCode(anyBoolean(), anyString())).thenReturn(mockSession);

// Perform the request and verify the response
mockMvc.perform(get("/session/end-session/ABCDEF"))
.andExpect(status().isOk());
assert !mockSession.getIsActive();
}


@Test
void testAllSession() throws Exception {
Session mockSession = new Session(true, "ABCDEF");

when(sessionRepository.getSessionsByActiveAndCode(anyBoolean(), anyString())).thenReturn(mockSession);
when(sessionRepository.save(any())).thenReturn(mockSession);

RequestBuilder newRequest = get("/session/new-session?code=ABCDEF");
mockMvc.perform(newRequest).andExpect(status().isOk());

RequestBuilder joinRequest = get("/session/join/ABCDEF");
mockMvc.perform(joinRequest).andExpect(status().isOk());

RequestBuilder endRequest = get("/session/end-session/ABCDEF");
mockMvc.perform(endRequest).andExpect(status().isOk());
}
}

0 comments on commit 9d51b46

Please sign in to comment.