Coverage Report - com.jcabi.github.mock.MkTags
 
Classes in this File Line Coverage Branch Coverage Complexity
MkTags
92%
23/25
6%
2/30
1.2
MkTags$AjcClosure1
0%
0/1
N/A
1.2
MkTags$AjcClosure3
100%
1/1
N/A
1.2
MkTags$AjcClosure5
100%
1/1
N/A
1.2
 
 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.Tag;
 38  
 import com.jcabi.github.Tags;
 39  
 import java.io.IOException;
 40  
 import java.util.Map.Entry;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonValue;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import org.xembly.Directives;
 45  
 
 46  
 /**
 47  
  * Mock of Github Tags.
 48  
  * @author Mihai Andronache (amihaiemil@gmail.com)
 49  
  * @version $Id: b77897f55ea390ac1553e114284f0011a4ebc196 $
 50  
  * @checkstyle MultipleStringLiterals (500 lines)
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 55  
 final class MkTags implements Tags {
 56  
 
 57  
     /**
 58  
      * Storage.
 59  
      */
 60  
     private final transient MkStorage storage;
 61  
 
 62  
     /**
 63  
      * Login of the user logged in.
 64  
      */
 65  
     private final transient String self;
 66  
 
 67  
     /**
 68  
      * Repo's name.
 69  
      */
 70  
     private final transient Coordinates coords;
 71  
     /**
 72  
      * Public constructor.
 73  
      * @param stg The storage.
 74  
      * @param login The login name.
 75  
      * @param rep Repo's coordinates.
 76  
      * @throws IOException If something goes wrong.
 77  
      */
 78  
     MkTags(
 79  
         final MkStorage stg,
 80  
         final String login,
 81  
         final Coordinates rep
 82  2
     ) throws IOException {
 83  2
         this.storage = stg;
 84  2
         this.self = login;
 85  2
         this.coords = rep;
 86  4
         this.storage.apply(
 87  2
             new Directives().xpath(
 88  2
                 String.format(
 89  
                     "/github/repos/repo[@coords='%s']/git",
 90  
                     this.coords
 91  
                 )
 92  2
             ).addIf("tags")
 93  
         );
 94  2
     }
 95  
     @Override
 96  
     public Repo repo() {
 97  0
         return new MkRepo(this.storage, this.self, this.coords);
 98  
     }
 99  
 
 100  
     @Override
 101  
     public Tag create(
 102  
         final JsonObject params
 103  
     ) throws IOException {
 104  4
         final Directives dirs = new Directives().xpath(this.xpath()).add("tag");
 105  2
         for (final Entry<String, JsonValue> entry : params.entrySet()) {
 106  7
             dirs.add(entry.getKey()).set(entry.getValue().toString()).up();
 107  7
         }
 108  2
         this.storage.apply(dirs);
 109  4
         new MkReferences(this.storage, this.self, this.coords).create(
 110  4
             new StringBuilder().append("refs/tags/").append(
 111  2
                 params.getString("name")
 112  2
             ).toString(),
 113  2
             params.getString("sha")
 114  
         );
 115  2
         return this.get(params.getString("sha"));
 116  
     }
 117  
 
 118  
     @Override
 119  
     public Tag get(final String sha) {
 120  4
         return new MkTag(this.storage, this.self, this.coords, sha);
 121  
     }
 122  
 
 123  
     /**
 124  
      * XPath of this element in XML tree.
 125  
      * @return XPath
 126  
      */
 127  
     private String xpath() {
 128  2
         return String.format(
 129  
             "/github/repos/repo[@coords='%s']/git/tags",
 130  
             this.coords
 131  
         );
 132  
     }
 133  
 
 134  
 }