From bd2ad04a6574bf468c4f6e4972d86c891004300d Mon Sep 17 00:00:00 2001 From: Mikhail Trofimov Date: Wed, 2 Jul 2025 13:09:33 +0300 Subject: [PATCH] Added tests for entry extracting and monitoring request validation --- .../nbch/credit_tracker/utils/UtilTest.java | 156 ++++++++++++++++++ .../monitoring_request/directory_request.zip | Bin 0 -> 154 bytes .../monitoring_request/empty_request.zip | 0 .../monitoring_request/invalid_id_request.zip | Bin 0 -> 415 bytes .../invalid_phone_request.zip | Bin 0 -> 422 bytes .../monitoring_request/invalid_xml.zip | Bin 0 -> 312 bytes .../monitoring_request/many_files_request.zip | Bin 0 -> 1134 bytes .../no_required_tag_request.zip | Bin 0 -> 376 bytes .../no_unique_ids_request.zip | Bin 0 -> 417 bytes .../monitoring_request/txt_request.zip | Bin 0 -> 156 bytes .../monitoring_request/valid_request.zip | Bin 0 -> 390 bytes 11 files changed, 156 insertions(+) create mode 100644 src/test/java/ru/nbch/credit_tracker/utils/UtilTest.java create mode 100644 src/test/resources/monitoring_request/directory_request.zip create mode 100644 src/test/resources/monitoring_request/empty_request.zip create mode 100644 src/test/resources/monitoring_request/invalid_id_request.zip create mode 100644 src/test/resources/monitoring_request/invalid_phone_request.zip create mode 100644 src/test/resources/monitoring_request/invalid_xml.zip create mode 100644 src/test/resources/monitoring_request/many_files_request.zip create mode 100644 src/test/resources/monitoring_request/no_required_tag_request.zip create mode 100644 src/test/resources/monitoring_request/no_unique_ids_request.zip create mode 100644 src/test/resources/monitoring_request/txt_request.zip create mode 100644 src/test/resources/monitoring_request/valid_request.zip diff --git a/src/test/java/ru/nbch/credit_tracker/utils/UtilTest.java b/src/test/java/ru/nbch/credit_tracker/utils/UtilTest.java new file mode 100644 index 0000000..c7dc696 --- /dev/null +++ b/src/test/java/ru/nbch/credit_tracker/utils/UtilTest.java @@ -0,0 +1,156 @@ +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; +import org.junit.jupiter.params.provider.MethodSource; +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.enums.SignalError; +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 java.io.IOException; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.Optional; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@SpringBootTest(classes = {UtilTest.TestConfig.class}) +public class UtilTest { + + private final IXmlProcessor xmlProcessor; + + @Autowired + public UtilTest(IXmlProcessor xmlProcessor) { + this.xmlProcessor = xmlProcessor; + } + + @Configuration + public static class TestConfig { + @Bean + public IXmlProcessor xmlProcessor() throws JAXBException { + return new XmlProcessorImplAnyClasses(Charset.forName("windows-1251"), + MonitoringRequest.class); + } + } + + private static Stream entryTestCases() { + return Stream.of( + Arguments.of( + "/monitoring_request/valid_request.zip", + true + ), + Arguments.of( + "/monitoring_request/directory_request.zip", + false + ) + , + Arguments.of( + "/monitoring_request/empty_request.zip", + false + ) + , + Arguments.of( + "/monitoring_request/many_files_request.zip", + false + ) + , + Arguments.of( + "/monitoring_request/txt_request.zip", + false + ) + ); + } + + @ParameterizedTest(name = "[{index}] PATH: [ {0} ] EXPECTED: [ {1} ]") + @MethodSource("entryTestCases") + void testExtractEntryFromZip(String path, boolean expected) { + ZipInputStream zipInputStream; + Optional xmlFileEntry; + try { + zipInputStream = new ZipInputStream(getClass().getResourceAsStream(path)); + xmlFileEntry = ZipUtils.extractValidEntry(zipInputStream); + Assertions.assertEquals(expected, xmlFileEntry.isPresent()); + zipInputStream.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static Stream validationTestCases() { + return Stream.of( + Arguments.of( + "/monitoring_request/valid_request.zip", + null, + null + ), + Arguments.of( + "/monitoring_request/invalid_xml.zip", + SignalError.ERROR_011, + Collections.emptyList().toArray(new Object[0]) + ) + , + Arguments.of( + "/monitoring_request/no_required_tag_request.zip", + SignalError.ERROR_002, + new Object[]{"Password"} + ), + Arguments.of( + "/monitoring_request/invalid_phone_request.zip", + SignalError.ERROR_003, + Collections.emptyList().toArray(new Object[0]) + ), + Arguments.of( + "/monitoring_request/invalid_id_request.zip", + SignalError.ERROR_010, + Collections.emptyList().toArray(new Object[0]) + ), + Arguments.of( + "/monitoring_request/no_unique_ids_request.zip", + SignalError.ERROR_004, + new Object[]{"abc125"} + ) + ); + } + + @ParameterizedTest(name = "[{index}] PATH: [ {0} ] SIGNALERROR: [ {1} ] ARGS [ {2} ]") + @MethodSource("validationTestCases") + void testMonitoringRequestValidation(String path, SignalError signalError, Object[] args) { + ZipInputStream zipInputStream = null; + MonitoringRequest monitoringRequest; + try { + zipInputStream = new ZipInputStream(getClass().getResourceAsStream(path)); + zipInputStream.getNextEntry(); + monitoringRequest = xmlProcessor.read(zipInputStream, MonitoringRequest.class); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + if (zipInputStream != null) { + try { + zipInputStream.close(); + } catch (IOException e) { + throw new RuntimeException(); + } + } + } + + SignalClientException 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()); + } + } +} diff --git a/src/test/resources/monitoring_request/directory_request.zip b/src/test/resources/monitoring_request/directory_request.zip new file mode 100644 index 0000000000000000000000000000000000000000..7ad23ba857b6fea2c7e0e355e5acf2faeba850c3 GIT binary patch literal 154 zcmWIWW@Zs#0D*~dkD|Z~D8U7!Q!Jfm_2z^`(3Scot h1__3s&ZW!rUf*VfDuPo1-mGjO4kHl80BH@d3;?cJ6W9O% literal 0 HcmV?d00001 diff --git a/src/test/resources/monitoring_request/empty_request.zip b/src/test/resources/monitoring_request/empty_request.zip new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/monitoring_request/invalid_id_request.zip b/src/test/resources/monitoring_request/invalid_id_request.zip new file mode 100644 index 0000000000000000000000000000000000000000..0de7ae4ebac795fd1eb48b1a8fe2ade2ef3ea4fd GIT binary patch literal 415 zcmWIWW@Zs#U|`^2=*)N&b-?SY$y*@Lh>3wg3`l3@l_ln6ro;nLQEFjnYH^8PMQ+aA ziPrvy4MbYgKWQ&AXqMkNTWROI1>8KY9ql2@w0zfTIcIm@Q2kSr=d?)2lilWq-Se3G zx|L7Qe-x;9e%iaayCwFr3S0D!OE>;1t?bo}eY;*_x=!RPkF|464~9zE9lp+4b0JV> zwK@M)=argEb2cB8b9eoB#;pF--u1DeuNt&PUZ%_!o&Qr#?z8fXufM;&`&L@IKeebY z)%jA`zBy&`0W9}B9L-i;-ElqjvdF>6{B?Y6IS$R~nv0vdXDaQzUg*I->)5m1O@)R> zpRN?CcxZg5+jE6XdywVynI4hf^k3x2R9)U$te$)1! literal 0 HcmV?d00001 diff --git a/src/test/resources/monitoring_request/invalid_phone_request.zip b/src/test/resources/monitoring_request/invalid_phone_request.zip new file mode 100644 index 0000000000000000000000000000000000000000..f32304894634f65193fa43caa088df52d62d060f GIT binary patch literal 422 zcmWIWW@Zs#U|`^2xSa7Q%Hc7G$vYs=jER9k5=dv}l_ln6ro8-*vu*oByZWmruF1)t@)* zh&Hx7x=Arpdw&0HevPL5BMQ=2v+ii$7wK8v^7QEPOUuSa%xk(qx$0uuh-4B&Qxw&9+vXt^kVq{Z$>72 z23*0U0t_e+Pymy70*wo*gpomlLAvtve~Z_*8KHXMRDd@t8;HXQgxNq^6|4dPJdd7P literal 0 HcmV?d00001 diff --git a/src/test/resources/monitoring_request/invalid_xml.zip b/src/test/resources/monitoring_request/invalid_xml.zip new file mode 100644 index 0000000000000000000000000000000000000000..25fc1766e7819b937f36b42df2951c1c5b15895f GIT binary patch literal 312 zcmWIWW@Zs#U|`^2&`Wz1Wqq-5)9u9^k>#Z+GW?Z*6C&`BiFjWd5p> zTeCcwzFTxo)m(EW&E8Whcgfm23>?iVT1xx2xK&5DeR7?9?o_4VQN#M;RWTlohHYP^ zt~1NT9@!?=yZUR^&-=7k==9ld9}`Vy=Qs9R?8<#Lb^Y_ZyS4VL ze%Qs!%9s9hHS?N7%0Dz3AK5iSOqrng*?& z=ALz<*zu^<#H`Do^nz4>CH>=kdL+BOPa;dzcMF`_tUEZXQXWs>^SS1uB~2p zwy5@3rrx|-u@yV&6)vb4BZCCPdCU5l=C5xvLKVTO S0B=?{5Qh;6V}LXpSOoxU^@e)@ literal 0 HcmV?d00001 diff --git a/src/test/resources/monitoring_request/no_unique_ids_request.zip b/src/test/resources/monitoring_request/no_unique_ids_request.zip new file mode 100644 index 0000000000000000000000000000000000000000..ee9204970b1392e35382fd18f4dcf21fff2a790a GIT binary patch literal 417 zcmWIWW@Zs#U|`^2u*rB7^{u~t=}REbn2CWw5=iIe$Cu`17M7;QXQmX#7o`GO#U*+b zxjAbmTKgY15NS>Sq`m0P1NWRDmFuDU>T3 zJ*W8Z&!uX+&szO!KP8=hoOAbLPo8ZBOE<1A43duCb?d&7UZl~*iL)%Tn^qgtwC~sb zv0$-9SiIb-=|RC>H*5NA`?_S#nAM-&yFNDbRfD$3%ar+|`+su9edc{}^ZTQhAB%tO zNhoSbb-ol6oh`?qIbCycQ+Ks%)q3WMeIdIccUxWSQ>^=j-R@ZV^0etzGnF|67223#Se0t_P% rPymy7!ix*4gpomlA-<+2+~W0ZMyMV*72wUv2I4RRVK$If0IL81cv6~v literal 0 HcmV?d00001 diff --git a/src/test/resources/monitoring_request/txt_request.zip b/src/test/resources/monitoring_request/txt_request.zip new file mode 100644 index 0000000000000000000000000000000000000000..b635e012ec8c0577403f5cda9c34160f958b9bd4 GIT binary patch literal 156 zcmWIWW@h1H0D;Q5M^Rt~l;8%^MX80Qsl_FFB^4zB-i%E447il4Ks6~qX(WwY3=ly^ h1__2<>kr*}uWvI#Rl%tMZ&o%ChY<*4fwU%A1^^OF6zc#0 literal 0 HcmV?d00001 diff --git a/src/test/resources/monitoring_request/valid_request.zip b/src/test/resources/monitoring_request/valid_request.zip new file mode 100644 index 0000000000000000000000000000000000000000..e1ac937be152015ee36765e2f09b281a87fb1d38 GIT binary patch literal 390 zcmWIWW@Zs#U|`^2cvg2WimCS||0^KRn2CXb8%P(W7M7+Km*`dG=FFYwn14t?qV4?a z^%+&=9|AYbQb_x}h<$o$apycA-6I<`k2Jlm`}ZPimG>>#Z+GW?Z*6C&`BiFjWd5p> zTeCcwzFTxo)m(EW&E8Whcgfm23>?iVT1xx2xK&5DeR7?9?o_4VQN#M;RWTlohHYP^ zt~1NT9@!?=yZUR^&-=7k==9ld9}`Vy=Qs9R?8<#Lb^Y_ZyS4VL ze%Qs!%9s9hH