Coverage Report - com.jcabi.github.mock.MkBlobs
 
Classes in this File Line Coverage Branch Coverage Complexity
MkBlobs
88%
16/18
0%
0/22
1
 
 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.github.Blob;
 34  
 import com.jcabi.github.Blobs;
 35  
 import com.jcabi.github.Coordinates;
 36  
 import com.jcabi.github.Repo;
 37  
 import java.io.IOException;
 38  
 import javax.validation.constraints.NotNull;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import org.apache.commons.lang3.RandomStringUtils;
 41  
 import org.xembly.Directives;
 42  
 
 43  
 /**
 44  
  * Mock Github blobs.
 45  
  *
 46  
  * @author Alexander Lukashevich (sanai56967@gmail.com)
 47  
  * @version $Id$
 48  
  */
 49  
 @Immutable
 50  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 51  
 final class MkBlobs implements Blobs {
 52  
 
 53  
     /**
 54  
      * Storage.
 55  
      */
 56  
     private final transient MkStorage storage;
 57  
 
 58  
     /**
 59  
      * Login of the user logged in.
 60  
      */
 61  
     private final transient String self;
 62  
 
 63  
     /**
 64  
      * Repo name.
 65  
      */
 66  
     private final transient Coordinates coords;
 67  
 
 68  
     /**
 69  
      * Public ctor.
 70  
      * @param stg Storage
 71  
      * @param login User to login
 72  
      * @param rep Repo
 73  
      * @throws java.io.IOException If there is any I/O problem
 74  
      */
 75  
     public MkBlobs(
 76  
         @NotNull(message = "stg is never NULL") final MkStorage stg,
 77  
         @NotNull(message = "login is never NULL") final String login,
 78  
         @NotNull(message = "rep is never NULL") final Coordinates rep
 79  2
     ) throws IOException {
 80  2
         this.storage = stg;
 81  2
         this.self = login;
 82  2
         this.coords = rep;
 83  2
         this.storage.apply(
 84  
             new Directives().xpath(
 85  
                 String.format(
 86  
                     "/github/repos/repo[@coords='%s']/git",
 87  
                     this.coords
 88  
                 )
 89  
             ).addIf("blobs")
 90  
         );
 91  2
     }
 92  
 
 93  
     @Override
 94  
     @NotNull(message = "repo is never NULL")
 95  
     public Repo repo() {
 96  0
         return new MkRepo(this.storage, this.self, this.coords);
 97  
     }
 98  
     /**
 99  
      * Gets a mocked Blob.
 100  
      * @param sha Blob sha
 101  
      * @return Mocked Blob
 102  
      */
 103  
     @NotNull(message = "blob is never NULL")
 104  
     public Blob get(
 105  
         @NotNull(message = "Sha can't be NULL") final String sha) {
 106  4
         return new MkBlob(this.storage, sha, this.coords);
 107  
     }
 108  
     @Override
 109  
     @NotNull(message = "created blob is never NULL")
 110  
     public Blob create(
 111  
         @NotNull(message = "Content can't be NULL") final String content,
 112  
         @NotNull(message = "Encoding can't be NULL") final String encoding)
 113  
         throws IOException {
 114  2
         this.storage.lock();
 115  2
         final String sha = fakeSha();
 116  
         try {
 117  2
             this.storage.apply(
 118  
                 new Directives().xpath(this.xpath()).add("blob")
 119  
                     .add("sha").set(sha).up()
 120  
                     .add("url").set("http://localhost/1").up()
 121  
                     .attr("content", content)
 122  
                     .attr("encoding", encoding)
 123  
             );
 124  
         } finally {
 125  2
             this.storage.unlock();
 126  2
         }
 127  2
         return this.get(sha);
 128  
     }
 129  
 
 130  
     /**
 131  
      * XPath of this element in XML tree.
 132  
      * @return XPath
 133  
      */
 134  
     @NotNull(message = "Xpath is never NULL")
 135  
     private String xpath() {
 136  2
         return String.format(
 137  
             "/github/repos/repo[@coords='%s']/git/blobs",
 138  
             this.coords
 139  
         );
 140  
     }
 141  
 
 142  
     /**
 143  
      * Generate a random fake SHA hex string.
 144  
      *
 145  
      * @return Fake SHA string.
 146  
      */
 147  
     private static String fakeSha() {
 148  
         // @checkstyle MagicNumberCheck (1 line)
 149  2
         return RandomStringUtils.random(40, "0123456789abcdef");
 150  
     }
 151  
 
 152  
 }