Coverage Report - com.jcabi.github.mock.MkRepoCommits
 
Classes in this File Line Coverage Branch Coverage Complexity
MkRepoCommits
81%
13/16
0%
0/22
1
MkRepoCommits$1
50%
1/2
N/A
1
MkRepoCommits$AjcClosure1
100%
1/1
N/A
1
MkRepoCommits$AjcClosure11
0%
0/1
N/A
1
MkRepoCommits$AjcClosure3
100%
1/1
N/A
1
MkRepoCommits$AjcClosure5
100%
1/1
N/A
1
MkRepoCommits$AjcClosure7
100%
1/1
N/A
1
MkRepoCommits$AjcClosure9
100%
1/1
N/A
1
 
 1  2
 /**
 2  
  * Copyright (c) 2013-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.github.mock;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.github.CommitsComparison;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.RepoCommit;
 37  
 import com.jcabi.github.RepoCommits;
 38  
 import com.jcabi.xml.XML;
 39  
 import java.io.IOException;
 40  
 import java.util.Map;
 41  
 import javax.json.JsonObject;
 42  
 import javax.validation.constraints.NotNull;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.apache.commons.lang3.StringUtils;
 46  
 import org.xembly.Directives;
 47  
 
 48  
 /**
 49  
  * Mock commits of a Github repository.
 50  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 51  
  * @version $Id$
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @ToString
 56  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 57  
 final class MkRepoCommits implements RepoCommits {
 58  
 
 59  
     /**
 60  
      * Storage.
 61  
      */
 62  
     private final transient MkStorage storage;
 63  
 
 64  
     /**
 65  
      * Login of the user logged in.
 66  
      */
 67  
     private final transient String self;
 68  
 
 69  
     /**
 70  
      * Repo coordinates.
 71  
      */
 72  
     private final transient Coordinates coords;
 73  
 
 74  
     /**
 75  
      * Public ctor.
 76  
      * @param stg Storage
 77  
      * @param login User to login
 78  
      * @param repo Repository coordinates
 79  
      * @throws IOException If something goes wrong.
 80  
      */
 81  
     MkRepoCommits(
 82  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 83  
         @NotNull(message = "login can't be NULL")  final String login,
 84  
         @NotNull(message = "repo can't be NULL")final Coordinates repo
 85  7
     ) throws IOException {
 86  7
         this.storage = stg;
 87  7
         this.self = login;
 88  7
         this.coords = repo;
 89  7
         this.storage.apply(
 90  
             new Directives().xpath(
 91  
                 String.format("/github/repos/repo[@coords='%s']", this.coords)
 92  
             ).addIf("commits")
 93  
         );
 94  7
     }
 95  
 
 96  
     @Override
 97  
     @NotNull(message = "Iterable of commits can't be NULL")
 98  
     public Iterable<RepoCommit> iterate(
 99  
         @NotNull(message = "params can't be NULL")
 100  
         final Map<String, String> params
 101  
     ) {
 102  2
         return new MkIterable<RepoCommit>(
 103  
             this.storage, String.format("%s/commit", this.xpath()),
 104  1
             new MkIterable.Mapping<RepoCommit>() {
 105  
                 @Override
 106  
                 public RepoCommit map(final XML xml) {
 107  0
                     return MkRepoCommits.this.get(
 108  
                         xml.xpath("sha/text()").get(0)
 109  
                     );
 110  
                 }
 111  
             }
 112  
         );
 113  
     }
 114  
 
 115  
     @Override
 116  
     @NotNull(message = "repocommit can't be NULL")
 117  
     public RepoCommit get(
 118  
         @NotNull(message = "sha shouldn't be NULL") final String sha
 119  
     ) {
 120  2
         return new MkRepoCommit(
 121  
             this.storage, new MkRepo(this.storage, this.self, this.coords), sha
 122  
         );
 123  
     }
 124  
 
 125  
     @Override
 126  
     @NotNull(message = "comparison is never NULL")
 127  
     public CommitsComparison compare(
 128  
         @NotNull(message = "base can't be NULL") final String base,
 129  
         @NotNull(message = "head can't be NULL") final String head
 130  
     ) {
 131  2
         return new MkCommitsComparison(this.storage, this.self, this.coords);
 132  
     }
 133  
 
 134  
     @Override
 135  
     @NotNull(message = "diff is never NULL")
 136  
     public String diff(
 137  
         @NotNull(message = "base should not be NULL") final String base,
 138  
         @NotNull(message = "head should not be NULL") final String head
 139  
     ) {
 140  2
         return
 141  
         String.format(
 142  
             "%s%sindex %s..%s",
 143  
             "diff --git a/README b/README",
 144  
             System.getProperty("line.separator"), base, head
 145  
         );
 146  
     }
 147  
 
 148  
     @Override
 149  
     @NotNull(message = "patch is never NULL")
 150  
     public String patch(
 151  
         @NotNull(message = "base shouldn't be NULL") final String base,
 152  
         @NotNull(message = "head shouldn't be NULL") final String head
 153  
     ) {
 154  2
         return StringUtils.join(
 155  
             String.format("From %s Mon Sep 17 00:00:00 2001\n", head),
 156  
             "From: Some Author <some_author@email.com>\n",
 157  
             "Date: Tue, 11 Feb 2014 20:33:49 +0200\n",
 158  
             "Subject: Some subject\n", "\n", "---\n",
 159  
             " .../java/com/jcabi/github/CommitsComparison.java   |   6 +-\n",
 160  
             " src/main/java/com/jcabi/github/RepoCommit.java     | 131 +++++",
 161  
             "++++++++++++++++\n",
 162  
             " src/main/java/com/jcabi/github/RepoCommits.java    |  15 +--\n",
 163  
             " src/main/java/com/jcabi/github/RtRepoCommit.java   | 110 +++++",
 164  
             "++++++++++++\n",
 165  
             " src/main/java/com/jcabi/github/RtRepoCommits.java  |   6 +-\n",
 166  
             " .../java/com/jcabi/github/mock/MkRepoCommits.java  |   6 +-\n",
 167  
             " src/test/java/com/jcabi/github/RepoCommitTest.java |  84 +++++",
 168  
             "++++++++\n",
 169  
             " .../java/com/jcabi/github/RtRepoCommitsITCase.java |   7 +-\n",
 170  
             " 8 files changed, 346 insertions(+), 19 deletions(-)\n",
 171  
             " create mode 100644 src/main/java/com/jcabi/github/",
 172  
             "RepoCommit.java\n",
 173  
             " create mode 100644 src/main/java/com/jcabi/github/RtRepoCommit",
 174  
             ".java\n",
 175  
             " create mode 100644 src/test/java/com/jcabi/github/",
 176  
             "RepoCommitTest.java"
 177  
         );
 178  
     }
 179  
 
 180  
     @Override
 181  
     @NotNull(message = "JSON is never NULL")
 182  
     public JsonObject json() throws IOException {
 183  0
         return new JsonNode(
 184  
             this.storage.xml().nodes(this.xpath()).get(0)
 185  
         ).json();
 186  
     }
 187  
 
 188  
     /**
 189  
      * Xpath of this element in XML tree.
 190  
      * @return Xpath
 191  
      */
 192  
     @NotNull(message = "Xpath is never NULL")
 193  
     private String xpath() {
 194  1
         return String.format(
 195  
             "/github/repos/repo[@coords='%s']/commits",
 196  
             this.coords
 197  
         );
 198  
     }
 199  
 }