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

Commit

Permalink
Unit tests for CachedRepo.charlesYml()
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 9, 2017
1 parent 88943fb commit 3f180b9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 12 deletions.
22 changes: 13 additions & 9 deletions src/main/java/com/amihaiemil/charles/github/CachedRepo.java
Expand Up @@ -157,15 +157,19 @@ public boolean isOwnedByOrganization() throws IOException {
*/
public CharlesYml charlesYml() throws IOException {
if(this.yml == null) {
this.yml = new CharlesYmlInput(
new ByteArrayInputStream(
new Content.Smart(
this.repo
.contents()
.get(".charles.yml")
).decoded()
)
);
if(this.repo.contents().exists(".charles.yml", "master")) {
this.yml = new CharlesYmlInput(
new ByteArrayInputStream(
new Content.Smart(
this.repo
.contents()
.get(".charles.yml")
).decoded()
)
);
} else {
this.yml = new CharlesYml.Default();
}
}
return this.yml;
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/amihaiemil/charles/github/CharlesYml.java
Expand Up @@ -25,6 +25,7 @@
*/
package com.amihaiemil.charles.github;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -40,12 +41,27 @@ public interface CharlesYml {
* Usernames of users who are allowed to command the bot.
* @return String[]
*/
abstract List<String> commanders();
List<String> commanders();

/**
* Should the bot tweet its activity?
* @return Boolean.
*/
abstract boolean tweet();
boolean tweet();

/**
* Default .charles.yml file.
*/
public static final class Default implements CharlesYml {
@Override
public List<String> commanders() {
return new ArrayList<>();
}

@Override
public boolean tweet() {
return false;
}
}

}
Expand Up @@ -35,9 +35,13 @@
import javax.json.Json;
import javax.json.JsonObject;

import org.apache.commons.codec.binary.Base64;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

import com.amihaiemil.camel.Yaml;
import com.jcabi.github.Repo;
import com.jcabi.github.Repos.RepoCreate;
import com.jcabi.github.mock.MkGithub;
Expand All @@ -62,7 +66,7 @@ public class CachedRepoTestCase {
@Test
public void repoHasGhPagesBranch() throws Exception {
int port = this.port();
MkContainer server = new MkGrizzlyContainer()
MkContainer server = new MkGrizzlyContainer()
.next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK))
.start(port);
try {
Expand Down Expand Up @@ -167,6 +171,56 @@ public void getsJson() throws Exception {
assertTrue(repoJson == repoFromCache);
}

/**
* CachedRepo can return the .charles.yml which is in the repo.
* @throws Exception If something goes wrong.
*/
@Test
public void getsExistingCharlesYml() throws Exception {
final MkGithub gh = new MkGithub("amihaiemil");
final Repo repo = gh.repos().create(new RepoCreate("charlesrepo", false));
repo.contents()
.create(
Json.createObjectBuilder()
.add("path", ".charles.yml")
.add("message", "just a test")
.add(
"content",
Base64.encodeBase64String(
Yaml.createYamlMappingBuilder()
.add("tweet", "true")
.add(
"commanders",
Yaml.createYamlSequenceBuilder()
.add("johndoe")
.add("amihaiemil")
.add("queeney")
.build()
).build().toString().getBytes()
)
).build()
);
final CharlesYml yml = new CachedRepo(repo).charlesYml();
MatcherAssert.assertThat(yml.commanders(), Matchers.hasSize(3));
MatcherAssert.assertThat(yml.commanders().get(0), Matchers.equalTo("amihaiemil"));//YAML orders them alphabetically
MatcherAssert.assertThat(yml.commanders().get(1), Matchers.equalTo("johndoe"));
MatcherAssert.assertThat(yml.commanders().get(2), Matchers.equalTo("queeney"));
MatcherAssert.assertThat(yml.tweet(), Matchers.is(true));
}

/**
* CachedRepo can return a default ChalesYml if the repo does not have it.
* @throws Exception If something goes worng.
*/
@Test
public void getsDefaultCharlesYml() throws Exception {
MkGithub gh = new MkGithub("amihaiemil");
Repo rep = gh.repos().create(new RepoCreate("charlesrepo", false));
CharlesYml yml = new CachedRepo(rep).charlesYml();
MatcherAssert.assertThat(yml.commanders(), Matchers.emptyIterable());
MatcherAssert.assertThat(yml.tweet(), Matchers.is(false));
}

/**
* Find a free port.
* @return A free port.
Expand Down

0 comments on commit 3f180b9

Please sign in to comment.