Added MonitoringRequestStaxReader; Do not use jaxb to read MonitoringRequest
This commit is contained in:
parent
29d57017ab
commit
adfd2d4d28
9 changed files with 207 additions and 15 deletions
|
|
@ -3,7 +3,6 @@ package ru.nbch.credit_tracker.config.xml;
|
||||||
import jakarta.xml.bind.JAXBException;
|
import jakarta.xml.bind.JAXBException;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import ru.nbch.credit_tracker.entities.request.monitoring_request.MonitoringRequest;
|
|
||||||
import ru.nbch.credit_tracker.entities.response.errors.MonitoringError;
|
import ru.nbch.credit_tracker.entities.response.errors.MonitoringError;
|
||||||
import ru.nbch.credit_tracker.entities.response.monitoring_signal_response.MonitoringSignalResponse;
|
import ru.nbch.credit_tracker.entities.response.monitoring_signal_response.MonitoringSignalResponse;
|
||||||
import ru.nbch.credit_tracker.entities.response.online_download.OnlineDownloadReport;
|
import ru.nbch.credit_tracker.entities.response.online_download.OnlineDownloadReport;
|
||||||
|
|
@ -12,10 +11,7 @@ import ru.nbch.credit_tracker.entities.response.success.MonitoringResponse;
|
||||||
import ru.nbch.credit_tracker.entities.signal_package.Signals;
|
import ru.nbch.credit_tracker.entities.signal_package.Signals;
|
||||||
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
|
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.jaxb.XmlProcessorImplAnyClasses;
|
||||||
import ru.nbch.credit_tracker.service.xml.stax.SgnlReportStatusStaxReader;
|
import ru.nbch.credit_tracker.service.xml.stax.*;
|
||||||
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 java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -28,7 +24,6 @@ public class XmlConfig {
|
||||||
public IXmlProcessor xmlProcessor() throws JAXBException {
|
public IXmlProcessor xmlProcessor() throws JAXBException {
|
||||||
return new XmlProcessorImplAnyClasses(Charset.forName("windows-1251"),
|
return new XmlProcessorImplAnyClasses(Charset.forName("windows-1251"),
|
||||||
// unmarshalling classes
|
// unmarshalling classes
|
||||||
MonitoringRequest.class,
|
|
||||||
OnlineDownloadReport.class,
|
OnlineDownloadReport.class,
|
||||||
SgnlOnlineReport.class,
|
SgnlOnlineReport.class,
|
||||||
// marshalling classes
|
// marshalling classes
|
||||||
|
|
@ -42,6 +37,7 @@ public class XmlConfig {
|
||||||
public StaxXmlProcessor staxXmlProcessor() {
|
public StaxXmlProcessor staxXmlProcessor() {
|
||||||
List<StaxReader<?>> staxReader = new ArrayList<>();
|
List<StaxReader<?>> staxReader = new ArrayList<>();
|
||||||
staxReader.add(new SgnlReportStatusStaxReader());
|
staxReader.add(new SgnlReportStatusStaxReader());
|
||||||
|
staxReader.add(new MonitoringRequestStaxReader());
|
||||||
return new XmlProcessorStaxImpl(staxReader);
|
return new XmlProcessorStaxImpl(staxReader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,5 +27,27 @@ public class MonitoringRequest {
|
||||||
@XmlElement(name = "Subjects", required = true)
|
@XmlElement(name = "Subjects", required = true)
|
||||||
protected Subjects subjects;
|
protected Subjects subjects;
|
||||||
|
|
||||||
|
public Subjects getSubjects() {
|
||||||
|
if (subjects == null) {
|
||||||
|
subjects = new Subjects();
|
||||||
|
}
|
||||||
|
return subjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Subject currentSubject;
|
||||||
|
|
||||||
|
public Subject getCurrentSubject() {
|
||||||
|
if (currentSubject == null) {
|
||||||
|
currentSubject = new Subject();
|
||||||
|
}
|
||||||
|
return currentSubject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void finalizeSubject() {
|
||||||
|
if (currentSubject != null) {
|
||||||
|
getSubjects().getSubject().add(currentSubject);
|
||||||
|
currentSubject = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,12 @@ import ru.nbch.credit_tracker.service.SignalService;
|
||||||
import ru.nbch.credit_tracker.service.indic.PhoneService;
|
import ru.nbch.credit_tracker.service.indic.PhoneService;
|
||||||
import ru.nbch.credit_tracker.service.scoring.AuthorizationService;
|
import ru.nbch.credit_tracker.service.scoring.AuthorizationService;
|
||||||
import ru.nbch.credit_tracker.service.signal.SignalRestService;
|
import ru.nbch.credit_tracker.service.signal.SignalRestService;
|
||||||
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
|
import ru.nbch.credit_tracker.service.xml.stax.StaxXmlProcessor;
|
||||||
import ru.nbch.credit_tracker.utils.CTUtil;
|
import ru.nbch.credit_tracker.utils.CTUtil;
|
||||||
import ru.nbch.credit_tracker.utils.ValidationUtil;
|
import ru.nbch.credit_tracker.utils.ValidationUtil;
|
||||||
import ru.nbch.credit_tracker.utils.ZipUtils;
|
import ru.nbch.credit_tracker.utils.ZipUtils;
|
||||||
|
|
||||||
|
import javax.xml.stream.XMLStreamException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
@ -52,7 +53,7 @@ public class SignalFacade {
|
||||||
private final AuthorizationService authorizationService;
|
private final AuthorizationService authorizationService;
|
||||||
private final SignalService signalService;
|
private final SignalService signalService;
|
||||||
private final SignalRestService signalRestService;
|
private final SignalRestService signalRestService;
|
||||||
private final IXmlProcessor xmlProcessor;
|
private final StaxXmlProcessor staxXmlProcessor;
|
||||||
|
|
||||||
private final ExecutorService executor;
|
private final ExecutorService executor;
|
||||||
|
|
||||||
|
|
@ -61,13 +62,13 @@ public class SignalFacade {
|
||||||
AuthorizationService authorizationService,
|
AuthorizationService authorizationService,
|
||||||
SignalService signalService,
|
SignalService signalService,
|
||||||
SignalRestService signalRestService,
|
SignalRestService signalRestService,
|
||||||
IXmlProcessor xmlProcessor,
|
StaxXmlProcessor staxXmlProcessor,
|
||||||
@Qualifier("executorService") ExecutorService executor) {
|
@Qualifier("executorService") ExecutorService executor) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.authorizationService = authorizationService;
|
this.authorizationService = authorizationService;
|
||||||
this.signalService = signalService;
|
this.signalService = signalService;
|
||||||
this.signalRestService = signalRestService;
|
this.signalRestService = signalRestService;
|
||||||
this.xmlProcessor = xmlProcessor;
|
this.staxXmlProcessor = staxXmlProcessor;
|
||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -95,8 +96,11 @@ public class SignalFacade {
|
||||||
zipInputStream = new ZipInputStream(file.getInputStream());
|
zipInputStream = new ZipInputStream(file.getInputStream());
|
||||||
zipInputStream.getNextEntry();
|
zipInputStream.getNextEntry();
|
||||||
|
|
||||||
monitoringRequest = xmlProcessor.read(zipInputStream, MonitoringRequest.class);
|
monitoringRequest = staxXmlProcessor.read(zipInputStream.readAllBytes(), MonitoringRequest.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
if (e.getCause() instanceof XMLStreamException) {
|
||||||
|
throw clientExcp(SignalError.ERROR_011, XmlErrorFormat.MonitoringError);
|
||||||
|
}
|
||||||
if (e instanceof SignalClientException) {
|
if (e instanceof SignalClientException) {
|
||||||
throw (SignalClientException) e;
|
throw (SignalClientException) e;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package ru.nbch.credit_tracker.service.xml.stax;
|
||||||
|
|
||||||
|
import ru.nbch.credit_tracker.entities.request.monitoring_request.MonitoringRequest;
|
||||||
|
|
||||||
|
import javax.xml.stream.XMLStreamReader;
|
||||||
|
|
||||||
|
public class MonitoringRequestStaxReader extends BaseStaxReader<MonitoringRequest> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<MonitoringRequest> type() {
|
||||||
|
return MonitoringRequest.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MonitoringRequest createObject() {
|
||||||
|
return new MonitoringRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean innerProcess(XMLStreamReader reader, MonitoringRequest obj) {
|
||||||
|
String text = buffer.toString();
|
||||||
|
String currPath = getPath();
|
||||||
|
switch (currPath) {
|
||||||
|
case "/MonitoringRequest/UserId" ->
|
||||||
|
obj.setUserId(text);
|
||||||
|
case "/MonitoringRequest/Password" ->
|
||||||
|
obj.setPassword(text);
|
||||||
|
case "/MonitoringRequest/PackageName" ->
|
||||||
|
obj.setPackageName(text);
|
||||||
|
case "/MonitoringRequest/Subjects/Subject" ->
|
||||||
|
obj.finalizeSubject();
|
||||||
|
case "/MonitoringRequest/Subjects/Subject/Id" ->
|
||||||
|
obj.getCurrentSubject().setId(text);
|
||||||
|
case "/MonitoringRequest/Subjects/Subject/ClientId" ->
|
||||||
|
obj.getCurrentSubject().setClientId(text);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
87
src/test/java/ru/nbch/credit_tracker/stax/StaxTest.java
Normal file
87
src/test/java/ru/nbch/credit_tracker/stax/StaxTest.java
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
package ru.nbch.credit_tracker.stax;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
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.entities.request.monitoring_request.Subject;
|
||||||
|
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.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = {StaxTest.TestConfig.class})
|
||||||
|
public class StaxTest {
|
||||||
|
|
||||||
|
private final StaxXmlProcessor staxXmlProcessor;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public StaxTest(StaxXmlProcessor staxXmlProcessor) {
|
||||||
|
this.staxXmlProcessor = staxXmlProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public static class TestConfig {
|
||||||
|
@Bean
|
||||||
|
public StaxXmlProcessor xmlProcessor() {
|
||||||
|
List<StaxReader<?>> staxReader = new ArrayList<>();
|
||||||
|
staxReader.add(new MonitoringRequestStaxReader());
|
||||||
|
return new XmlProcessorStaxImpl(staxReader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidRequest() throws IOException {
|
||||||
|
MonitoringRequest monitoringRequest = staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/valid_request.xml").readAllBytes(), MonitoringRequest.class);
|
||||||
|
Assertions.assertNotNull(monitoringRequest);
|
||||||
|
Assertions.assertEquals("0001XX000001", monitoringRequest.getUserId());
|
||||||
|
Assertions.assertEquals("test", monitoringRequest.getPassword());
|
||||||
|
Assertions.assertEquals("MY_BATCH_170", monitoringRequest.getPackageName());
|
||||||
|
Subject firstSubject = monitoringRequest.getSubjects().getSubject().get(0);
|
||||||
|
Assertions.assertEquals("abc123", firstSubject.getId());
|
||||||
|
Assertions.assertEquals("273104246", firstSubject.getClientId());
|
||||||
|
Subject secondSubject = monitoringRequest.getSubjects().getSubject().get(1);
|
||||||
|
Assertions.assertEquals("abc124", secondSubject.getId());
|
||||||
|
Assertions.assertEquals("272354001", secondSubject.getClientId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testInvalidXmlRequest() {
|
||||||
|
Exception ex = null;
|
||||||
|
try {
|
||||||
|
staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/invalid_xml_request.xml").readAllBytes(), MonitoringRequest.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
ex = e;
|
||||||
|
}
|
||||||
|
Assertions.assertNotNull(ex);
|
||||||
|
Assertions.assertEquals(XMLStreamException.class, ex.getCause().getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEmptyXmlRequest() {
|
||||||
|
Exception ex = null;
|
||||||
|
try {
|
||||||
|
staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/empty_request.xml").readAllBytes(), MonitoringRequest.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
ex = e;
|
||||||
|
}
|
||||||
|
Assertions.assertNotNull(ex);
|
||||||
|
Assertions.assertEquals(XMLStreamException.class, ex.getCause().getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNonEpectedTag() throws IOException {
|
||||||
|
MonitoringRequest monitoringRequest = staxXmlProcessor.read(getClass().getResourceAsStream("/monitoring_request/xml/non_expected_tag_request.xml").readAllBytes(), MonitoringRequest.class);
|
||||||
|
Assertions.assertNull(monitoringRequest.getSubjects().getSubject().get(0).getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?xml version="1.0" encoding="windows-1251"?>
|
||||||
|
<MonitoringRequest>
|
||||||
|
<UserId>0001XX000001</UserId>
|
||||||
|
<Password>test</Password>
|
||||||
|
<PackageName>MY_BATCH_170</PackageName>
|
||||||
|
<Subjects>
|
||||||
|
<Subject>
|
||||||
|
<Id>abc123</Id>
|
||||||
|
<ClientId>273104246</ClientId>
|
||||||
|
</Subject>
|
||||||
|
<Subject>
|
||||||
|
<Id>abc124</Id>
|
||||||
|
</Subjects>
|
||||||
|
</MonitoringRequest>
|
||||||
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="windows-1251"?>
|
||||||
|
<MonitoringRequest>
|
||||||
|
<UserId>0001XX000001</UserId>
|
||||||
|
<Password>test</Password>
|
||||||
|
<Subjects>
|
||||||
|
<Subject>
|
||||||
|
<non_expected_tag>abc123</non_expected_tag>
|
||||||
|
<ClientId>273104246</ClientId>
|
||||||
|
</Subject>
|
||||||
|
</Subjects>
|
||||||
|
</MonitoringRequest>
|
||||||
|
|
||||||
17
src/test/resources/monitoring_request/xml/valid_request.xml
Normal file
17
src/test/resources/monitoring_request/xml/valid_request.xml
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="windows-1251"?>
|
||||||
|
<MonitoringRequest>
|
||||||
|
<UserId>0001XX000001</UserId>
|
||||||
|
<Password>test</Password>
|
||||||
|
<PackageName>MY_BATCH_170</PackageName>
|
||||||
|
<Subjects>
|
||||||
|
<Subject>
|
||||||
|
<Id>abc123</Id>
|
||||||
|
<ClientId>273104246</ClientId>
|
||||||
|
</Subject>
|
||||||
|
<Subject>
|
||||||
|
<Id>abc124</Id>
|
||||||
|
<ClientId>272354001</ClientId>
|
||||||
|
</Subject>
|
||||||
|
</Subjects>
|
||||||
|
</MonitoringRequest>
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue