Coverage Report - com.jcabi.github.mock.MkRepoCommits
 
Classes in this File Line Coverage Branch Coverage Complexity
MkRepoCommits
80%
20/25
0%
0/28
1
MkRepoCommits$1
33%
1/3
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-2017, 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 lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 import org.apache.commons.lang3.StringUtils;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock commits of a Github repository.
 49  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 50  
  * @version $Id: f441d6e1d28895ef528b22781056ba5077bb72cb $
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @ToString
 55  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 56  
 final class MkRepoCommits implements RepoCommits {
 57  
 
 58  
     /**
 59  
      * Storage.
 60  
      */
 61  
     private final transient MkStorage storage;
 62  
 
 63  
     /**
 64  
      * Login of the user logged in.
 65  
      */
 66  
     private final transient String self;
 67  
 
 68  
     /**
 69  
      * Repo coordinates.
 70  
      */
 71  
     private final transient Coordinates coords;
 72  
 
 73  
     /**
 74  
      * Public ctor.
 75  
      * @param stg Storage
 76  
      * @param login User to login
 77  
      * @param repo Repository coordinates
 78  
      * @throws IOException If something goes wrong.
 79  
      */
 80  
     MkRepoCommits(
 81  
         final MkStorage stg,
 82  
         final String login,
 83  
         final Coordinates repo
 84  7
     ) throws IOException {
 85  7
         this.storage = stg;
 86  7
         this.self = login;
 87  7
         this.coords = repo;
 88  13
         this.storage.apply(
 89  6
             new Directives().xpath(
 90  7
                 String.format("/github/repos/repo[@coords='%s']", this.coords)
 91  6
             ).addIf("commits")
 92  
         );
 93  7
     }
 94  
 
 95  
     @Override
 96  
     public Iterable<RepoCommit> iterate(
 97  
         final Map<String, String> params
 98  
     ) {
 99  3
         return new MkIterable<RepoCommit>(
 100  1
             this.storage, String.format("%s/commit", this.xpath()),
 101  1
             new MkIterable.Mapping<RepoCommit>() {
 102  
                 @Override
 103  
                 public RepoCommit map(final XML xml) {
 104  0
                     return MkRepoCommits.this.get(
 105  0
                         xml.xpath("sha/text()").get(0)
 106  
                     );
 107  
                 }
 108  
             }
 109  
         );
 110  
     }
 111  
 
 112  
     @Override
 113  
     public RepoCommit get(
 114  
         final String sha
 115  
     ) {
 116  2
         return new MkRepoCommit(
 117  
             this.storage, new MkRepo(this.storage, this.self, this.coords), sha
 118  
         );
 119  
     }
 120  
 
 121  
     @Override
 122  
     public CommitsComparison compare(
 123  
         final String base,
 124  
         final String head
 125  
     ) {
 126  2
         return new MkCommitsComparison(this.storage, this.self, this.coords);
 127  
     }
 128  
 
 129  
     @Override
 130  
     public String diff(
 131  
         final String base,
 132  
         final String head
 133  
     ) {
 134  3
         return
 135  1
         String.format(
 136  
             "%s%sindex %s..%s",
 137  
             "diff --git a/README b/README",
 138  1
             System.getProperty("line.separator"), base, head
 139  
         );
 140  
     }
 141  
 
 142  
     @Override
 143  
     public String patch(
 144  
         final String base,
 145  
         final String head
 146  
     ) {
 147  3
         return StringUtils.join(
 148  1
             String.format("From %s Mon Sep 17 00:00:00 2001\n", head),
 149  
             "From: Some Author <some_author@email.com>\n",
 150  
             "Date: Tue, 11 Feb 2014 20:33:49 +0200\n",
 151  
             "Subject: Some subject\n", "\n", "---\n",
 152  
             " .../java/com/jcabi/github/CommitsComparison.java   |   6 +-\n",
 153  
             " src/main/java/com/jcabi/github/RepoCommit.java     | 131 +++++",
 154  
             "++++++++++++++++\n",
 155  
             " src/main/java/com/jcabi/github/RepoCommits.java    |  15 +--\n",
 156  
             " src/main/java/com/jcabi/github/RtRepoCommit.java   | 110 +++++",
 157  
             "++++++++++++\n",
 158  
             " src/main/java/com/jcabi/github/RtRepoCommits.java  |   6 +-\n",
 159  
             " .../java/com/jcabi/github/mock/MkRepoCommits.java  |   6 +-\n",
 160  
             " src/test/java/com/jcabi/github/RepoCommitTest.java |  84 +++++",
 161  
             "++++++++\n",
 162  
             " .../java/com/jcabi/github/RtRepoCommitsITCase.java |   7 +-\n",
 163  
             " 8 files changed, 346 insertions(+), 19 deletions(-)\n",
 164  
             " create mode 100644 src/main/java/com/jcabi/github/",
 165  
             "RepoCommit.java\n",
 166  
             " create mode 100644 src/main/java/com/jcabi/github/RtRepoCommit",
 167  
             ".java\n",
 168  
             " create mode 100644 src/test/java/com/jcabi/github/",
 169  
             "RepoCommitTest.java"
 170  
         );
 171  
     }
 172  
 
 173  
     @Override
 174  
     public JsonObject json() throws IOException {
 175  0
         return new JsonNode(
 176  0
             this.storage.xml().nodes(this.xpath()).get(0)
 177  0
         ).json();
 178  
     }
 179  
 
 180  
     /**
 181  
      * Xpath of this element in XML tree.
 182  
      * @return Xpath
 183  
      */
 184  
     private String xpath() {
 185  1
         return String.format(
 186  
             "/github/repos/repo[@coords='%s']/commits",
 187  
             this.coords
 188  
         );
 189  
     }
 190  
 }