Coverage Report - com.jcabi.github.mock.MkGist
 
Classes in this File Line Coverage Branch Coverage Complexity
MkGist
85%
61/71
44%
17/38
1.462
MkGist$AjcClosure1
0%
0/1
N/A
1.462
MkGist$AjcClosure11
100%
1/1
N/A
1.462
MkGist$AjcClosure13
100%
1/1
N/A
1.462
MkGist$AjcClosure15
100%
1/1
N/A
1.462
MkGist$AjcClosure17
0%
0/1
N/A
1.462
MkGist$AjcClosure19
0%
0/1
N/A
1.462
MkGist$AjcClosure21
0%
0/1
N/A
1.462
MkGist$AjcClosure3
100%
1/1
N/A
1.462
MkGist$AjcClosure5
100%
1/1
N/A
1.462
MkGist$AjcClosure7
100%
1/1
N/A
1.462
MkGist$AjcClosure9
100%
1/1
N/A
1.462
 
 1  4
 /**
 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.Gist;
 35  
 import com.jcabi.github.GistComments;
 36  
 import com.jcabi.github.Github;
 37  
 import com.jcabi.xml.XML;
 38  
 import java.io.IOException;
 39  
 import java.util.List;
 40  
 import javax.json.JsonObject;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 import org.apache.commons.lang3.StringUtils;
 44  
 import org.xembly.Directives;
 45  
 
 46  
 /**
 47  
  * Mock Github gist.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 50  
  * @version $Id: fab6244753d38021640e27c66733875531543022 $
 51  
  * @since 0.5
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @ToString
 56  1
 @EqualsAndHashCode(of = { "storage", "self", "gist" })
 57  
 @SuppressWarnings("PMD.TooManyMethods")
 58  
 final class MkGist implements Gist {
 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  
      * Gist name.
 72  
      */
 73  
     private final transient String gist;
 74  
 
 75  
     /**
 76  
      * Public ctor.
 77  
      * @param stg Storage
 78  
      * @param login User to login
 79  
      * @param name Gist name
 80  
      * @checkstyle ParameterNumber (5 lines)
 81  
      */
 82  
     MkGist(
 83  
         final MkStorage stg,
 84  
         final String login,
 85  
         final String name
 86  11
     ) {
 87  11
         this.storage = stg;
 88  11
         this.self = login;
 89  11
         this.gist = name;
 90  11
     }
 91  
 
 92  
     @Override
 93  
     public Github github() {
 94  0
         return new MkGithub(this.storage, this.self);
 95  
     }
 96  
 
 97  
     @Override
 98  
     public String identifier() {
 99  2
         return this.gist;
 100  
     }
 101  
 
 102  
     @Override
 103  
     public String read(
 104  
         final String file
 105  
     ) throws IOException {
 106  24
         final List<XML> files = this.storage.xml().nodes(
 107  8
             String.format(
 108  
                 "%s/files/file[filename='%s']",
 109  8
                 this.xpath(), file
 110  
             )
 111  
         );
 112  8
         if (files.isEmpty()) {
 113  0
             throw new IOException(
 114  0
                 String.format("Couldn't find file with the name %s.", file)
 115  
             );
 116  
         }
 117  8
         final List<String> contents = files.get(0)
 118  8
             .xpath("raw_content/text()");
 119  8
         String content = "";
 120  8
         if (!contents.isEmpty()) {
 121  6
             content = contents.get(0);
 122  
         }
 123  8
         return content;
 124  
     }
 125  
 
 126  
     @Override
 127  
     public void write(
 128  
         final String file,
 129  
         final String content
 130  
     )
 131  
         throws IOException {
 132  12
         this.storage.apply(
 133  
             // @checkstyle MultipleStringLiterals (3 lines)
 134  8
             new Directives().xpath(this.xpath()).xpath(
 135  4
                 String.format("files[not(file[filename='%s'])]", file)
 136  4
             ).add("file").add("filename").set(file).up().add("raw_content")
 137  
         );
 138  8
         this.storage.apply(
 139  8
             new Directives().xpath(this.xpath()).xpath(
 140  4
                 String.format(
 141  
                     "files/file[filename='%s']/raw_content",
 142  
                     file
 143  
                 )
 144  4
             ).set(content)
 145  
         );
 146  4
     }
 147  
 
 148  
     /**
 149  
      * Stars.
 150  
      * @throws IOException If there is any I/O problem
 151  
      * @checkstyle MultipleStringLiterals (10 lines)
 152  
      */
 153  
     @Override
 154  
     public void star() throws IOException {
 155  6
         this.storage.apply(
 156  
             new Directives()
 157  2
                 .xpath(this.xpath())
 158  2
                 .attr("starred", Boolean.toString(true))
 159  
         );
 160  2
     }
 161  
 
 162  
     /**
 163  
      * Unstars.
 164  
      * @throws IOException If there is any I/O problem
 165  
      * @checkstyle MultipleStringLiterals (10 lines)
 166  
      */
 167  
     @Override
 168  
     public void unstar() throws IOException {
 169  3
         this.storage.apply(
 170  
             new Directives()
 171  1
                 .xpath(this.xpath())
 172  1
                 .attr("starred", Boolean.toString(false))
 173  
         );
 174  1
     }
 175  
 
 176  
     /**
 177  
      * Checks if starred.
 178  
      * @return True if gist is starred
 179  
      * @throws IOException If there is any I/O problem
 180  
      */
 181  
     @Override
 182  
     public boolean starred() throws IOException {
 183  15
         final List<String> xpath = this.storage.xml().xpath(
 184  5
             String.format("%s/@starred", this.xpath())
 185  
         );
 186  8
         return !xpath.isEmpty() && StringUtils.equalsIgnoreCase(
 187  3
             Boolean.toString(true),
 188  3
             xpath.get(0)
 189  
         );
 190  
     }
 191  
 
 192  
     @Override
 193  
     public Gist fork() throws IOException {
 194  2
         this.storage.lock();
 195  
         final String number;
 196  
         try {
 197  1
             final XML xml = this.storage.xml();
 198  2
             number = Integer.toString(
 199  1
                 1 + xml.xpath("/github/gists/gist/id/text()").size()
 200  
             );
 201  1
             final Directives dirs = new Directives().xpath("/github/gists")
 202  1
                 .add("gist")
 203  1
                 .add("id").set(number).up()
 204  1
                 .add("files");
 205  2
             final List<XML> files = xml.nodes(
 206  1
                 String.format("%s/files/file", this.xpath())
 207  
             );
 208  1
             for (final XML file : files) {
 209  1
                 final String filename = file.xpath("filename/text()").get(0);
 210  
                 // @checkstyle MultipleStringLiterals (3 lines)
 211  1
                 dirs.add("file")
 212  1
                     .add("filename").set(filename).up()
 213  1
                     .add("raw_content").set(this.read(filename)).up().up();
 214  1
             }
 215  1
             this.storage.apply(dirs);
 216  
         } finally {
 217  1
             this.storage.unlock();
 218  1
         }
 219  1
         return new MkGist(this.storage, this.self, number);
 220  
     }
 221  
 
 222  
     @Override
 223  
     public GistComments comments() throws IOException {
 224  0
         throw new UnsupportedOperationException();
 225  
     }
 226  
 
 227  
     @Override
 228  
     public JsonObject json() throws IOException {
 229  0
         return new JsonNode(
 230  0
             this.storage.xml().nodes(this.xpath()).get(0)
 231  0
         ).json();
 232  
     }
 233  
 
 234  
     @Override
 235  
     public void patch(
 236  
         final JsonObject json
 237  
     ) throws IOException {
 238  0
         new JsonPatch(this.storage).patch(this.xpath(), json);
 239  0
     }
 240  
 
 241  
     /**
 242  
      * XPath of this element in XML tree.
 243  
      * @return XPath
 244  
      */
 245  
     private String xpath() {
 246  25
         return String.format(
 247  
             "/github/gists/gist[id='%s']",
 248  
             this.gist
 249  
         );
 250  
     }
 251  
 
 252  
 }