Coverage Report - com.jcabi.github.mock.MkContents
 
Classes in this File Line Coverage Branch Coverage Complexity
MkContents
93%
59/63
39%
15/38
1.562
MkContents$AjcClosure1
100%
1/1
N/A
1.562
MkContents$AjcClosure11
100%
1/1
N/A
1.562
MkContents$AjcClosure13
100%
1/1
N/A
1.562
MkContents$AjcClosure15
100%
1/1
N/A
1.562
MkContents$AjcClosure17
100%
1/1
N/A
1.562
MkContents$AjcClosure19
100%
1/1
N/A
1.562
MkContents$AjcClosure3
100%
1/1
N/A
1.562
MkContents$AjcClosure5
100%
1/1
N/A
1.562
MkContents$AjcClosure7
100%
1/1
N/A
1.562
MkContents$AjcClosure9
100%
1/1
N/A
1.562
 
 1  6
 /**
 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.Content;
 35  
 import com.jcabi.github.Contents;
 36  
 import com.jcabi.github.Coordinates;
 37  
 import com.jcabi.github.Repo;
 38  
 import com.jcabi.github.RepoCommit;
 39  
 import com.jcabi.xml.XML;
 40  
 import java.io.IOException;
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import javax.json.JsonObject;
 44  
 import javax.validation.constraints.NotNull;
 45  
 import lombok.EqualsAndHashCode;
 46  
 import lombok.ToString;
 47  
 import org.apache.commons.lang3.RandomStringUtils;
 48  
 import org.xembly.Directives;
 49  
 
 50  
 /**
 51  
  * Mock Github contents.
 52  
  *
 53  
  * @author Andres Candal (andres.candal@rollasolution.com)
 54  
  * @version $Id$
 55  
  * @since 0.8
 56  
  */
 57  
 @Immutable
 58  
 @Loggable(Loggable.DEBUG)
 59  0
 @ToString
 60  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 61  
 @SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods" })
 62  
 final class MkContents implements Contents {
 63  
 
 64  
     /**
 65  
      * Storage.
 66  
      */
 67  
     private final transient MkStorage storage;
 68  
 
 69  
     /**
 70  
      * Login of the user logged in.
 71  
      */
 72  
     private final transient String self;
 73  
 
 74  
     /**
 75  
      * Repo name.
 76  
      */
 77  
     private final transient Coordinates coords;
 78  
 
 79  
     /**
 80  
      * Public ctor.
 81  
      * @param stg Storage
 82  
      * @param login User to login
 83  
      * @param rep Repo
 84  
      * @throws IOException If there is any I/O problem
 85  
      */
 86  
     public MkContents(
 87  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 88  
         @NotNull(message = "login can't be NULL") final String login,
 89  
         @NotNull(message = "rep can't be NULL") final Coordinates rep
 90  22
     ) throws IOException {
 91  22
         this.storage = stg;
 92  22
         this.self = login;
 93  22
         this.coords = rep;
 94  22
         this.storage.apply(
 95  
             new Directives().xpath(
 96  
                 String.format("/github/repos/repo[@coords='%s']", this.coords)
 97  
             ).addIf("contents").up().addIf("commits")
 98  
         );
 99  22
     }
 100  
 
 101  
     @Override
 102  
     @NotNull(message = "Repo is never NULL")
 103  
     public Repo repo() {
 104  48
         return new MkRepo(this.storage, this.self, this.coords);
 105  
     }
 106  
 
 107  
     @Override
 108  
     @NotNull(message = "the content is never NULL")
 109  
     public Content readme() throws IOException {
 110  
         // @checkstyle MultipleStringLiterals (1 line)
 111  2
         return this.readme("master");
 112  
     }
 113  
 
 114  
     @Override
 115  
     @NotNull(message = "the content is never NULL")
 116  
     public Content readme(
 117  
         @NotNull(message = "github can't be  NULL") final String branch
 118  
     ) throws IOException {
 119  4
         return new MkContent(
 120  
             this.storage, this.self, this.coords, "README.md", branch
 121  
         );
 122  
     }
 123  
 
 124  
     @Override
 125  
     @NotNull(message = "created content is never NULL")
 126  
     public Content create(
 127  
         @NotNull(message = "json can't be NULL") final JsonObject json
 128  
     ) throws IOException {
 129  38
         this.storage.lock();
 130  
         // @checkstyle MultipleStringLiterals (20 lines)
 131  
         final String branch;
 132  
         try {
 133  19
             if (json.containsKey("ref")) {
 134  8
                 branch = json.getString("ref");
 135  
             } else {
 136  11
                 branch = "master";
 137  
             }
 138  19
             this.storage.apply(
 139  
                 new Directives().xpath(this.xpath()).add("content")
 140  
                     .attr("ref", branch)
 141  
                     .add("name").set(json.getString("path")).up()
 142  
                     .add("path").set(json.getString("path")).up()
 143  
                     .add("content").set(json.getString("content")).up()
 144  
                     .add("type").set("file").up()
 145  
                     .add("encoding").set("base64").up()
 146  
                     .add("sha").set(fakeSha()).up()
 147  
                     .add("url").set("http://localhost/1").up()
 148  
                     .add("git_url").set("http://localhost/2").up()
 149  
                     .add("html_url").set("http://localhost/3").up()
 150  
             );
 151  19
             this.commit(json);
 152  
         } finally {
 153  19
             this.storage.unlock();
 154  19
         }
 155  19
         return new MkContent(
 156  
             this.storage, this.self, this.coords, json.getString("path"), branch
 157  
         );
 158  
     }
 159  
 
 160  
     @Override
 161  
     @NotNull(message = "retrieved content is never NULL")
 162  
     public Content get(
 163  
         @NotNull(message = "path can't be NULL") final String path,
 164  
         @NotNull(message = "ref can't be NULL") final String ref
 165  
     ) throws IOException {
 166  6
         return new MkContent(this.storage, this.self, this.coords, path, ref);
 167  
     }
 168  
 
 169  
     @Override
 170  
     @NotNull(message = "retrieved content is never NULL")
 171  
     public Content get(
 172  
         @NotNull(message = "path can't be NULL") final String path
 173  
     ) throws IOException {
 174  2
         return new MkContent(
 175  
             this.storage, this.self, this.coords, path, "master"
 176  
         );
 177  
     }
 178  
 
 179  
     @Override
 180  
     @NotNull(message = "Iterable of contents is never NULL")
 181  
     public Iterable<Content> iterate(
 182  
         @NotNull(message = "pattern can't be NULL") final String pattern,
 183  
         @NotNull(message = "ref can't be NULL") final String ref
 184  
     ) throws IOException {
 185  2
         final Collection<XML> nodes = this.storage.xml().nodes(
 186  
             String.format("%s/content[@ref='%s']", this.xpath(), ref)
 187  
         );
 188  1
         final Collection<Content> result = new ArrayList<Content>(nodes.size());
 189  1
         for (final XML node : nodes) {
 190  4
             final String path = node.xpath("path/text()").get(0);
 191  4
             if (path.startsWith(pattern)) {
 192  2
                 result.add(
 193  
                     this.mkContent(ref, path)
 194  
                 );
 195  
             }
 196  4
         }
 197  1
         return result;
 198  
     }
 199  
 
 200  
     @Override
 201  
     @NotNull(message = "commit is never NULL")
 202  
     public RepoCommit remove(
 203  
         @NotNull(message = "content should not be NULL")
 204  
         final JsonObject content
 205  
     ) throws IOException {
 206  4
         this.storage.lock();
 207  2
         final String path = content.getString("path");
 208  
         // @checkstyle MultipleStringLiterals (20 lines)
 209  
         final String branch;
 210  
         try {
 211  2
             if (content.containsKey("ref")) {
 212  1
                 branch = content.getString("ref");
 213  
             } else {
 214  1
                 branch = "master";
 215  
             }
 216  2
             this.storage.apply(
 217  
                 new Directives()
 218  
                     .xpath(this.xpath())
 219  
                     .xpath(String.format("content[path='%s']", path))
 220  
                     .attr("ref", branch)
 221  
                     .remove()
 222  
             );
 223  2
             return this.commit(content);
 224  
         } finally {
 225  2
             this.storage.unlock();
 226  
         }
 227  
     }
 228  
 
 229  
     /**
 230  
      * Updates a file.
 231  
      * @param path The content path.
 232  
      * @param json JSON object containing updates to the content.
 233  
      * @return Commit related to this update.
 234  
      * @throws IOException If any I/O problem occurs.
 235  
      */
 236  
     @Override
 237  
     @NotNull(message = "updated commit is never NULL")
 238  
     public RepoCommit update(
 239  
         @NotNull(message = "path cannot be NULL") final String path,
 240  
         @NotNull(message = "json should not be NULL") final JsonObject json
 241  
     ) throws IOException {
 242  6
         this.storage.lock();
 243  
         try {
 244  3
             final String ref = "ref";
 245  
             final String branch;
 246  3
             if (json.containsKey(ref)) {
 247  1
                 branch = json.getString(ref);
 248  
             } else {
 249  2
                 branch = "master";
 250  
             }
 251  3
             final String xpath = String.format(
 252  
                 // @checkstyle LineLengthCheck (1 line)
 253  
                 "/github/repos/repo[@coords='%s']/contents/content[path='%s' and @ref='%s']",
 254  
                 this.coords, path, branch
 255  
             );
 256  3
             new JsonPatch(this.storage).patch(xpath, json);
 257  3
             return this.commit(json);
 258  
         } finally {
 259  3
             this.storage.unlock();
 260  
         }
 261  
     }
 262  
 
 263  
     @Override
 264  
     public boolean exists(final String path, final String ref)
 265  
         throws IOException {
 266  4
         return this.storage.xml().nodes(
 267  
             String.format("%s/content[path='%s']", this.xpath(), path)
 268  
         ).size() > 0;
 269  
     }
 270  
 
 271  
     /**
 272  
      * Builder method for MkContent.
 273  
      * @param ref Branch name.
 274  
      * @param path Path to MkContent.
 275  
      * @return MkContent instance.
 276  
      * @throws IOException if any I/O error occurs.
 277  
      */
 278  
     private MkContent mkContent(final String ref, final String path)
 279  
         throws IOException {
 280  2
         return new MkContent(this.storage, this.self, this.coords, path, ref);
 281  
     }
 282  
 
 283  
     /**
 284  
      * XPath of this element in XML tree.
 285  
      * @return The XPath
 286  
      */
 287  
     @NotNull(message = "Xpath is never NULL")
 288  
     private String xpath() {
 289  24
         return String.format(
 290  
             "/github/repos/repo[@coords='%s']/contents",
 291  
             this.coords
 292  
         );
 293  
     }
 294  
 
 295  
     /**
 296  
      * Xpath of the commits element in XML tree.
 297  
      * @return Xpath
 298  
      */
 299  
     @NotNull(message = "commit xpath is never NULL")
 300  
     private String commitXpath() {
 301  24
         return String.format(
 302  
             "/github/repos/repo[@coords='%s']/commits",
 303  
             this.coords
 304  
         );
 305  
     }
 306  
 
 307  
     /**
 308  
      * XML Directives for commit creation.
 309  
      * @param json Source
 310  
      * @return SHA string
 311  
      * @throws IOException If an IO Exception occurs
 312  
      */
 313  
     @NotNull(message = "MkRepoCommit is never NULL")
 314  
     private MkRepoCommit commit(
 315  
         @NotNull(message = "json can't be NULL") final JsonObject json
 316  
     ) throws IOException {
 317  24
         final String sha = fakeSha();
 318  
         // @checkstyle MultipleStringLiterals (40 lines)
 319  24
         final Directives commit = new Directives().xpath(this.commitXpath())
 320  
             .add("commit")
 321  
             .add("sha").set(sha).up()
 322  
             .add("url").set("http://localhost/4").up()
 323  
             .add("html_url").set("http://localhost/5").up()
 324  
             .add("message").set(json.getString("message")).up();
 325  24
         if (json.containsKey("committer")) {
 326  6
             final JsonObject committer = json.getJsonObject("committer");
 327  6
             commit.add("committer")
 328  
                 .add("email").set(committer.getString("email")).up()
 329  
                 .add("name").set(committer.getString("name")).up();
 330  
         }
 331  24
         if (json.containsKey("author")) {
 332  0
             final JsonObject author = json.getJsonObject("author");
 333  0
             commit.add("author")
 334  
                 .add("email").set(author.getString("email")).up()
 335  
                 .add("name").set(author.getString("name")).up();
 336  
         }
 337  24
         this.storage.apply(commit);
 338  24
         return new MkRepoCommit(this.storage, this.repo(), sha);
 339  
     }
 340  
 
 341  
     /**
 342  
      * Generate a random fake SHA hex string.
 343  
      *
 344  
      * @return Fake SHA string.
 345  
      */
 346  
     @NotNull(message = "fake sha can't be NULL")
 347  
     private static String fakeSha() {
 348  
         // @checkstyle MagicNumberCheck (1 line)
 349  43
         return RandomStringUtils.random(40, "0123456789abcdef");
 350  
     }
 351  
 }