Coverage Report - com.jcabi.github.mock.MkTrees
 
Classes in this File Line Coverage Branch Coverage Complexity
MkTrees
88%
24/27
17%
5/28
1.5
MkTrees$AjcClosure1
0%
0/1
N/A
1.5
MkTrees$AjcClosure3
100%
1/1
N/A
1.5
MkTrees$AjcClosure5
100%
1/1
N/A
1.5
MkTrees$AjcClosure7
100%
1/1
N/A
1.5
 
 1  0
 /**
 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  
 
 31  
 package com.jcabi.github.mock;
 32  
 
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.Repo;
 37  
 import com.jcabi.github.Tree;
 38  
 import com.jcabi.github.Trees;
 39  
 import java.io.IOException;
 40  
 import java.util.Map.Entry;
 41  
 import javax.json.JsonArray;
 42  
 import javax.json.JsonObject;
 43  
 import javax.json.JsonValue;
 44  
 import javax.validation.constraints.NotNull;
 45  
 import lombok.EqualsAndHashCode;
 46  
 import org.xembly.Directives;
 47  
 
 48  
 /**
 49  
  * Mock of Github Trees.
 50  
  * @author Alexander Lukashevich (sanai56967@gmail.com)
 51  
  * @version $Id$
 52  
  * @checkstyle MultipleStringLiterals (500 lines)
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 57  
 final class MkTrees implements Trees {
 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's name.
 71  
      */
 72  
     private final transient Coordinates coords;
 73  
     /**
 74  
      * Public constructor.
 75  
      * @param stg The storage.
 76  
      * @param login The login name.
 77  
      * @param rep Repo's coordinates.
 78  
      * @throws IOException If something goes wrong.
 79  
      */
 80  
     MkTrees(
 81  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 82  
         @NotNull(message = "login can't be NULL") final String login,
 83  
         @NotNull(message = "rep can't be NULL") final Coordinates rep
 84  4
     ) throws IOException {
 85  4
         this.storage = stg;
 86  4
         this.self = login;
 87  4
         this.coords = rep;
 88  4
         this.storage.apply(
 89  
             new Directives().xpath(
 90  
                 String.format(
 91  
                     "/github/repos/repo[@coords='%s']/git",
 92  
                     this.coords
 93  
                 )
 94  
             ).addIf("trees")
 95  
         );
 96  4
     }
 97  
     @Override
 98  
     @NotNull(message = "Repository can't be NULL")
 99  
     public Repo repo() {
 100  0
         return new MkRepo(this.storage, this.self, this.coords);
 101  
     }
 102  
 
 103  
     @Override
 104  
     @NotNull(message = "created tree is never NULL")
 105  
     @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 106  
     public Tree create(
 107  
         @NotNull(message = "params can't be NULL") final JsonObject params
 108  
     ) throws IOException {
 109  6
         final JsonArray trees = params.getJsonArray("tree");
 110  3
         for (final JsonValue val : trees) {
 111  3
             final JsonObject tree = (JsonObject) val;
 112  3
             final String sha = tree.getString("sha");
 113  3
             final Directives dirs = new Directives().xpath(this.xpath())
 114  
                 .add("tree");
 115  3
             for (final Entry<String, JsonValue> entry : tree.entrySet()) {
 116  13
                 dirs.add(entry.getKey()).set(entry.getValue().toString()).up();
 117  13
             }
 118  3
             this.storage.apply(dirs);
 119  
             final String ref;
 120  3
             if (tree.containsValue("name")) {
 121  0
                 ref = tree.getString("name");
 122  
             } else {
 123  3
                 ref = sha;
 124  
             }
 125  3
             new MkReferences(this.storage, this.self, this.coords).create(
 126  
                 new StringBuilder("refs/trees/").append(ref).toString(),
 127  
                 sha
 128  
             );
 129  3
         }
 130  3
         return this.get(trees.getJsonObject(0).getString("sha"));
 131  
     }
 132  
 
 133  
     @Override
 134  
     @NotNull(message = "tree is never NULL")
 135  
     public Tree get(@NotNull(message = "sha can't be NULL") final String sha) {
 136  6
         return new MkTree(this.storage, this.self, this.coords, sha);
 137  
     }
 138  
 
 139  
     /**
 140  
      * Gets a tree recursively.
 141  
      * @param sha The tree sha.
 142  
      * @return Trees
 143  
      * @see <a href="https://developer.github.com/v3/git/trees/#get-a-tree-recursively">Trees API</a>
 144  
      */
 145  
     @Override
 146  
     @NotNull(message = "tree is never NULL")
 147  
     public Tree getRec(@NotNull(message = "sha can't be NULL") final String sha
 148  
     ) {
 149  2
         return new MkTree(this.storage, this.self, this.coords, sha);
 150  
     }
 151  
 
 152  
     /**
 153  
      * XPath of this element in XML tree.
 154  
      * @return XPath
 155  
      */
 156  
     @NotNull(message = "Xpath is never NULL")
 157  
     private String xpath() {
 158  3
         return String.format(
 159  
             "/github/repos/repo[@coords='%s']/git/trees",
 160  
             this.coords
 161  
         );
 162  
     }
 163  
 
 164  
 }