Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
added raml-io & raml-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
skiddykong committed Mar 8, 2016
1 parent b1d35d8 commit 0ca5238
Show file tree
Hide file tree
Showing 19 changed files with 404 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<modules>
<module>raml-maven-plugin</module>
<module>raml-generator-core</module>
<module>raml-io</module>
<module>raml-parser</module>
</modules>

<dependencyManagement>
Expand Down
39 changes: 39 additions & 0 deletions raml-io/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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">
<parent>
<artifactId>raml-jaxrs</artifactId>
<groupId>uk.gov.justice</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>raml-io</artifactId>


<dependencies>
<dependency>
<groupId>org.raml</groupId>
<artifactId>raml-parser</artifactId>
<version>0.8.12</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>net.trajano.commons</groupId>
<artifactId>commons-testing</artifactId>
</dependency>
</dependencies>

</project>
43 changes: 43 additions & 0 deletions raml-io/src/main/java/uk/gov/justice/raml/io/FileTreeResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package uk.gov.justice.raml.io;

import com.google.common.io.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
*
* Created by jcooke on 01/02/16.
*
*/
public class FileTreeResolver {

private static final Logger LOGGER = LoggerFactory.getLogger(FileTreeResolver.class);

/**
*
* returns a list of Paths to files found under searchDirectory where the file name matches the supplied
* regex pattern.
*
* @param searchDirectory
* @param pattern
* @return
*/
public List<Path> load(File searchDirectory, Pattern pattern) {

LOGGER.debug("Loading files from : " + searchDirectory.getAbsolutePath());

return Files.fileTreeTraverser()
.breadthFirstTraversal(searchDirectory)
.toList()
.stream()
.filter(s -> pattern.matcher(s.getPath()).matches())
.map(File::toPath)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package uk.gov.justice.raml.io;

import java.util.regex.Pattern;

/**
* Created by jcooke on 27/01/16.
*/
public final class TierImportPattern {

public static final Pattern ALL = Pattern.compile(".*\\.raml");

private TierImportPattern(){

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.gov.justice.raml.io;

import org.junit.Test;

import java.nio.file.Path;
import java.util.List;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* Created by jcooke on 05/02/16.
*/
public class FileTreeResolverTest {


@Test
public void testValidateSyntax() throws Exception {

FileTreeResolver fileTreeResolver = new FileTreeResolver();

List<Path> results = fileTreeResolver.load(TestRaml.getTestRamlDirectory(), TierImportPattern.ALL);

results.stream()
.forEach(path -> System.out.println(path));

assertNotNull(results.get(0));

assertTrue(results.size() == 2);
}

}
31 changes: 31 additions & 0 deletions raml-io/src/test/java/uk/gov/justice/raml/io/TestRaml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.justice.raml.io;

import java.io.File;

/**
* Created by jcooke on 01/02/16.
*/
public class TestRaml {

private TestRaml() {}

public static File getTestRamlDirectory() {

return new File("src/test/resources/raml/");

}


public static File getTestNoRamlDirectory() {

return new File("src/test/resources/noraml/");

}


public static File getTestInvalidRamlDirectory() {

return new File("src/test/resources/invalidraml/");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.justice.raml.io;

import org.junit.Test;

import static net.trajano.commons.testing.UtilityClassTestUtil.assertUtilityClassWellDefined;

/**
* Created by jcooke on 15/02/16.
*/
public class TierImportPatternTest {

/**
* Tests the private constructor & that class is well defined.
* http://www.trajano.net/2013/04/covering-utility-classes/
* https://github.com/trajano/commons-testing
*/
@Test
public void isUtilityClassWellDefined() {
assertUtilityClassWellDefined(TierImportPattern.class);
}

}
9 changes: 9 additions & 0 deletions raml-io/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
15 changes: 15 additions & 0 deletions raml-io/src/test/resources/raml/example-1.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#%RAML 0.8
title: Example
baseUri: http://localhost:8080/
version: v0.1
mediaType: application/json
protocols: [ HTTP, HTTPS ]

/test:
description: |
Post to this entry point to create a test
post:
headers:
Content-Type:
type: string
example: application/json
15 changes: 15 additions & 0 deletions raml-io/src/test/resources/raml/example-2.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#%RAML 0.8
title: Example
baseUri: http://localhost:8080/
version: v0.1
mediaType: application/json
protocols: [ HTTP, HTTPS ]

/test:
description: |
Post to this entry point to create a test
post:
headers:
Content-Type:
type: string
example: application/json
1 change: 1 addition & 0 deletions raml-io/src/test/resources/raml/notRaml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A test file which is not raml.
33 changes: 33 additions & 0 deletions raml-parser/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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>

<parent>
<artifactId>raml-jaxrs</artifactId>
<groupId>uk.gov.justice</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>raml-parser</artifactId>


<dependencies>

<dependency>
<groupId>org.raml</groupId>
<artifactId>raml-parser</artifactId>
<version>0.8.12</version>
</dependency>

<dependency>
<artifactId>raml-io</artifactId>
<groupId>uk.gov.justice</groupId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.gov.justice.raml.io.files.parser;

import org.raml.model.Raml;
import org.raml.parser.loader.FileResourceLoader;
import org.raml.parser.visitor.RamlDocumentBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

/**
* Takes a list of file paths and loads the files and parses them into a @code List<raml>
*
* Created by jcooke on 27/01/16.
*/
public class RamlFileParser {

private static final Logger LOGGER = LoggerFactory.getLogger(RamlFileParser.class);

public RamlFileParser() {

LOGGER.debug("RamlLoader created");

}

public List<Raml> loadRaml(List<Path> ramlURIs) {


return ramlURIs
.stream()
.map(aFile -> {
LOGGER.info(new StringBuilder().append("Loading file ").append(aFile).toString());

return new RamlDocumentBuilder(new FileResourceLoader(aFile.toFile().getParent())).build(aFile.toFile().getName());
})
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uk.gov.justice.raml.io.files.parser;

import org.junit.Test;
import org.raml.model.Raml;
import uk.gov.justice.raml.io.FileTreeResolver;
import uk.gov.justice.raml.io.TierImportPattern;

import java.nio.file.Path;
import java.util.List;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* Created by jcooke on 08/03/16.
*/
public class RamlFileParserTest {

@Test
public void testLoadRaml() throws Exception {

FileTreeResolver fileTreeResolver = new FileTreeResolver();

List<Path> paths = fileTreeResolver.load(TestRaml.getTestRamlDirectory(), TierImportPattern.ALL);

RamlFileParser ramlLoader = new RamlFileParser();

List<Raml> ramls = ramlLoader.loadRaml(paths);

assertNotNull(ramls.get(0));
assertTrue(ramls.size() == 2);
assertTrue(ramls.get(1) instanceof Raml);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.justice.raml.io.files.parser;

import java.io.File;

/**
* Created by jcooke on 01/02/16.
*/
public class TestRaml {

private TestRaml() {}

public static File getTestRamlDirectory() {

return new File("src/test/resources/raml/");

}


public static File getTestNoRamlDirectory() {

return new File("src/test/resources/noraml/");

}


public static File getTestInvalidRamlDirectory() {

return new File("src/test/resources/invalidraml/");

}
}
Loading

0 comments on commit 0ca5238

Please sign in to comment.