Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorant Miglecz committed Oct 9, 2017
0 parents commit 054f6ad
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.epam.test</groupId>
<artifactId>adapter</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
21 changes: 21 additions & 0 deletions src/main/java/com/epam/test/adapter/Adapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.epam.test.adapter;

import java.util.stream.Stream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* Do not modify!
*/
public interface Adapter {
Stream<Element> getElementsByTagName(Document document, String tagName);

Stream<Element> getElementsByTagName(Element element, String tagName);

Stream<Element> getChildElementsByTagName(Node node, String tagName);

Stream<String> xpathValues(Node node, String xpathSelector);

Stream<Node> xpathNodes(Node node, String expression);
}
41 changes: 41 additions & 0 deletions src/main/java/com/epam/test/adapter/AdapterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.epam.test.adapter;

import java.util.stream.Stream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* Implement methods body!
*/
public class AdapterImpl implements Adapter {
@Override
public Stream<Element> getElementsByTagName(Document document, String tagName) {
//TODO implement this
throw new UnsupportedOperationException("Method not implemented yet.");
}

@Override
public Stream<Element> getElementsByTagName(Element element, String tagName) {
//TODO implement this
throw new UnsupportedOperationException("Method not implemented yet.");
}

@Override
public Stream<Element> getChildElementsByTagName(Node node, String tagName) {
//TODO implement this
throw new UnsupportedOperationException("Method not implemented yet.");
}

@Override
public Stream<String> xpathValues(Node node, String xpathSelector) {
//TODO implement this
throw new UnsupportedOperationException("Method not implemented yet.");
}

@Override
public Stream<Node> xpathNodes(Node node, String expression) {
//TODO implement this
throw new UnsupportedOperationException("Method not implemented yet.");
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/epam/test/adapter/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.epam.test.adapter;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.file.Path;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
* Do not modify!
*/
public class Helper {
public static Document parseXml(Path path) {
try {
final InputSource inputSource = new InputSource(new InputStreamReader(new FileInputStream(path.toFile()), "UTF-8"));
inputSource.setEncoding("UTF-8");
final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
builder.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader("")));
return builder.parse(inputSource);
} catch (Exception e) {
throw new RuntimeException("xml error of file='" + path.toAbsolutePath() + "'", e);
}
}
}
81 changes: 81 additions & 0 deletions src/test/java/com/epam/test/adapter/AdapterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.epam.test.adapter;

import static com.epam.test.adapter.Helper.parseXml;
import static java.nio.file.Paths.get;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.of;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import java.util.List;
import org.testng.annotations.Test;
import org.w3c.dom.Document;

