test: Add FormOfAdressControllerTest.java and SecondarySkillControllerTest.java

pull/1/head
Tarik Gökmen 2024-11-09 15:42:09 +01:00
parent cf608ceb28
commit d16c98ad04
2 changed files with 261 additions and 0 deletions

View File

@ -0,0 +1,131 @@
package com.maradona.backend.controllers.api;
import com.maradona.backend.entities.FormOfAddress;
import com.maradona.backend.services.FormOfAddressService;
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.util.Optional;
import java.util.Arrays;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@WebMvcTest(FormOfAdressController.class)
public class FormOfAdressControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private FormOfAddressService formOfAdressService;
@Test
public void testGetFormOfAdressById() throws Exception {
FormOfAddress formOfAddress = new FormOfAddress();
formOfAddress.setId(1L);
formOfAddress.setDescription("Mr.");
when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.of(formOfAddress));
mockMvc.perform(get("/api/form-of-adress")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.description").value("Mr."));
}
@Test
public void testGetAllFormOfAdresses() throws Exception {
FormOfAddress form1 = new FormOfAddress();
form1.setId(1L);
form1.setDescription("Mr.");
FormOfAddress form2 = new FormOfAddress();
form2.setId(2L);
form2.setDescription("Ms.");
when(formOfAdressService.getAllFormOfAddresses()).thenReturn(Arrays.asList(form1, form2));
mockMvc.perform(get("/api/form-of-adress/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].description").value("Mr."))
.andExpect(jsonPath("$[1].id").value(2))
.andExpect(jsonPath("$[1].description").value("Ms."));
}
@Test
public void testCreateFormOfAdress() throws Exception {
FormOfAddress formOfAddress = new FormOfAddress();
formOfAddress.setId(1L);
formOfAddress.setDescription("Dr.");
when(formOfAdressService.saveFormOfAddress(any(FormOfAddress.class))).thenReturn(formOfAddress);
mockMvc.perform(post("/api/form-of-adress")
.contentType(MediaType.APPLICATION_JSON)
.content("\"Dr.\""))
.andExpect(status().isOk());
}
@Test
public void testUpdateFormOfAdress() throws Exception {
FormOfAddress existingForm = new FormOfAddress();
existingForm.setId(1L);
existingForm.setDescription("Mr.");
when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.of(existingForm));
mockMvc.perform(put("/api/form-of-adress")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON)
.content("\"Prof.\""))
.andExpect(status().isOk());
}
@Test
public void testUpdateFormOfAdressNotFound() throws Exception {
when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.empty());
mockMvc.perform(put("/api/form-of-adress")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON)
.content("\"Prof.\""))
.andExpect(status().isNotFound());
}
@Test
public void testDeleteFormOfAdress() throws Exception {
FormOfAddress formOfAddress = new FormOfAddress();
formOfAddress.setId(1L);
formOfAddress.setDescription("Mr.");
when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.of(formOfAddress));
mockMvc.perform(delete("/api/form-of-adress")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
@Test
public void testDeleteFormOfAdressNotFound() throws Exception {
when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.empty());
mockMvc.perform(delete("/api/form-of-adress")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
}

View File

@ -0,0 +1,130 @@
package com.maradona.backend.controllers.api;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.services.SecondarySkillService;
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.util.Arrays;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@WebMvcTest(SecondarySkillController.class)
public class SecondarySkillControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SecondarySkillService secondarySkillService;
@Test
public void testGetSecondarySkillById() throws Exception {
SecondarySkill skill = new SecondarySkill();
skill.setSsid(1L);
skill.setDescription("Java Programming");
when(secondarySkillService.getSecondarySkillById(1L)).thenReturn(Optional.of(skill));
mockMvc.perform(get("/api/secondary-skill")
.param("id", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.ssid").value(1))
.andExpect(jsonPath("$.description").value("Java Programming"));
}
@Test
public void testGetAllSecondarySkills() throws Exception {
SecondarySkill skill1 = new SecondarySkill();
skill1.setSsid(1L);
skill1.setDescription("Java Programming");
SecondarySkill skill2 = new SecondarySkill();
skill2.setSsid(2L);
skill2.setDescription("Python Programming");
when(secondarySkillService.getAllSecondarySkills()).thenReturn(Arrays.asList(skill1, skill2));
mockMvc.perform(get("/api/secondary-skill/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[1].ssid").value(1))
.andExpect(jsonPath("$[1].description").value("Java Programming"))
.andExpect(jsonPath("$[2].ssid").value(2))
.andExpect(jsonPath("$[2].description").value("Python Programming"));
}
@Test
public void testGetSecondarySkillsByPrimarySkillId() throws Exception {
SecondarySkill skill1 = new SecondarySkill();
skill1.setSsid(1L);
skill1.setDescription("Data Analysis");
SecondarySkill skill2 = new SecondarySkill();
skill2.setSsid(2L);
skill2.setDescription("Data Visualization");
when(secondarySkillService.getSecondarySkillsByPrimarySkillId(100L)).thenReturn(Arrays.asList(skill1, skill2));
mockMvc.perform(get("/api/secondary-skill/from-primary-skill")
.param("primarySkillId", "100")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[1].ssid").value(1))
.andExpect(jsonPath("$[1].description").value("Data Analysis"))
.andExpect(jsonPath("$[2].ssid").value(2))
.andExpect(jsonPath("$[2].description").value("Data Visualization"));
}
@Test
public void testCreateSecondarySkill() throws Exception {
SecondarySkill skill = new SecondarySkill();
skill.setSsid(1L);
skill.setDescription("Machine Learning");
when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(skill);
mockMvc.perform(post("/api/secondary-skill")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Machine Learning\"}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.ssid").value(1))
.andExpect(jsonPath("$.description").value("Machine Learning"));
}
@Test
public void testUpdateSecondarySkill() throws Exception {
SecondarySkill updatedSkill = new SecondarySkill();
updatedSkill.setSsid(1L);
updatedSkill.setDescription("Advanced Machine Learning");
when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(updatedSkill);
mockMvc.perform(put("/api/secondary-skill")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"ssid\":1,\"description\":\"Advanced Machine Learning\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.ssid").value(1))
.andExpect(jsonPath("$.description").value("Advanced Machine Learning"));
}
@Test
public void testDeleteSecondarySkill() throws Exception {
Long skillId = 1L;
mockMvc.perform(delete("/api/secondary-skill")
.param("id", String.valueOf(skillId))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
}