From 3d479247d7b1cd51663e63b504379f8bb40671cb Mon Sep 17 00:00:00 2001 From: krzyk Date: Sat, 27 Dec 2014 12:42:46 +0100 Subject: [PATCH] #561 YAML validation tests --- .rultor.yml | 1 + .../com/rultor/profiles/GithubProfile.java | 25 ++++ .../rultor/profiles/GithubProfileTest.java | 1 - .../profiles/GithubProfileValidationTest.java | 136 ++++++++++++++++++ 4 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/rultor/profiles/GithubProfileValidationTest.java diff --git a/.rultor.yml b/.rultor.yml index a0fd378086..22be3c361b 100644 --- a/.rultor.yml +++ b/.rultor.yml @@ -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 ../.. diff --git a/src/main/java/com/rultor/profiles/GithubProfile.java b/src/main/java/com/rultor/profiles/GithubProfile.java index 172d833d03..8a0232c231 100644 --- a/src/main/java/com/rultor/profiles/GithubProfile.java +++ b/src/main/java/com/rultor/profiles/GithubProfile.java @@ -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; @@ -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; @@ -224,7 +227,29 @@ private String yml() throws IOException { } else { yml = ""; } + final List 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 validate(final String yml) { + return Collections.emptyList(); + } } diff --git a/src/test/java/com/rultor/profiles/GithubProfileTest.java b/src/test/java/com/rultor/profiles/GithubProfileTest.java index 12f9ce106d..ad72870148 100644 --- a/src/test/java/com/rultor/profiles/GithubProfileTest.java +++ b/src/test/java/com/rultor/profiles/GithubProfileTest.java @@ -53,7 +53,6 @@ */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class GithubProfileTest { - /** * GithubProfile can fetch a YAML config. * @throws Exception In case of error. diff --git a/src/test/java/com/rultor/profiles/GithubProfileValidationTest.java b/src/test/java/com/rultor/profiles/GithubProfileValidationTest.java new file mode 100644 index 0000000000..9b2d33c45a --- /dev/null +++ b/src/test/java/com/rultor/profiles/GithubProfileValidationTest.java @@ -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 (Krzysztof.Krason@gmail.com) + * @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; + } +}