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