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