Skip to content

Commit

Permalink
Add Unit Test configuration and create initial tests (#6)
Browse files Browse the repository at this point in the history
* 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
joariasl authored Jun 11, 2024
1 parent 16dcf2a commit 5933ae3
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,23 @@
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.annotation.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jakarta.xml.bind-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-plugins-runtime</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/DTETest.java
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())
);
}
}
64 changes: 64 additions & 0 deletions src/test/java/EnvioDTETest.java
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);
}
}

0 comments on commit 5933ae3

Please sign in to comment.