Coverage Report - com.jcabi.github.mock.MkContent
 
Classes in this File Line Coverage Branch Coverage Complexity
MkContent
73%
14/19
35%
12/34
1
MkContent$AjcClosure1
0%
0/1
N/A
1
MkContent$AjcClosure11
100%
1/1
N/A
1
MkContent$AjcClosure3
0%
0/1
N/A
1
MkContent$AjcClosure5
100%
1/1
N/A
1
MkContent$AjcClosure7
100%
1/1
N/A
1
MkContent$AjcClosure9
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  
 package com.jcabi.github.mock;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.github.Content;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.Repo;
 37  
 import java.io.ByteArrayInputStream;
 38  
 import java.io.IOException;
 39  
 import java.io.InputStream;
 40  
 import javax.json.JsonObject;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import javax.xml.bind.DatatypeConverter;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.apache.commons.lang3.builder.CompareToBuilder;
 46  
 
 47  
 /**
 48  
  * Mock Github content.
 49  
  *
 50  
  * @author Andres Candal (andres.candal@rollasolution.com)
 51  
  * @version $Id$
 52  
  * @since 0.8
 53  
  */
 54  0
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @ToString
 57  2
 @EqualsAndHashCode(of = { "storage", "self", "coords", "location", "branch" })
 58  
 final class MkContent implements Content {
 59  
 
 60  
     /**
 61  
      * Storage.
 62  
      */
 63  
     private final transient MkStorage storage;
 64  
 
 65  
     /**
 66  
      * Login of the user logged in.
 67  
      */
 68  
     private final transient String self;
 69  
 
 70  
     /**
 71  
      * Repo name.
 72  
      */
 73  
     private final transient Coordinates coords;
 74  
 
 75  
     /**
 76  
      * Path of this content.
 77  
      */
 78  
     private final transient String location;
 79  
 
 80  
     /**
 81  
      * Branch of this content.
 82  
      */
 83  
     private final transient String branch;
 84  
 
 85  
     /**
 86  
      * Public ctor.
 87  
      * @param stg Storage
 88  
      * @param login User to login
 89  
      * @param rep Repo
 90  
      * @param path Path of this file
 91  
      * @param ref Branch of this file
 92  
      * @throws IOException If there is any I/O problem
 93  
      * @checkstyle ParameterNumberCheck (6 lines)
 94  
      */
 95  
     public MkContent(
 96  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 97  
         @NotNull(message = "login can't be NULL") final String login,
 98  
         @NotNull(message = "rep can't be NULL") final Coordinates rep,
 99  
         @NotNull(message = "path can't be NULL") final String path,
 100  
         @NotNull(message = "ref can't be NULL") final String ref
 101  27
     ) throws IOException {
 102  27
         this.storage = stg;
 103  27
         this.self = login;
 104  27
         this.coords = rep;
 105  27
         this.location = path;
 106  27
         this.branch = ref;
 107  27
     }
 108  
 
 109  
     @Override
 110  
     public int compareTo(
 111  
         @NotNull(message = "cont should not be NULL") final Content cont
 112  
     ) {
 113  0
         return new CompareToBuilder()
 114  
             .append(this.path(), cont.path())
 115  
                 .append(this.repo().coordinates(), cont.repo().coordinates())
 116  
             .build();
 117  
     }
 118  
 
 119  
     @Override
 120  
     public void patch(
 121  
         @NotNull(message = "JSON is never NULL") final JsonObject json)
 122  
         throws IOException {
 123  0
         new JsonPatch(this.storage).patch(this.xpath(), json);
 124  0
     }
 125  
 
 126  
     @Override
 127  
     @NotNull(message = "JSON is never NULL")
 128  
     public JsonObject json() throws IOException {
 129  28
         return new JsonNode(
 130  
             this.storage.xml().nodes(this.xpath()).get(0)
 131  
         ).json();
 132  
     }
 133  
 
 134  
     @Override
 135  
     @NotNull(message = "repo is never NULL")
 136  
     public Repo repo() {
 137  2
         return new MkRepo(this.storage, this.self, this.coords);
 138  
     }
 139  
 
 140  
     @Override
 141  
     @NotNull(message = "path is never NULL")
 142  
     public String path() {
 143  8
         return this.location;
 144  
     }
 145  
 
 146  
     @Override
 147  
     @NotNull(message = "input stream is never NULL")
 148  
     public InputStream raw() throws IOException {
 149  2
         return new ByteArrayInputStream(
 150  
             DatatypeConverter.parseBase64Binary(
 151  
                 this.storage.xml().xpath(
 152  
                     String.format("%s/content/text()", this.xpath())
 153  
                 ).get(0)
 154  
             )
 155  
         );
 156  
     }
 157  
 
 158  
     /**
 159  
      * XPath of this element in XML tree.
 160  
      * @return The XPath
 161  
      */
 162  
     @NotNull(message = "Xpath is never NULL")
 163  
     private String xpath() {
 164  15
         return String.format(
 165  
             // @checkstyle LineLength (1 line)
 166  
             "/github/repos/repo[@coords='%s']/contents/content[path='%s' and @ref='%s']",
 167  
             this.coords, this.location, this.branch
 168  
         );
 169  
     }
 170  
 }