Coverage Report - com.jcabi.github.mock.MkReference
 
Classes in this File Line Coverage Branch Coverage Complexity
MkReference
92%
13/14
0%
0/28
1
MkReference$AjcClosure1
100%
1/1
N/A
1
MkReference$AjcClosure3
100%
1/1
N/A
1
MkReference$AjcClosure5
100%
1/1
N/A
1
MkReference$AjcClosure7
100%
1/1
N/A
1
 
 1  2
 /**
 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.Reference;
 37  
 import com.jcabi.github.Repo;
 38  
 import java.io.IOException;
 39  
 import javax.json.JsonObject;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import lombok.EqualsAndHashCode;
 42  
 
 43  
 /**
 44  
  * Mock of Github Reference.
 45  
  * @author Mihai Andronache (amihaiemil@gmail.com)
 46  
  * @version $Id$
 47  
  */
 48  
 @Immutable
 49  
 @Loggable(Loggable.DEBUG)
 50  0
 @EqualsAndHashCode(of = { "storage", "self", "coords", "name" })
 51  
 final class MkReference implements Reference {
 52  
 
 53  
     /**
 54  
      * Storage.
 55  
      */
 56  
     private final transient MkStorage storage;
 57  
 
 58  
     /**
 59  
      * Login of the user logged in.
 60  
      */
 61  
     private final transient String self;
 62  
 
 63  
     /**
 64  
      * Repo name.
 65  
      */
 66  
     private final transient Coordinates coords;
 67  
 
 68  
     /**
 69  
      * The Reference's name.
 70  
      */
 71  
     private final transient String name;
 72  
 
 73  
     /**
 74  
      * Public constructor.
 75  
      * @param strg Storage.
 76  
      * @param login Login name.
 77  
      * @param crds Repo coordinates.
 78  
      * @param reference Name of the reference.
 79  
      * @checkstyle ParameterNumber (5 lines)
 80  
      */
 81  
     MkReference(
 82  
         @NotNull(message = "strg can't be NULL") final MkStorage strg,
 83  
         @NotNull(message = "login can't be NULL") final String login,
 84  
         @NotNull(message = "crds can't be NULL") final Coordinates crds,
 85  
         @NotNull(message = "reference can't be NULL") final String reference
 86  27
     ) {
 87  27
         this.storage = strg;
 88  27
         this.self = login;
 89  27
         this.coords = crds;
 90  27
         this.name = reference;
 91  27
     }
 92  
 
 93  
     @Override
 94  
     @NotNull(message = "Repository is never NULL")
 95  
     public Repo repo() {
 96  2
         return new MkRepo(this.storage, this.self, this.coords);
 97  
     }
 98  
 
 99  
     @Override
 100  
     @NotNull(message = "Ref is never NULL")
 101  
     public String ref() {
 102  2
         return this.name;
 103  
     }
 104  
 
 105  
     @Override
 106  
     @NotNull(message = "JSON is never NULL")
 107  
     public JsonObject json() throws IOException {
 108  4
         return new JsonNode(
 109  
             this.storage.xml().nodes(this.xpath()).get(0)
 110  
         ).json();
 111  
     }
 112  
 
 113  
     @Override
 114  
     public void patch(
 115  
         @NotNull(message = "json can't be NULL") final JsonObject json
 116  
     ) throws IOException {
 117  2
         new JsonPatch(this.storage).patch(this.xpath(), json);
 118  1
     }
 119  
 
 120  
     /**
 121  
      * XPath of this element in XML tree.
 122  
      *
 123  
      * @return XPath
 124  
      */
 125  
     @NotNull(message = "Xpath is never NULL")
 126  
     private String xpath() {
 127  3
         return String.format(
 128  
             "/github/repos/repo[@coords='%s']/git/refs/reference[ref='%s']",
 129  
             this.coords, this.name
 130  
         );
 131  
     }
 132  
 }