Coverage Report - com.jcabi.github.mock.MkTags
 
Classes in this File Line Coverage Branch Coverage Complexity
MkTags
88%
16/18
8%
2/24
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  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.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 javax.validation.constraints.NotNull;
 44  
 import lombok.EqualsAndHashCode;
 45  
 import org.xembly.Directives;
 46  
 
 47  
 /**
 48  
  * Mock of Github Tags.
 49  
  * @author Mihai Andronache (amihaiemil@gmail.com)
 50  
  * @version $Id$
 51  
  * @checkstyle MultipleStringLiterals (500 lines)
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 56  
 final class MkTags implements Tags {
 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  
     MkTags(
 80  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 81  
         @NotNull(message = "login can't be NULL") final String login,
 82  
         @NotNull(message = "rep can't be NULL") final Coordinates rep
 83  2
     ) throws IOException {
 84  2
         this.storage = stg;
 85  2
         this.self = login;
 86  2
         this.coords = rep;
 87  2
         this.storage.apply(
 88  
             new Directives().xpath(
 89  
                 String.format(
 90  
                     "/github/repos/repo[@coords='%s']/git",
 91  
                     this.coords
 92  
                 )
 93  
             ).addIf("tags")
 94  
         );
 95  2
     }
 96  
     @Override
 97  
     @NotNull(message = "Repository can't be NULL")
 98  
     public Repo repo() {
 99  0
         return new MkRepo(this.storage, this.self, this.coords);
 100  
     }
 101  
 
 102  
     @Override
 103  
     @NotNull(message = "created tag is never NULL")
 104  
     public Tag create(
 105  
         @NotNull(message = "params can't be NULL") final JsonObject params
 106  
     ) throws IOException {
 107  4
         final Directives dirs = new Directives().xpath(this.xpath()).add("tag");
 108  2
         for (final Entry<String, JsonValue> entry : params.entrySet()) {
 109  7
             dirs.add(entry.getKey()).set(entry.getValue().toString()).up();
 110  7
         }
 111  2
         this.storage.apply(dirs);
 112  2
         new MkReferences(this.storage, this.self, this.coords).create(
 113  
             new StringBuilder().append("refs/tags/").append(
 114  
                 params.getString("name")
 115  
             ).toString(),
 116  
             params.getString("sha")
 117  
         );
 118  2
         return this.get(params.getString("sha"));
 119  
     }
 120  
 
 121  
     @Override
 122  
     @NotNull(message = "tag is never NULL")
 123  
     public Tag get(@NotNull(message = "sha can't be NULL") final String sha) {
 124  4
         return new MkTag(this.storage, this.self, this.coords, sha);
 125  
     }
 126  
 
 127  
     /**
 128  
      * XPath of this element in XML tree.
 129  
      * @return XPath
 130  
      */
 131  
     @NotNull(message = "Xpath is never NULL")
 132  
     private String xpath() {
 133  2
         return String.format(
 134  
             "/github/repos/repo[@coords='%s']/git/tags",
 135  
             this.coords
 136  
         );
 137  
     }
 138  
 
 139  
 }