Skip to content

Commit

Permalink
ComdorYamlInput, Mention.comdorYaml() and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Nov 4, 2017
1 parent 584fcbf commit ee8187c
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/main/java/co/comdor/github/ComdorYaml.java
Expand Up @@ -36,7 +36,6 @@
* @todo #27:30min Design and implement "aliases", which will basically be
* customized commands, aliases, labels for scripts which the users may want
* to execute periodically, like merging or deploying scripts.
* @todo #27:30min Add an implementation based on camel library.
*/
public interface ComdorYaml {

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/co/comdor/github/ComdorYamlInput.java
@@ -0,0 +1,67 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package co.comdor.github;

import com.amihaiemil.camel.Yaml;
import com.amihaiemil.camel.YamlMapping;
import com.amihaiemil.camel.YamlSequence;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* CcmdorYaml from InputStream.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.2
*/
public final class ComdorYamlInput implements ComdorYaml {

/**
* Contents of .comdor.yml.
*/
private final YamlMapping yaml;

/**
* Ctor.
* @param yaml .charles.yml.
* @throws IOException If the input stream cannot be read.
*/
public ComdorYamlInput(final InputStream yaml) throws IOException {
this.yaml = Yaml.createYamlInput(yaml).readYamlMapping();
}

@Override
public String docker() {
return this.yaml.string("docker");
}

@Override
public List<String> architects() {
final List<String> architects = new ArrayList<>();
final YamlSequence sequence = this.yaml.yamlSequence("architects");
if(sequence != null) {
for(int idx=0; idx<sequence.size(); idx = idx + 1) {
architects.add(sequence.string(idx));
}
}
return architects;
}

@Override
public List<String> commanders() {
final List<String> commanders = new ArrayList<>();
final YamlSequence sequence = this.yaml.yamlSequence("commanders");
if(sequence != null) {
for(int idx=0; idx<sequence.size(); idx = idx + 1) {
commanders.add(sequence.string(idx));
}
}
return commanders;
}

}
25 changes: 23 additions & 2 deletions src/main/java/co/comdor/github/JsonMention.java
Expand Up @@ -25,7 +25,9 @@
*/
package co.comdor.github;

import com.jcabi.github.Content;
import com.jcabi.github.Issue;
import java.io.ByteArrayInputStream;

import javax.json.JsonObject;
import java.io.IOException;
Expand All @@ -47,12 +49,12 @@ public abstract class JsonMention implements Mention {
* Github issue Comment in Json format.
* @see https://developer.github.com/v3/issues/comments/
*/
private JsonObject json;
private final JsonObject json;

/**
* Issue where the mention was found.
*/
private Issue issue;
private final Issue issue;

/**
* Ctor.
Expand All @@ -70,6 +72,25 @@ public JsonMention(final JsonObject json, final Issue issue) {
@Override
public abstract Language language();

@Override
public final ComdorYaml comdorYaml() throws IOException {
final ComdorYaml yaml;
if(this.issue.repo().contents().exists(".comdor.yml", "master")) {
yaml = new ComdorYamlInput(
new ByteArrayInputStream(
new Content.Smart(
this.issue.repo()
.contents()
.get(".comdor.yml")
).decoded()
)
);
} else {
yaml = new ComdorYaml.Missing();
}
return yaml;
}

@Override
public abstract void understand(final Language... langs) throws IOException;

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/co/comdor/github/Mention.java
Expand Up @@ -68,6 +68,14 @@ public interface Mention {
*/
Issue issue();

/**
* Comdor.yml file present in the repository's root, which contains
* configurations.
* @return ComdorYaml
* @throws IOException If .comdor.yml cannot be read from the repository.
*/
ComdorYaml comdorYaml() throws IOException;

/**
* Reply to this mention.
* @param message Message of the reply.
Expand Down
130 changes: 130 additions & 0 deletions src/test/java/co/comdor/github/ComdorYamlInputTestCase.java
@@ -0,0 +1,130 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package co.comdor.github;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Unit tests for {@link ComdorYamlInput}.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.2
*/
public final class ComdorYamlInputTestCase {

/**
* ComdorYamlInput can read the commanders list.
* @throws IOException If something goes wrong.
*/
@Test
public void readsCommanders() throws IOException {
final ComdorYaml comdor = new ComdorYamlInput(
new ByteArrayInputStream(
"docker: a/b\ncommanders:\n - john\n - amihaiemil".getBytes()
)
);
final List<String> commanders = comdor.commanders();
MatcherAssert.assertThat(commanders, Matchers.hasSize(2));
MatcherAssert.assertThat(
commanders.get(0), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
commanders.get(1), Matchers.equalTo("john")
);
}

/**
* ComdorYamlInput can read the architects list.
* @throws IOException If something goes wrong.
*/
@Test
public void readsArchitects() throws IOException {
final ComdorYaml comdor = new ComdorYamlInput(
new ByteArrayInputStream(
"docker: c/d\narchitects:\n - john\n - amihaiemil".getBytes()
)
);
final List<String> architects = comdor.architects();
MatcherAssert.assertThat(architects, Matchers.hasSize(2));
MatcherAssert.assertThat(
architects.get(0), Matchers.equalTo("amihaiemil")
);
MatcherAssert.assertThat(
architects.get(1), Matchers.equalTo("john")
);
}

/**
* Reads the docker attribute.
* @throws IOException If something goes wrong.
*/
@Test
public void readsDocker() throws IOException {
final ComdorYaml comdor = new ComdorYamlInput(
new ByteArrayInputStream(
"docker: amihaiemil/java9".getBytes()
)
);
MatcherAssert.assertThat(
comdor.docker(), Matchers.equalTo("amihaiemil/java9")
);
}

/**
* Docker attribute is missing, so ComdorYamlInput returns null.
* @throws IOException If something goes wrong.
*/
@Test
public void missingDocker() throws IOException {
final ComdorYaml comdor = new ComdorYamlInput(
new ByteArrayInputStream(
"commanders:\n - somone".getBytes()
)
);
MatcherAssert.assertThat(
comdor.docker(), Matchers.equalTo(null)
);
}

/**
* The commanders' list is missing, so ComdorYamlInput returns an
* empty list.
* @throws IOException If something goes wrong.
*/
@Test
public void missingCommanders() throws IOException {
final ComdorYaml comdor = new ComdorYamlInput(
new ByteArrayInputStream(
"architects:\n - somone".getBytes()
)
);
MatcherAssert.assertThat(
comdor.commanders(), Matchers.iterableWithSize(0)
);
}

/**
* The architects' list is missing, so ComdorYamlInput returns an
* empty list.
* @throws IOException If something goes wrong.
*/
@Test
public void missingArchitects() throws IOException {
final ComdorYaml comdor = new ComdorYamlInput(
new ByteArrayInputStream(
"docker: a/b".getBytes()
)
);
MatcherAssert.assertThat(
comdor.architects(), Matchers.iterableWithSize(0)
);
}
}
61 changes: 61 additions & 0 deletions src/test/java/co/comdor/github/JsonMentionTestCase.java
Expand Up @@ -25,7 +25,11 @@
*/
package co.comdor.github;

import com.amihaiemil.camel.Yaml;
import com.jcabi.github.Issue;
import com.jcabi.github.Repo;
import com.jcabi.github.Repos;
import com.jcabi.github.mock.MkGithub;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand All @@ -34,6 +38,7 @@
import javax.json.Json;
import javax.json.JsonObject;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;

/**
* Unit tests for {@link JsonMention}
Expand Down Expand Up @@ -110,6 +115,62 @@ public void returnsTheScripts() throws Exception {
);
}

/**
* JsonMention can return the .comdor.yml if it exists in the repository.
* @throws Exception If something goes wrong.
*/
@Test
public void returnsExistingComdorYaml() throws Exception {
final MkGithub gh = new MkGithub("amihaiemil");
final Repo repo = gh.repos().create(
new Repos.RepoCreate("charlesrepo", false)
);
repo.contents()
.create(
Json.createObjectBuilder()
.add("path", ".comdor.yml")
.add("message", "just a test")
.add(
"content",
Base64.encodeBase64String(
"docker: test".getBytes()
)
).build()
);
final Mention mention = new MockConcrete(
Json.createObjectBuilder().build(),
repo.issues().create("for test", "body")
);
MatcherAssert.assertThat(mention.comdorYaml(), Matchers.notNullValue());
MatcherAssert.assertThat(
mention.comdorYaml() instanceof ComdorYamlInput,
Matchers.is(true)
);
}

/**
* JsonMention can return the .comdor.yml
* if it is missing from the repository.
* @throws Exception If something goes wrong.
*/
@Test
public void returnsMissingComdorYaml() throws Exception {
final MkGithub gh = new MkGithub("amihaiemil");
final Repo repo = gh.repos().create(
new Repos.RepoCreate("charlesrepo", false)
);
final Mention mention = new MockConcrete(
Json.createObjectBuilder().build(),
repo.issues().create("for test", "body")
);
final ComdorYaml yaml = mention.comdorYaml();
MatcherAssert.assertThat(yaml, Matchers.notNullValue());
MatcherAssert.assertThat(
yaml instanceof ComdorYaml.Missing,
Matchers.is(true)
);
}

/**
* Mock concrete class, for testing. It's better to test the final
* methods of JsonMention separately from LastMention or other concrete
Expand Down

1 comment on commit ee8187c

@0pdd
Copy link

@0pdd 0pdd commented on ee8187c Nov 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 27-316a9c24 disappeared from src/main/java/co/comdor/github/ComdorYaml.java, that's why I closed #32.

Please sign in to comment.