mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-05-24 19:02:01 +00:00
Fix test compilation around pipeline processor (#3554)
## Summary - allow tests to spy on PipelineProcessor web requests - fix ResponseEntity usage in PipelineProcessorTest ## Testing - `./gradlew test --offline` *(fails: No route to host while downloading gradle-8.14-all.zip)*
This commit is contained in:
parent
d59e39b4b6
commit
9fe49c494d
@ -93,6 +93,7 @@ public class PipelineProcessor {
|
||||
ByteArrayOutputStream logStream = new ByteArrayOutputStream();
|
||||
PrintStream logPrintStream = new PrintStream(logStream);
|
||||
boolean hasErrors = false;
|
||||
boolean filtersApplied = false;
|
||||
for (PipelineOperation pipelineOperation : config.getOperations()) {
|
||||
String operation = pipelineOperation.getOperation();
|
||||
boolean isMultiInputOperation = apiDocService.isMultiInput(operation);
|
||||
@ -134,7 +135,7 @@ public class PipelineProcessor {
|
||||
if (operation.startsWith("filter-")
|
||||
&& (response.getBody() == null
|
||||
|| response.getBody().length == 0)) {
|
||||
result.setFiltersApplied(true);
|
||||
filtersApplied = true;
|
||||
log.info("Skipping file due to filtering {}", operation);
|
||||
continue;
|
||||
}
|
||||
@ -215,12 +216,12 @@ public class PipelineProcessor {
|
||||
log.error("Errors occurred during processing. Log: {}", logStream.toString());
|
||||
}
|
||||
result.setHasErrors(hasErrors);
|
||||
result.setFiltersApplied(hasErrors);
|
||||
result.setFiltersApplied(filtersApplied);
|
||||
result.setOutputFiles(outputFiles);
|
||||
return result;
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> sendWebRequest(String url, MultiValueMap<String, Object> body) {
|
||||
/* package */ ResponseEntity<byte[]> sendWebRequest(String url, MultiValueMap<String, Object> body) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
// Set up headers, including API key
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
@ -0,0 +1,76 @@
|
||||
package stirling.software.SPDF.controller.api.pipeline;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
|
||||
import stirling.software.SPDF.model.PipelineConfig;
|
||||
import stirling.software.SPDF.model.PipelineOperation;
|
||||
import stirling.software.SPDF.model.PipelineResult;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PipelineProcessorTest {
|
||||
|
||||
@Mock
|
||||
ApiDocService apiDocService;
|
||||
|
||||
@Mock
|
||||
UserServiceInterface userService;
|
||||
|
||||
@Mock
|
||||
ServletContext servletContext;
|
||||
|
||||
PipelineProcessor pipelineProcessor;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
pipelineProcessor = spy(new PipelineProcessor(apiDocService, userService, servletContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runPipelineWithFilterSetsFlag() throws Exception {
|
||||
PipelineOperation op = new PipelineOperation();
|
||||
op.setOperation("filter-page-count");
|
||||
op.setParameters(Map.of());
|
||||
PipelineConfig config = new PipelineConfig();
|
||||
config.setOperations(List.of(op));
|
||||
|
||||
Resource file = new ByteArrayResource("data".getBytes()) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return "test.pdf";
|
||||
}
|
||||
};
|
||||
|
||||
List<Resource> files = List.of(file);
|
||||
|
||||
when(apiDocService.isMultiInput("filter-page-count")).thenReturn(false);
|
||||
when(apiDocService.getExtensionTypes(false, "filter-page-count")).thenReturn(List.of("pdf"));
|
||||
|
||||
doReturn(new ResponseEntity<>(new byte[0], HttpStatus.OK))
|
||||
.when(pipelineProcessor)
|
||||
.sendWebRequest(anyString(), any());
|
||||
|
||||
PipelineResult result = pipelineProcessor.runPipelineAgainstFiles(files, config);
|
||||
|
||||
assertTrue(result.isFiltersApplied(), "Filter flag should be true when operation filters file");
|
||||
assertFalse(result.isHasErrors(), "No errors should occur");
|
||||
assertTrue(result.getOutputFiles().isEmpty(), "Filtered file list should be empty");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user