|
| 1 | +package br.com.swconsultoria.nfe; |
| 2 | + |
| 3 | +import br.com.swconsultoria.nfe.dom.ConfiguracoesNfe; |
| 4 | +import br.com.swconsultoria.nfe.dom.Evento; |
| 5 | +import br.com.swconsultoria.nfe.dom.enuns.AmbienteEnum; |
| 6 | +import br.com.swconsultoria.nfe.dom.enuns.AssinaturaEnum; |
| 7 | +import br.com.swconsultoria.nfe.dom.enuns.DocumentoEnum; |
| 8 | +import br.com.swconsultoria.nfe.dom.enuns.EstadosEnum; |
| 9 | +import br.com.swconsultoria.nfe.exception.NfeException; |
| 10 | +import br.com.swconsultoria.nfe.schema.envEventoCancNFe.TEnvEvento; |
| 11 | +import br.com.swconsultoria.nfe.schema.envEventoCancNFe.TRetEnvEvento; |
| 12 | +import br.com.swconsultoria.nfe.util.CancelamentoUtil; |
| 13 | +import br.com.swconsultoria.nfe.util.StubUtil; |
| 14 | +import br.com.swconsultoria.nfe.wsdl.NFeRecepcaoEvento.NFeRecepcaoEvento4Stub; |
| 15 | +import mockit.Mock; |
| 16 | +import mockit.MockUp; |
| 17 | +import org.apache.axiom.om.util.AXIOMUtil; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | + |
| 24 | +class CancelarTest { |
| 25 | + |
| 26 | + private static final String RET_EVENTO_XML = |
| 27 | + "<retEnvEvento versao=\"1.00\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">" + |
| 28 | + "<idLote>1</idLote><tpAmb>2</tpAmb><verAplic>TESTE</verAplic>" + |
| 29 | + "<cOrgao>91</cOrgao><cStat>128</cStat>" + |
| 30 | + "<xMotivo>Lote de Evento Processado</xMotivo>" + |
| 31 | + "</retEnvEvento>"; |
| 32 | + |
| 33 | + private ConfiguracoesNfe config; |
| 34 | + private TEnvEvento enviEvento; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() throws NfeException { |
| 38 | + config = new ConfiguracoesNfe(); |
| 39 | + config.setEstado(EstadosEnum.SP); |
| 40 | + config.setAmbiente(AmbienteEnum.HOMOLOGACAO); |
| 41 | + config.setEncode("UTF-8"); |
| 42 | + |
| 43 | + config.setZoneId(java.time.ZoneId.of("America/Sao_Paulo")); |
| 44 | + |
| 45 | + Evento evento = new Evento(); |
| 46 | + evento.setChave("52230309158456000159550010000731791567812345"); |
| 47 | + evento.setCnpj("09158456000159"); |
| 48 | + evento.setProtocolo("352230000123456"); |
| 49 | + evento.setMotivo("Cancelamento por erro na emissao do documento fiscal"); |
| 50 | + evento.setDataEvento(java.time.LocalDateTime.of(2024, 1, 15, 10, 0, 0)); |
| 51 | + enviEvento = CancelamentoUtil.montaCancelamento(evento, config); |
| 52 | + } |
| 53 | + |
| 54 | + private void mockStubUtil() { |
| 55 | + new MockUp<StubUtil>() { |
| 56 | + @Mock |
| 57 | + public void configuraHttpClient(org.apache.axis2.client.Stub stub, |
| 58 | + ConfiguracoesNfe cfg, String url) { } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + private void mockAssinar() { |
| 63 | + new MockUp<Assinar>() { |
| 64 | + @Mock |
| 65 | + public String assinaNfe(ConfiguracoesNfe cfg, String xml, |
| 66 | + AssinaturaEnum tipo) throws NfeException { |
| 67 | + return xml; |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + private void mockEventosStub() { |
| 73 | + new MockUp<NFeRecepcaoEvento4Stub>() { |
| 74 | + @Mock |
| 75 | + public void $init(String endpoint) { } |
| 76 | + |
| 77 | + @Mock |
| 78 | + public NFeRecepcaoEvento4Stub.NfeResultMsg nfeRecepcaoEvento( |
| 79 | + NFeRecepcaoEvento4Stub.NfeDadosMsg data) throws Exception { |
| 80 | + NFeRecepcaoEvento4Stub.NfeResultMsg result = new NFeRecepcaoEvento4Stub.NfeResultMsg(); |
| 81 | + result.setExtraElement(AXIOMUtil.stringToOM(RET_EVENTO_XML)); |
| 82 | + return result; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void eventoCancelamento_semValidacao_retornaEvento() throws NfeException { |
| 89 | + mockStubUtil(); |
| 90 | + mockAssinar(); |
| 91 | + mockEventosStub(); |
| 92 | + |
| 93 | + TRetEnvEvento ret = Cancelar.eventoCancelamento(config, enviEvento, false, DocumentoEnum.NFE); |
| 94 | + |
| 95 | + assertNotNull(ret); |
| 96 | + assertEquals("128", ret.getCStat()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void eventoCancelamento_retornaLoteProcessado() throws NfeException { |
| 101 | + mockStubUtil(); |
| 102 | + mockAssinar(); |
| 103 | + mockEventosStub(); |
| 104 | + |
| 105 | + TRetEnvEvento ret = Cancelar.eventoCancelamento(config, enviEvento, false, DocumentoEnum.NFE); |
| 106 | + |
| 107 | + assertEquals("Lote de Evento Processado", ret.getXMotivo()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void eventoCancelamento_retornaAmbienteHomologacao() throws NfeException { |
| 112 | + mockStubUtil(); |
| 113 | + mockAssinar(); |
| 114 | + mockEventosStub(); |
| 115 | + |
| 116 | + TRetEnvEvento ret = Cancelar.eventoCancelamento(config, enviEvento, false, DocumentoEnum.NFE); |
| 117 | + |
| 118 | + assertEquals("2", ret.getTpAmb()); |
| 119 | + } |
| 120 | +} |
0 commit comments