package com.kpaudel.controller;
import com.kpaudel.model.ApplicationStatus;
import com.kpaudel.model.Company;
import com.kpaudel.model.JobApplication;
import com.kpaudel.repository.CompanyRepository;
import com.kpaudel.repository.JobApplicationRepository;
import com.kpaudel.service.JobService;
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.test.web.servlet.MockMvc;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@WebMvcTest(ApiController.class)
public class ApiControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JobService jobService;
@MockBean
private CompanyRepository companyRepository;
@MockBean
private JobApplicationRepository jobApplicationRepository;
@Test
public void testListJobs() throws Exception {
Company testCompnay = new Company();
testCompnay.setId(UUID.randomUUID());
testCompnay.setName("Home-Lab");
testCompnay.setWebsite("https://home-lab.com");
JobApplication softwareApplication = JobApplication.builder()
.id(UUID.randomUUID())
.role("Software Engineer")
.location("Remote")
.company(testCompnay)
.status(ApplicationStatus.APPLIED)
.build();
JobApplication devOpsEngineer = JobApplication.builder()
.id(UUID.randomUUID())
.role("DevOps Engineer")
.location("New York, NY")
.company(testCompnay)
.status(ApplicationStatus.INTERESTED)
.build();
when(this.jobApplicationRepository.findAll()).thenReturn(List.of(softwareApplication,devOpsEngineer));
mockMvc.perform(get("/api/jobs")).andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.length()").value(2))
.andExpect(jsonPath("$[0].role").value("Software Engineer"));
}
@Test
public void testListJobsByCompany() throws Exception {
Company testCompnay = new Company();
testCompnay.setId(UUID.randomUUID());
testCompnay.setName("Home-Lab");
testCompnay.setWebsite("https://home-lab.com");
List<JobApplication> applications = new ArrayList<>();
applications.add(JobApplication.builder()
.id(UUID.randomUUID())
.role("Software Engineer")
.location("Remote")
.company(testCompnay)
.status(ApplicationStatus.APPLIED)
.build());
applications.add(JobApplication.builder()
.id(UUID.randomUUID())
.role("DevOps Engineer")
.location("New York, NY")
.company(testCompnay)
.status(ApplicationStatus.INTERESTED)
.build());
when(this.jobApplicationRepository.findByCompanyId(testCompnay.getId())).thenReturn(applications);
mockMvc.perform(get("/api/companies/"+testCompnay.getId()+"/jobs")).andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.length()").value(2))
.andExpect(jsonPath("$[0].role").value("Software Engineer"));
}
}