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