parent
2022c1612e
commit
798281fd17
8 changed files with 239 additions and 15 deletions
@ -0,0 +1,140 @@ |
||||
package org.springblade.lims.controller; |
||||
|
||||
import org.junit.jupiter.api.BeforeEach; |
||||
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.springblade.lims.entry.ExamineItem; |
||||
import org.springblade.lims.entry.OriginalRecordTemplate; |
||||
import org.springblade.lims.entry.Reagent; |
||||
import org.springblade.lims.entry.TemplateField; |
||||
import org.springblade.lims.service.IExamineItemService; |
||||
import org.springblade.lims.service.IOriginalRecordTemplateService; |
||||
import org.springblade.lims.service.IReagentFormulaService; |
||||
import org.springblade.lims.service.IReagentService; |
||||
import org.springblade.lims.service.ITemplateFieldService; |
||||
import org.springframework.test.web.servlet.MockMvc; |
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
|
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.Mockito.when; |
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class ReagentFormulaControllerTest { |
||||
|
||||
private MockMvc mockMvc; |
||||
|
||||
@Mock |
||||
private IReagentFormulaService reagentFormulaService; |
||||
|
||||
@Mock |
||||
private IReagentService reagentService; |
||||
|
||||
@Mock |
||||
private IExamineItemService examineItemService; |
||||
|
||||
@Mock |
||||
private ITemplateFieldService templateFieldService; |
||||
|
||||
@Mock |
||||
private IOriginalRecordTemplateService originalRecordTemplateService; |
||||
|
||||
@InjectMocks |
||||
private ReagentFormulaController controller; |
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); |
||||
} |
||||
|
||||
@Test |
||||
void templatePath_ShouldReturnNumberTypeFieldNames() throws Exception { |
||||
ExamineItem item = new ExamineItem(); |
||||
item.setId(1L); |
||||
item.setReagentId("5"); |
||||
|
||||
OriginalRecordTemplate template = new OriginalRecordTemplate(); |
||||
template.setId(10L); |
||||
|
||||
TemplateField field1 = new TemplateField(); |
||||
field1.setFieldName("OD值"); |
||||
field1.setFieldType("number"); |
||||
TemplateField field2 = new TemplateField(); |
||||
field2.setFieldName("临界值"); |
||||
field2.setFieldType("number"); |
||||
|
||||
when(examineItemService.list(any())).thenReturn(Collections.singletonList(item)); |
||||
when(originalRecordTemplateService.getByExamineItemId(1L)).thenReturn(Collections.singletonList(template)); |
||||
when(templateFieldService.getFieldsByTemplateIdAndType(10L, "number")) |
||||
.thenReturn(Arrays.asList(field1, field2)); |
||||
|
||||
mockMvc.perform(get("/reagentFormula/variables") |
||||
.param("reagentId", "5")) |
||||
.andExpect(status().isOk()) |
||||
.andExpect(jsonPath("$.code").value(200)) |
||||
.andExpect(jsonPath("$.data").isArray()) |
||||
.andExpect(jsonPath("$.data.length()").value(2)) |
||||
.andExpect(jsonPath("$.data[0].name").value("OD值")) |
||||
.andExpect(jsonPath("$.data[0].type").value("number")) |
||||
.andExpect(jsonPath("$.data[1].name").value("临界值")) |
||||
.andExpect(jsonPath("$.data[1].type").value("number")); |
||||
} |
||||
|
||||
@Test |
||||
void fallback_WhenNoTemplateFields_ShouldUseResultDeterminationMethod() throws Exception { |
||||
ExamineItem item = new ExamineItem(); |
||||
item.setId(1L); |
||||
item.setReagentId("5"); |
||||
|
||||
OriginalRecordTemplate template = new OriginalRecordTemplate(); |
||||
template.setId(10L); |
||||
|
||||
Reagent reagent = new Reagent(); |
||||
reagent.setResultDeterminationMethod( |
||||
"{\"variables\":[{\"name\":\"OD值\",\"type\":\"number\"},{\"name\":\"临界值\",\"type\":\"number\"}]}" |
||||
); |
||||
|
||||
when(examineItemService.list(any())).thenReturn(Collections.singletonList(item)); |
||||
when(originalRecordTemplateService.getByExamineItemId(1L)).thenReturn(Collections.singletonList(template)); |
||||
when(templateFieldService.getFieldsByTemplateIdAndType(10L, "number")) |
||||
.thenReturn(Collections.emptyList()); |
||||
when(reagentService.getById(5L)).thenReturn(reagent); |
||||
|
||||
mockMvc.perform(get("/reagentFormula/variables") |
||||
.param("reagentId", "5")) |
||||
.andExpect(status().isOk()) |
||||
.andExpect(jsonPath("$.code").value(200)) |
||||
.andExpect(jsonPath("$.data").isArray()) |
||||
.andExpect(jsonPath("$.data.length()").value(2)) |
||||
.andExpect(jsonPath("$.data[0].name").value("OD值")) |
||||
.andExpect(jsonPath("$.data[0].type").value("number")) |
||||
.andExpect(jsonPath("$.data[1].name").value("临界值")) |
||||
.andExpect(jsonPath("$.data[1].type").value("number")); |
||||
} |
||||
|
||||
@Test |
||||
void nullReagentId_ShouldReturn400() throws Exception { |
||||
mockMvc.perform(get("/reagentFormula/variables")) |
||||
.andExpect(status().isBadRequest()); |
||||
} |
||||
|
||||
@Test |
||||
void noMatchingItems_ShouldReturnEmptyList() throws Exception { |
||||
when(examineItemService.list(any())).thenReturn(Collections.emptyList()); |
||||
|
||||
mockMvc.perform(get("/reagentFormula/variables") |
||||
.param("reagentId", "999")) |
||||
.andExpect(status().isOk()) |
||||
.andExpect(jsonPath("$.code").value(200)) |
||||
.andExpect(jsonPath("$.data").isArray()) |
||||
.andExpect(jsonPath("$.data.length()").value(0)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue