Skip to content

Commit

Permalink
yegor256#561 YAML validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyk authored and krzyk committed Dec 27, 2014
1 parent c2c212e commit 3d47924
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
1 change: 1 addition & 0 deletions .rultor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ architect:
install: |
sudo apt-get install bsdmainutils
sudo gem install pdd
sudo gem install kwalify
cd src/bundle
sudo bundle install
cd ../..
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/rultor/profiles/GithubProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package com.rultor.profiles;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.jcabi.aspects.Immutable;
import com.jcabi.github.Content;
Expand All @@ -43,6 +44,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -224,7 +227,29 @@ private String yml() throws IOException {
} else {
yml = "";
}
final List<String> msg = this.validate(yml);
if (!msg.isEmpty()) {
throw new Profile.ConfigException(
String.format(
"`%s` is not valid according to schema:\n``%s``",
GithubProfile.FILE,
Joiner.on('\n').join(msg)
)
);
}
return yml;
}

/**
* Validate rultor config YAML according to schema.
* @param yml Rultor YAML config
* @return Validation result message, empty list means validation succeeded.
* @todo #561:30min Implement validation using Kwalify library enable
* rejectsEmptyYaml and rejectsYamlWithout(Release/Deploy/Merge) tests and
* remove UnusedFormalParameter suppress below.
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
private List<String> validate(final String yml) {
return Collections.emptyList();
}
}
1 change: 0 additions & 1 deletion src/test/java/com/rultor/profiles/GithubProfileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class GithubProfileTest {

/**
* GithubProfile can fetch a YAML config.
* @throws Exception In case of error.
Expand Down
136 changes: 136 additions & 0 deletions src/test/java/com/rultor/profiles/GithubProfileValidationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Copyright (c) 2009-2014, rultor.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.profiles;

import com.google.common.base.Joiner;
import com.jcabi.github.Github;
import com.jcabi.github.Repo;
import com.jcabi.github.mock.MkGithub;
import com.rultor.spi.Profile;
import java.io.IOException;
import javax.json.Json;
import org.apache.commons.codec.binary.Base64;
import org.junit.Ignore;
import org.junit.Test;

/**
* Tests for ${@link GithubProfile} YAML validation.
* @author Krzysztof Krason ([email protected])
* @version $Id$
* @checkstyle MultipleStringLiteralsCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class GithubProfileValidationTest {
/**
* GithubProfile can reject empty YAML.
* @throws Exception In case of error.
*/
@Ignore
@Test(expected = Profile.ConfigException.class)
public void rejectsEmptyYaml() throws Exception {
final Repo repo = GithubProfileValidationTest.repo("");
new GithubProfile(repo).read();
}

/**
* GithubProfile can reject YAML without release phase.
* @throws Exception In case of error.
*/
@Ignore
@Test(expected = Profile.ConfigException.class)
public void rejectsYamlWithoutRelease() throws Exception {
final Repo repo = GithubProfileValidationTest.repo(
Joiner.on('\n').join(
"deploy:",
" script: whoami",
"merge:",
" script: pwd"
)
);
new GithubProfile(repo).read();
}

/**
* GithubProfile can reject YAML without merge phase.
* @throws Exception In case of error.
*/
@Ignore
@Test(expected = Profile.ConfigException.class)
public void rejectsYamlWithoutMerge() throws Exception {
final Repo repo = GithubProfileValidationTest.repo(
Joiner.on('\n').join(
"deploy:",
" script: whoami",
"release:",
" script: pwd"
)
);
new GithubProfile(repo).read();
}

/**
* GithubProfile can reject YAML without deploy phase.
* @throws Exception In case of error.
*/
@Ignore
@Test(expected = Profile.ConfigException.class)
public void rejectsYamlWithoutDeploy() throws Exception {
final Repo repo = GithubProfileValidationTest.repo(
Joiner.on('\n').join(
"merge:",
" script: whoami",
"release:",
" script: pwd"
)
);
new GithubProfile(repo).read();
}

/**
* Create repo with .rultor.yml inside.
* @param yaml Content of .rultor.yml file
* @return Created repo.
* @throws IOException In case of error.
*/
private static Repo repo(final String yaml) throws IOException {
final Github github = new MkGithub("jeff");
final Repo repo = github.repos().create(
Json.createObjectBuilder().add("name", "test").build()
);
repo.contents().create(
Json.createObjectBuilder()
.add("path", ".rultor.yml")
.add("message", "just test")
.add("content", Base64.encodeBase64String(yaml.getBytes()))
.build()
);
return repo;
}
}

0 comments on commit 3d47924

Please sign in to comment.