Updated BaseStaxReader; Updated tests; Fix issue with stax reader;
This commit is contained in:
parent
9a1ead682d
commit
9b2e723f4c
5 changed files with 79 additions and 43 deletions
|
|
@ -36,18 +36,16 @@ public class MonitoringRequest {
|
|||
|
||||
private Subject currentSubject;
|
||||
|
||||
public Subject getCurrentSubject() {
|
||||
if (currentSubject == null) {
|
||||
public void createCurrentSubject() {
|
||||
currentSubject = new Subject();
|
||||
}
|
||||
|
||||
public Subject getCurrentSubject() {
|
||||
return currentSubject;
|
||||
}
|
||||
|
||||
public void finalizeSubject() {
|
||||
if (currentSubject != null) {
|
||||
getSubjects().getSubject().add(currentSubject);
|
||||
currentSubject = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ public abstract class BaseStaxReader<T> implements StaxReader<T> {
|
|||
@Override
|
||||
public T read(XMLStreamReader reader) throws XMLStreamException {
|
||||
T obj = createObject();
|
||||
try {
|
||||
while (reader.hasNext()) {
|
||||
boolean breakExecution = false;
|
||||
int ev = reader.next();
|
||||
|
|
@ -22,6 +23,7 @@ public abstract class BaseStaxReader<T> implements StaxReader<T> {
|
|||
case XMLStreamConstants.START_ELEMENT -> {
|
||||
buffer.setLength(0);
|
||||
startElement(reader.getLocalName());
|
||||
innerProcessStartElement(reader, obj);
|
||||
}
|
||||
case XMLStreamConstants.CHARACTERS -> {
|
||||
String text = reader.getText().trim();
|
||||
|
|
@ -36,10 +38,15 @@ public abstract class BaseStaxReader<T> implements StaxReader<T> {
|
|||
if (breakExecution) break;
|
||||
}
|
||||
return obj;
|
||||
} catch (Exception e) {
|
||||
stack.clear();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract boolean innerProcess(XMLStreamReader reader, T obj) throws XMLStreamException;
|
||||
|
||||
protected abstract void innerProcessStartElement(XMLStreamReader reader, T obj) throws XMLStreamException;
|
||||
|
||||
public void startElement(String localName) {
|
||||
localName = substringAfterColon(localName);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package ru.nbch.credit_tracker.service.xml.stax;
|
|||
|
||||
import ru.nbch.credit_tracker.entities.request.monitoring_request.MonitoringRequest;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
public class MonitoringRequestStaxReader extends BaseStaxReader<MonitoringRequest> {
|
||||
|
|
@ -36,4 +37,15 @@ public class MonitoringRequestStaxReader extends BaseStaxReader<MonitoringReques
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void innerProcessStartElement(XMLStreamReader reader, MonitoringRequest obj) throws XMLStreamException {
|
||||
String text = buffer.toString();
|
||||
String currPath = getPath();
|
||||
|
||||
switch (currPath) {
|
||||
case "/MonitoringRequest/Subjects/Subject" ->
|
||||
obj.createCurrentSubject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import ru.nbch.credit_tracker.entities.response.external_errors.Error;
|
|||
import ru.nbch.credit_tracker.entities.response.external_errors.InputParams;
|
||||
import ru.nbch.credit_tracker.entities.response.external_errors.SgnlReport;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
@Component
|
||||
|
|
@ -39,4 +40,9 @@ public class SgnlReportStatusStaxReader extends BaseStaxReader<SgnlReport> {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void innerProcessStartElement(XMLStreamReader reader, SgnlReport obj) throws XMLStreamException {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package ru.nbch.credit_tracker.utils;
|
||||
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
|
|
@ -11,34 +10,42 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.nbch.credit_tracker.entities.request.monitoring_request.MonitoringRequest;
|
||||
import ru.nbch.credit_tracker.enums.SignalError;
|
||||
import ru.nbch.credit_tracker.enums.XmlErrorFormat;
|
||||
import ru.nbch.credit_tracker.exceptions.SignalClientException;
|
||||
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
|
||||
import ru.nbch.credit_tracker.service.xml.jaxb.XmlProcessorImplAnyClasses;
|
||||
import ru.nbch.credit_tracker.service.xml.stax.MonitoringRequestStaxReader;
|
||||
import ru.nbch.credit_tracker.service.xml.stax.StaxReader;
|
||||
import ru.nbch.credit_tracker.service.xml.stax.StaxXmlProcessor;
|
||||
import ru.nbch.credit_tracker.service.xml.stax.XmlProcessorStaxImpl;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import static ru.nbch.credit_tracker.utils.error.ExceptionUtils.clientExcp;
|
||||
|
||||
@SpringBootTest(classes = {UtilTest.TestConfig.class})
|
||||
public class UtilTest {
|
||||
|
||||
private final IXmlProcessor xmlProcessor;
|
||||
private final StaxXmlProcessor staxXmlProcessor;
|
||||
|
||||
@Autowired
|
||||
public UtilTest(IXmlProcessor xmlProcessor) {
|
||||
this.xmlProcessor = xmlProcessor;
|
||||
public UtilTest(StaxXmlProcessor staxXmlProcessor) {
|
||||
this.staxXmlProcessor = staxXmlProcessor;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig {
|
||||
@Bean
|
||||
public IXmlProcessor xmlProcessor() throws JAXBException {
|
||||
return new XmlProcessorImplAnyClasses(Charset.forName("windows-1251"),
|
||||
MonitoringRequest.class);
|
||||
public StaxXmlProcessor xmlProcessor() {
|
||||
List<StaxReader<?>> staxReader = new ArrayList<>();
|
||||
staxReader.add(new MonitoringRequestStaxReader());
|
||||
return new XmlProcessorStaxImpl(staxReader);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -125,13 +132,18 @@ public class UtilTest {
|
|||
@MethodSource("validationTestCases")
|
||||
void testMonitoringRequestValidation(String path, SignalError signalError, Object[] args) {
|
||||
ZipInputStream zipInputStream = null;
|
||||
MonitoringRequest monitoringRequest;
|
||||
MonitoringRequest monitoringRequest = null;
|
||||
SignalClientException ex = null;
|
||||
try {
|
||||
zipInputStream = new ZipInputStream(getClass().getResourceAsStream(path));
|
||||
zipInputStream.getNextEntry();
|
||||
monitoringRequest = xmlProcessor.read(zipInputStream, MonitoringRequest.class);
|
||||
monitoringRequest = staxXmlProcessor.read(zipInputStream.readAllBytes(), MonitoringRequest.class);
|
||||
} catch (Exception e) {
|
||||
if (e.getCause() instanceof XMLStreamException) {
|
||||
ex = clientExcp(SignalError.ERROR_011, XmlErrorFormat.MonitoringError);
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} finally {
|
||||
if (zipInputStream != null) {
|
||||
try {
|
||||
|
|
@ -142,12 +154,13 @@ public class UtilTest {
|
|||
}
|
||||
}
|
||||
|
||||
SignalClientException ex = null;
|
||||
if(ex == null) {
|
||||
try {
|
||||
ValidationUtil.validateMonitoringRequest(monitoringRequest);
|
||||
} catch (SignalClientException e) {
|
||||
ex = e;
|
||||
}
|
||||
}
|
||||
if (ex != null) {
|
||||
Assertions.assertEquals(signalError, ex.getErrorMessage().getSubject());
|
||||
Assertions.assertArrayEquals(args, ex.getErrorMessage().getArgs());
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue