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