-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Unit Test configuration and create initial tests (#6)
* Add JUnit * Add jaxb-impl to run in test scope * Add Datafaker to fake data in test scope * Create initial JUnit tests for DTE and EnvioDTE
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import cl.sii.siidte.DTEDefType; | ||
import cl.sii.siidte.ObjectFactory; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class DTETest { | ||
private final ObjectFactory objectFactory = new ObjectFactory(); | ||
private final DTEDefType dteDefType = objectFactory.createDTEDefType(); | ||
private final DTEDefType.Documento documento = objectFactory.createDTEDefTypeDocumento(); | ||
private final DTEDefType.Documento.Encabezado encabezado = objectFactory.createDTEDefTypeDocumentoEncabezado(); | ||
private final DTEDefType.Documento.Encabezado.IdDoc idDoc = objectFactory.createDTEDefTypeDocumentoEncabezadoIdDoc(); | ||
|
||
@Test | ||
void setDocumento() { | ||
dteDefType.setDocumento(documento); | ||
assertAll("documento", | ||
() -> assertNotNull(dteDefType.getDocumento()), | ||
() -> assertInstanceOf(DTEDefType.Documento.class, dteDefType.getDocumento()) | ||
); | ||
} | ||
|
||
@Test | ||
void setEncabezado() { | ||
documento.setEncabezado(encabezado); | ||
assertAll("encabezado", | ||
() -> assertNotNull(documento.getEncabezado()), | ||
() -> assertInstanceOf(DTEDefType.Documento.Encabezado.class, documento.getEncabezado()) | ||
); | ||
} | ||
|
||
@Test | ||
void setIdDoc() { | ||
encabezado.setIdDoc(idDoc); | ||
assertAll("idDoc", | ||
() -> assertNotNull(encabezado.getIdDoc()), | ||
() -> assertInstanceOf(DTEDefType.Documento.Encabezado.IdDoc.class, encabezado.getIdDoc()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import cl.sii.siidte.DTEDefType; | ||
import cl.sii.siidte.EnvioDTE; | ||
import cl.sii.siidte.MedioPagoType; | ||
import cl.sii.siidte.ObjectFactory; | ||
import jakarta.xml.bind.JAXBContext; | ||
import jakarta.xml.bind.JAXBException; | ||
import jakarta.xml.bind.Marshaller; | ||
import jakarta.xml.bind.Unmarshaller; | ||
import net.datafaker.Faker; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.math.BigInteger; | ||
import java.util.Locale; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class EnvioDTETest { | ||
private final ObjectFactory objectFactory = new ObjectFactory(); | ||
private final EnvioDTE envioDTE = objectFactory.createEnvioDTE(); | ||
private final Faker faker = new Faker(); | ||
|
||
@Test | ||
void createEnvioDTE() throws JAXBException { | ||
DTEDefType dteDefType = objectFactory.createDTEDefType(); | ||
dteDefType.setDocumento(objectFactory.createDTEDefTypeDocumento()); | ||
DTEDefType.Documento documento = dteDefType.getDocumento(); | ||
documento.setEncabezado(objectFactory.createDTEDefTypeDocumentoEncabezado()); | ||
DTEDefType.Documento.Encabezado encabezado = documento.getEncabezado(); | ||
encabezado.setIdDoc(objectFactory.createDTEDefTypeDocumentoEncabezadoIdDoc()); | ||
DTEDefType.Documento.Encabezado.IdDoc idDoc = encabezado.getIdDoc(); | ||
|
||
idDoc.setMedioPago(MedioPagoType.CH); | ||
idDoc.setFmaPago(new BigInteger("1")); | ||
|
||
DTEDefType.Documento.Detalle item1 = objectFactory.createDTEDefTypeDocumentoDetalle(); | ||
item1.setNroLinDet(0); | ||
DTEDefType.Documento.Detalle.CdgItem cdg1 = objectFactory.createDTEDefTypeDocumentoDetalleCdgItem(); | ||
cdg1.setTpoCodigo("EAN"); | ||
cdg1.setVlrCodigo(faker.code().ean13()); | ||
item1.getCdgItem().add(cdg1); | ||
item1.setNmbItem(faker.commerce().productName()); | ||
|
||
documento.getDetalle().add(item1); | ||
|
||
envioDTE.setSetDTE(objectFactory.createEnvioDTESetDTE()); | ||
EnvioDTE.SetDTE setDTE = envioDTE.getSetDTE(); | ||
|
||
// Add Document to SetDTE | ||
setDTE.getDTE().add(dteDefType); | ||
|
||
StringWriter writer = new StringWriter(); | ||
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class); | ||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); | ||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML | ||
jaxbMarshaller.marshal(envioDTE, writer); | ||
|
||
StringReader reader = new StringReader(writer.toString()); | ||
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); | ||
jaxbUnmarshaller.setEventHandler(new jakarta.xml.bind.helpers.DefaultValidationEventHandler()); | ||
jaxbUnmarshaller.unmarshal(reader); | ||
} | ||
} |