Coverage Report - com.jcabi.github.mock.MkGists
 
Classes in this File Line Coverage Branch Coverage Complexity
MkGists
92%
35/38
9%
2/22
1.125
MkGists$1
100%
2/2
N/A
1.125
MkGists$AjcClosure1
0%
0/1
N/A
1.125
MkGists$AjcClosure3
100%
1/1
N/A
1.125
MkGists$AjcClosure5
100%
1/1
N/A
1.125
MkGists$AjcClosure7
100%
1/1
N/A
1.125
MkGists$AjcClosure9
100%
1/1
N/A
1.125
 
 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.Gist;
 35  
 import com.jcabi.github.Gists;
 36  
 import com.jcabi.github.Github;
 37  
 import com.jcabi.xml.XML;
 38  
 import java.io.IOException;
 39  
 import java.util.Map;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 import org.xembly.Directives;
 43  
 
 44  
 /**
 45  
  * Mock Github gists.
 46  
  *
 47  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 48  
  * @version $Id: 0588078401bdb93b9a47ca145267b34380c3d1bc $
 49  
  * @since 0.5
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self" })
 55  
 final class MkGists implements Gists {
 56  
 
 57  
     /**
 58  
      * Storage.
 59  
      */
 60  
     private final transient MkStorage storage;
 61  
 
 62  
     /**
 63  
      * Login of the user logged in.
 64  
      */
 65  
     private final transient String self;
 66  
 
 67  
     /**
 68  
      * Public ctor.
 69  
      * @param stg Storage
 70  
      * @param login User to login
 71  
      * @throws IOException If there is any I/O problem
 72  
      */
 73  
     MkGists(
 74  
         final MkStorage stg,
 75  
         final String login
 76  8
     ) throws IOException {
 77  8
         this.storage = stg;
 78  8
         this.self = login;
 79  16
         this.storage.apply(
 80  8
             new Directives().xpath("/github").addIf("gists")
 81  
         );
 82  8
     }
 83  
 
 84  
     @Override
 85  
     public Github github() {
 86  0
         return new MkGithub(this.storage, this.self);
 87  
     }
 88  
 
 89  
     @Override
 90  
     public Gist create(
 91  
         final Map<String, String> files, final boolean visible
 92  
     ) throws IOException {
 93  18
         this.storage.lock();
 94  
         final String number;
 95  
         try {
 96  18
             number = Integer.toString(
 97  18
                 1 + this.storage.xml().xpath(
 98  9
                     String.format("%s/gist/id/text()", this.xpath())
 99  9
                 ).size()
 100  
             );
 101  9
             final Directives dirs = new Directives().xpath(this.xpath())
 102  9
                 .add("gist")
 103  9
                 .add("id").set(number).up()
 104  9
                 .add("public").set(String.valueOf(visible)).up()
 105  9
                 .add("files");
 106  9
             for (final Map.Entry<String, String> file : files.entrySet()) {
 107  9
                 dirs.add("file")
 108  9
                     .add("filename").set(file.getKey()).up()
 109  9
                     .add("raw_content").set(file.getValue()).up().up();
 110  9
             }
 111  9
             this.storage.apply(dirs);
 112  
         } finally {
 113  9
             this.storage.unlock();
 114  9
         }
 115  9
         return this.get(number);
 116  
     }
 117  
 
 118  
     @Override
 119  
     public Gist get(final String name
 120  
     ) {
 121  20
         return new MkGist(this.storage, this.self, name);
 122  
     }
 123  
 
 124  
     @Override
 125  
     public Iterable<Gist> iterate() {
 126  6
         return new MkIterable<Gist>(
 127  
             this.storage,
 128  2
             String.format("%s/gist", this.xpath()),
 129  3
             new MkIterable.Mapping<Gist>() {
 130  
                 @Override
 131  
                 public Gist map(final XML xml) {
 132  1
                     return MkGists.this.get(xml.xpath("id/text()").get(0));
 133  
                 }
 134  
             }
 135  
         );
 136  
     }
 137  
 
 138  
     @Override
 139  
     public void remove(final String identifier
 140  
     ) throws IOException {
 141  3
         this.storage.apply(
 142  1
             new Directives().xpath(
 143  1
                 String.format("%s/gist[id='%s']", this.xpath(), identifier)
 144  1
             ).remove()
 145  
         );
 146  1
     }
 147  
 
 148  
     /**
 149  
      * XPath of this element in XML tree.
 150  
      * @return XPath
 151  
      */
 152  
     private String xpath() {
 153  21
         return "/github/gists";
 154  
     }
 155  
 
 156  
 }