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

Subject test controller #194

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package ufrn.imd.boraPagar.subject;


import org.mockito.Mockito;


import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
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.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@WebMvcTest(SubjectController.class)
public class SubjectControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private SubjectService subjectService;

private int pageSize = 5;
private int firstPage = 0;

@Test
public void shouldFindAllByTotalHours() throws Exception {
int desireHour = 1;
List<SubjectModel> expected = new ArrayList<>();
expected.add(new SubjectModel());

Mockito.when(subjectService.findAllByTotalHours(desireHour))
.thenReturn(expected);

mockMvc.perform(MockMvcRequestBuilders.get("/subjects?totalHours={totalHours}", String.valueOf(desireHour)))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
public void shouldFindAllByModality() throws Exception {
SubjectModalityType desire = SubjectModalityType.IN_PERSON;
List<SubjectModel> expected = new ArrayList<>();
expected.add(new SubjectModel());

Mockito.when(subjectService.findAllByModality(desire))
.thenReturn(expected);

mockMvc.perform(MockMvcRequestBuilders.get("/subjects?modality={modality}", String.valueOf(desire)))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
public void shouldFindByCode() throws Exception {
String desireCode = "1";
SubjectModel expected = new SubjectModel();

Mockito.when(subjectService.findByCode(desireCode))
.thenReturn(Optional.of(expected));

mockMvc.perform(MockMvcRequestBuilders.get("/subjects?code={code}", desireCode))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
public void shouldFindByComponentID() throws Exception {
String desireComponentId = "1";
SubjectModel expected = new SubjectModel();

Mockito.when(subjectService.findByComponentID(desireComponentId))
.thenReturn(Optional.of(expected));

mockMvc.perform(MockMvcRequestBuilders.get("/subjects?componentID={componentID}", desireComponentId))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
public void shouldAddInterestedUserByComponentID() throws Exception {
String credential = "credential";
String desireComponentId = "1";
SubjectModel expected = new SubjectModel();

Mockito.when(subjectService.addInterestedUserByComponentID(credential, desireComponentId))
.thenReturn(expected);

mockMvc.perform(MockMvcRequestBuilders.post("/subjects/interested", desireComponentId)
.header(credential, credential)
.param("componentID", desireComponentId))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}

@Test
public void shouldRemoveInterestedUserByComponentID() throws Exception {
String credential = "credential";
String desireComponentId = "1";
SubjectModel expected = new SubjectModel();

Mockito.when(subjectService.removeInterestedUserByComponentID(credential, desireComponentId))
.thenReturn(expected);

mockMvc.perform(MockMvcRequestBuilders.delete("/subjects/interested", desireComponentId)
.header(credential, credential)
.param("componentID", desireComponentId))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
}

@Test
public void shouldFindAll() throws Exception {
String credential = "credential";
SubjectModel obj = new SubjectModel();

List<SubjectModel> list = new ArrayList<>();
list.add(obj);

Page<SubjectModel> expectedPage = new PageImpl<>(list);

Mockito.when(subjectService.findAllByPage(credential, PageRequest.of(firstPage, pageSize)))
.thenReturn(expectedPage);

mockMvc.perform(MockMvcRequestBuilders.get("/subjects/findAll")
.param("page", String.valueOf(firstPage))
.param("size", String.valueOf(pageSize)))
.andExpect(MockMvcResultMatchers.status().isOk());
// .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
}
}
Loading