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