Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#561 YAML validation tests #691

Merged
merged 3 commits into from
Dec 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 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,30 @@ 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 #570:30min Implement validation using Kwalify library in separate
* class called ValidYaml, move this method to that class and move tests
* from GitHubProfileValidationTest to ValidYamlTest. Remember about
* removing PMD 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
131 changes: 131 additions & 0 deletions src/test/java/com/rultor/profiles/GithubProfileValidationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* 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)
* @todo #561:30min Implement validation tests based on 2014-07-13-basics.md
* file.
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class GithubProfileValidationTest {
/**
* GithubProfile will accept empty rultor configuration.
* @throws Exception In case of error.
*/
@Test
public void acceptsEmptyYaml() throws Exception {
final Repo repo = GithubProfileValidationTest.repo("");
new GithubProfile(repo).read();
}

/**
* GithubProfile can reject YAML with missing script in merge command.
* @throws Exception In case of error.
*/
@Ignore
@Test(expected = Profile.ConfigException.class)
public void rejectsYamlWithoutMergeScript() throws Exception {
final Repo repo = GithubProfileValidationTest.repo(
Joiner.on('\n').join(
"merge:",
" - pwd"
)
);
new GithubProfile(repo).read();
}

/**
* GithubProfile can reject YAML with missing script in deploy command.
* @throws Exception In case of error.
*/
@Ignore
@Test(expected = Profile.ConfigException.class)
public void rejectsYamlWithoutDeployScript() throws Exception {
final Repo repo = GithubProfileValidationTest.repo(
Joiner.on('\n').join(
"deploy:",
" - pwd"
)
);
new GithubProfile(repo).read();
}

/**
* GithubProfile can reject YAML with missing script in release command.
* @throws Exception In case of error.
*/
@Ignore
@Test(expected = Profile.ConfigException.class)
public void rejectsYamlWithoutReleaseScript() throws Exception {
final Repo repo = GithubProfileValidationTest.repo(
Joiner.on('\n').join(
"release:",
" - 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;
}
}