ReceiverTask { public void readACK(byte[] targetArray, String ackName) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { LOGGER.debug("Received ack file : {}", ackName); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(targetArray); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); XPathFactory xpf = XPathFactory.newInstance(); XPath path = xpf.newXPath(); Document document = builder.parse(byteArrayInputStream); String fileName = path.evaluate("/PES_ACQUIT/Enveloppe/Parametres/NomFic/@V", document); LOGGER.debug("Extracted PES filename : {}", fileName); PesAller pesAller = pesService.getByFileName(fileName).orElseThrow(PesNotFoundException::new); boolean isAckOk = true; List<PesHistoryError> errors = new ArrayList<>(); NodeList nodes = (NodeList) path.evaluate("/PES_ACQUIT/ACQUIT/ElementACQUIT", document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); boolean isCurrentNodeValid = path.evaluate("EtatAck/@V", node).equals("1"); isAckOk = isAckOk && isCurrentNodeValid; if (isCurrentNodeValid) continue; NodeList innerNodes = (NodeList) path.evaluate("DetailPiece", node, XPathConstants.NODESET); if (innerNodes.getLength() != 0) { for (int j = 0; j < innerNodes.getLength(); j++) { Node in = innerNodes.item(j); NodeList detailLigneNode = (NodeList) path.evaluate("DetailLigne", in, XPathConstants.NODESET); if (detailLigneNode.getLength() != 0) { for (int k = 0; k < detailLigneNode.getLength(); k++) { Node detail = detailLigneNode.item(k); PesHistoryError e = new PesHistoryError( path.evaluate("Erreur/NumAnoAck/@V", detail), path.evaluate("Erreur/LibelleAnoAck/@V", detail), path.evaluate("NumPiece/@V", in) ); errors.add(e); } } else { PesHistoryError e = new PesHistoryError( path.evaluate("Erreur/NumAnoAck/@V", in), path.evaluate("Erreur/LibelleAnoAck/@V", in), path.evaluate("NumPiece/@V", in) ); errors.add(e); } } } else { PesHistoryError e = new PesHistoryError( path.evaluate("Erreur/NumAnoAck/@V", node), path.evaluate("Erreur/LibelleAnoAck/@V", node), path.evaluate("IdUnique/@V", node) ); errors.add(e); } } if (isAckOk) { pesService.updateStatus(pesAller.getUuid(), StatusType.ACK_RECEIVED, targetArray, ackName); } else { pesService.updateStatus(pesAller.getUuid(), StatusType.NACK_RECEIVED, targetArray, ackName, errors); } }  @Scheduled(fixedDelay = 60000) void receive(); void readACK(byte[] targetArray, String ackName); void readPesRetour(byte[] targetArray, String pesRetourName);  }
@Test public void testAckKoWithInnerDetailLigne() throws Exception { PesAller pesAller = gimmePesAller(); given(pesAllerService.getByFileName("PesDepense-02-2019-5-11112222333344445")) .willReturn(Optional.of(pesAller)); doNothing().when(pesAllerService).updateStatus(anyString(), any(), any(), anyString(), anyList()); InputStream pesFile = new ClassPathResource("data/pes-ack-ko/006111_190130123456-ACK-A3020100_A0000IDL_KO.xml") .getInputStream(); receiverTask.readACK(IOUtils.toByteArray(pesFile), "ackName"); verify(pesAllerService).updateStatus(eq(pesAller.getUuid()), eq(StatusType.NACK_RECEIVED), any(), eq("ackName"), argThat(errors -> errors.size() == 6 && errors.stream().allMatch(pesHistoryError -> pesHistoryError.getTitle().equals("ERREUR_AUTRE")) && errors.stream().filter(pesHistoryError -> pesHistoryError.getMessage().startsWith("1971")).count() == 5 && errors.stream().noneMatch(pesHistoryError -> pesHistoryError.getSource().isEmpty()))); }
@Test public void testAckKoWithDetailPiece() throws Exception { PesAller pesAller = gimmePesAller(); given(pesAllerService.getByFileName("PesRecette-02-2018-258-258-20190129666666")) .willReturn(Optional.of(pesAller)); doNothing().when(pesAllerService).updateStatus(anyString(), any(), any(), anyString(), anyList()); InputStream pesFile = new ClassPathResource("data/pes-ack-ko/006111_190130123456-ACK-A3020100_A0000SDP_KO.xml") .getInputStream(); receiverTask.readACK(IOUtils.toByteArray(pesFile), "ackName"); verify(pesAllerService).updateStatus(eq(pesAller.getUuid()), eq(StatusType.NACK_RECEIVED), any(), eq("ackName"), argThat(errors -> errors.size() == 2 && errors.stream().allMatch(pesHistoryError -> pesHistoryError.getTitle().equals("ERREUR_AUTRE")) && errors.stream().filter(pesHistoryError -> pesHistoryError.getMessage().startsWith("2559")).count() == 2 && errors.stream().noneMatch(pesHistoryError -> pesHistoryError.getSource().isEmpty()))); }
@Test public void testAckKoWithSimpleErreur() throws Exception { PesAller pesAller = gimmePesAller(); given(pesAllerService.getByFileName("PesDepense-12-2018-209-2092019012917666666")) .willReturn(Optional.of(pesAller)); doNothing().when(pesAllerService).updateStatus(anyString(), any(), any(), anyString(), anyList()); InputStream pesFile = new ClassPathResource("data/pes-ack-ko/006111_190130123456-ACK-A3020100_A0000SEN_KO.xml") .getInputStream(); receiverTask.readACK(IOUtils.toByteArray(pesFile), "ackName"); verify(pesAllerService).updateStatus(eq(pesAller.getUuid()), eq(StatusType.NACK_RECEIVED), any(), eq("ackName"), argThat(errors -> errors.size() == 1 && errors.stream().allMatch(pesHistoryError -> pesHistoryError.getTitle().equals("ERREUR_DOUBLON_PJ") && pesHistoryError.getSource().equals("TI00064567") && pesHistoryError.getMessage().startsWith("1984")))); }
@Test public void testAckKoWithAMixOfErreurPatterns() throws Exception { PesAller pesAller = gimmePesAller(); given(pesAllerService.getByFileName("PesDepense-02-2019-5-11112222333355556")) .willReturn(Optional.of(pesAller)); doNothing().when(pesAllerService).updateStatus(anyString(), any(), any(), anyString(), anyList()); InputStream pesFile = new ClassPathResource("data/pes-ack-ko/006111_190130123456-ACK-A3020100_A0000MIX_KO.xml") .getInputStream(); receiverTask.readACK(IOUtils.toByteArray(pesFile), "ackName"); verify(pesAllerService).updateStatus(eq(pesAller.getUuid()), eq(StatusType.NACK_RECEIVED), any(), eq("ackName"), argThat(errors -> errors.size() == 6 && errors.stream().noneMatch(pesHistoryError -> pesHistoryError.getTitle().isEmpty()) && errors.stream().noneMatch(pesHistoryError -> pesHistoryError.getMessage().isEmpty()) && errors.stream().noneMatch(pesHistoryError -> pesHistoryError.getSource().isEmpty()))); }