/**
* Do not modify!
*/
public class AdapterTest {
private final Adapter adapter = new AdapterImpl();
private final Document document = parseXml(get("src/test/resources/10DT0000029352.xml"));

@Test
public void testAdapterGetElementByTagNameShouldReturnThreeIdentifiers() {
// Given
// When
final List<String> result = of(document)
.flatMap(d -> adapter.getElementsByTagName(d, "cdc-dottrina"))
.flatMap(e -> adapter.getElementsByTagName(e, "extra-item"))
.map(e -> e.getAttribute("chiave"))
.collect(toList());
// Then
assertThat(result, hasItem("10DT0000029352SOMM"));
assertThat(result, hasItem("10DT0000029352ART6"));
assertThat(result, hasItem("10DT0000029352ART40"));
assertThat(result.toString(), result, hasSize(3));
}

@Test
public void testAdapterGetChildElementByTagNameShouldReturnThreeIdentifiers() {
// Given
// When
final List<String> result = of(document)
.flatMap(d -> adapter.getChildElementsByTagName(d, "cdc-dottrina"))
.flatMap(e -> adapter.getChildElementsByTagName(e, "extra-info"))
.flatMap(e -> adapter.getChildElementsByTagName(e, "extra-item"))
.map(e -> e.getAttribute("chiave"))
.collect(toList());
// Then
assertThat(result, hasItem("10DT0000029352SOMM"));
assertThat(result, hasItem("10DT0000029352ART6"));
assertThat(result, hasItem("10DT0000029352ART40"));
assertThat(result.toString(), result, hasSize(3));
}

@Test
public void testAdapterGetXpathShouldReturnThreeIdentifiers() {
// Given
// When
final List<String> result = of(document)
.flatMap(d -> adapter.xpathNodes(d, "/cdc-dottrina"))
.flatMap(e -> adapter.xpathNodes(e, "//extra-item"))
.flatMap(e -> adapter.xpathValues(e, "@chiave"))
.collect(toList());
// Then
assertThat(result, hasItem("10DT0000029352SOMM"));
assertThat(result, hasItem("10DT0000029352ART6"));
assertThat(result, hasItem("10DT0000029352ART40"));
assertThat(result.toString(), result, hasSize(3));
}

@Test
public void testAdapterGetXpathShouldReturnOneText() {
// Given
// When
final List<String> result = of(document)
.flatMap(d -> adapter.xpathValues(d, "//extra-item[@id-ga='i1']/extra-classificazione/extra-classvoce[2]/extra-classinfo/text()"))
.collect(toList());
// Then
assertThat(result, hasItem("Iva e Dogane"));
assertThat(result.toString(), result, hasSize(1));
}
}
73 changes: 73 additions & 0 deletions src/test/resources/10DT0000029352.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cdc-dottrina SYSTEM "cdc-dottrina.dtd">
<cdc-dottrina>
<extra-info>
<extra-common flag-storico="n" chiave="10DT0000029352" omenid="9464135" flag-anteprima="n">
<extra-sottotipo id-ga="i1" label="Inquadramento"/>
<extra-operasez>
<extra-atomo id-ga="i96" codice="GACF" contesto="fiscale"/>
<extra-atomo id-ga="i141" codice="IYCF" contesto="fiscale"/>
<extra-atomo id-ga="i95" codice="GAAT" contesto="fiscale"/>
</extra-operasez>
</extra-common>
<extra-item id-ga="i1" chiave="10DT0000029352SOMM" tipo-elem="1">
<extra-classificazione>
<extra-classvoce contesto="fiscale">
<extra-classinfo classid="GACF_4_xDOC" id-ga="iGACF_4_xDOC_15" codice="15" peso="0" atomo="GACF" livello="1">MOSS</extra-classinfo>
<extra-classinfo classid="GACF_4_xDOC" id-ga="iGACF_4_xDOC_15.1" codice="15.1" peso="10" atomo="GACF" livello="2">MOSS Mini one stop shop</extra-classinfo>
</extra-classvoce>
<extra-classvoce contesto="fiscale">
<extra-classinfo classid="GAAT_4_xDOC" id-ga="iGAAT_4_xDOC_7" codice="7" peso="0" atomo="GAAT" livello="1">Iva e Dogane</extra-classinfo>
</extra-classvoce>
</extra-classificazione>
<extra-riferimenti>
<extra-estremo tipo="provn">
<extra-descrizione id-ga="10LX0000812396ART14" contesto="unico">DLgs. 42/2015 artt. 2</extra-descrizione>
</extra-estremo>
<extra-estremo tipo="provn">
<extra-descrizione id-ga="10LX0000813102ART0" contesto="unico">DM 20.4.2015</extra-descrizione>
</extra-estremo>
<extra-estremo tipo="provn">
<extra-descrizione id-ga="10LX0000812396ART21" contesto="unico">DLgs. 42/2015 9</extra-descrizione>
</extra-estremo>
<extra-estremo tipo="provn">
<extra-descrizione id-ga="10LX0000109684ART36738454" contesto="unico">DPR 633/72 art. 74-octies</extra-descrizione>
</extra-estremo>
<extra-estremo tipo="provn">
<extra-descrizione id-ga="10LX0000833610ART0" contesto="unico">Provv. Agenzia delle Entrate 26.7.2016 n. 118987</extra-descrizione>
</extra-estremo>
<extra-estremo tipo="provn">
<extra-descrizione id-ga="10LX0000749411ART0" contesto="unico">Direttiva 2006/112/C art. 358</extra-descrizione>
<extra-descrizione id-ga="10LX0000749411ART0" contesto="unico">Direttiva 2006/112/C art. 369-duodecies</extra-descrizione>
<extra-descrizione id-ga="10LX0000749411ART0" contesto="unico">Reg. UE 282/2011 artt. 58</extra-descrizione>
<extra-descrizione id-ga="10LX0000749411ART0" contesto="unico">Reg. UE 282/2011 art 63</extra-descrizione>
</extra-estremo>
<extra-estremo tipo="provn">
<extra-descrizione id-ga="10LX0000109684ART94" contesto="unico">artt. 74-quinquies</extra-descrizione>
</extra-estremo>
</extra-riferimenti>
</extra-item>
<extra-item id-prev="10DT0000029352ART40" id-ga="i5" chiave="10DT0000029352ART6" tipo-elem="5">
<extra-classificazione>
<extra-classvoce contesto="fiscale">
<extra-classinfo classid="GACF_4_xDOC" id-ga="iGACF_4_xDOC_15" codice="15" peso="0" atomo="GACF" livello="1">MOSS</extra-classinfo>
<extra-classinfo classid="GACF_4_xDOC" id-ga="iGACF_4_xDOC_15.1" codice="15.1" peso="10" atomo="GACF" livello="2">MOSS Mini one stop shop</extra-classinfo>
</extra-classvoce>
<extra-classvoce contesto="fiscale">
<extra-classinfo classid="GAAT_4_xDOC" id-ga="iGAAT_4_xDOC_7" codice="7" peso="0" atomo="GAAT" livello="1">Iva e Dogane</extra-classinfo>
</extra-classvoce>
</extra-classificazione>
</extra-item>
<extra-item id-ga="i5" chiave="10DT0000029352ART40" tipo-elem="5" id-next="10DT0000029352ART6">
<extra-classificazione>
<extra-classvoce contesto="fiscale">
<extra-classinfo classid="GACF_4_xDOC" id-ga="iGACF_4_xDOC_15" codice="15" peso="0" atomo="GACF" livello="1">MOSS</extra-classinfo>
<extra-classinfo classid="GACF_4_xDOC" id-ga="iGACF_4_xDOC_15.1" codice="15.1" peso="10" atomo="GACF" livello="2">MOSS Mini one stop shop</extra-classinfo>
</extra-classvoce>
<extra-classvoce contesto="fiscale">
<extra-classinfo classid="GAAT_4_xDOC" id-ga="iGAAT_4_xDOC_7" codice="7" peso="0" atomo="GAAT" livello="1">Iva e Dogane</extra-classinfo>
</extra-classvoce>
</extra-classificazione>
</extra-item>
</extra-info>
</cdc-dottrina>

0 comments on commit 054f6ad

Please sign in to comment.