Coverage Report - com.jcabi.github.mock.MkGithub
 
Classes in this File Line Coverage Branch Coverage Complexity
MkGithub
56%
23/41
0%
0/20
1.706
MkGithub$AjcClosure1
100%
1/1
N/A
1.706
MkGithub$AjcClosure11
100%
1/1
N/A
1.706
MkGithub$AjcClosure13
0%
0/1
N/A
1.706
MkGithub$AjcClosure15
100%
1/1
N/A
1.706
MkGithub$AjcClosure17
100%
1/1
N/A
1.706
MkGithub$AjcClosure19
0%
0/1
N/A
1.706
MkGithub$AjcClosure21
100%
1/1
N/A
1.706
MkGithub$AjcClosure23
100%
1/1
N/A
1.706
MkGithub$AjcClosure25
100%
1/1
N/A
1.706
MkGithub$AjcClosure3
100%
1/1
N/A
1.706
MkGithub$AjcClosure5
100%
1/1
N/A
1.706
MkGithub$AjcClosure7
100%
1/1
N/A
1.706
MkGithub$AjcClosure9
100%
1/1
N/A
1.706
 
 1  0
 /**
 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.aspects.Tv;
 35  
 import com.jcabi.github.Gists;
 36  
 import com.jcabi.github.Github;
 37  
 import com.jcabi.github.Gitignores;
 38  
 import com.jcabi.github.Limits;
 39  
 import com.jcabi.github.Markdown;
 40  
 import com.jcabi.github.Organizations;
 41  
 import com.jcabi.github.Repo;
 42  
 import com.jcabi.github.Repos;
 43  
 import com.jcabi.github.Search;
 44  
 import com.jcabi.github.Users;
 45  
 import com.jcabi.http.Request;
 46  
 import com.jcabi.http.request.FakeRequest;
 47  
 import java.io.IOException;
 48  
 import java.net.HttpURLConnection;
 49  
 import javax.json.Json;
 50  
 import javax.json.JsonObject;
 51  
 import lombok.EqualsAndHashCode;
 52  
 import org.apache.commons.lang3.RandomStringUtils;
 53  
 
 54  
 /**
 55  
  * Mock Github client.
 56  
  *
 57  
  * <p>This is how you use it:
 58  
  *
 59  
  * <pre> GitHub gitHub = new MkGithub("username");
 60  
  * Repos.RepoCreate create = new Repos.RepoCreate("dummy", false);
 61  
  * Repo repo = gitHub.repos().create(create);
 62  
  * Issue issue = repo.issues().create("title", "body");</pre>
 63  
  *
 64  
  * <p>By default, it works with a temporary file, which will be deleted
 65  
  * on JVM exit:
 66  
  *
 67  
  * <pre> Github github = new MkGithub("jeff");</pre>
 68  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 69  
  * @version $Id: 1f4224cd0372e0def85e351cbdd0103a81a5c70b $
 70  
  * @since 0.5
 71  
  * @checkstyle ClassDataAbstractionCoupling (500 lines)
 72  
  */
 73  
 @Immutable
 74  
 @Loggable(Loggable.DEBUG)
 75  0
 @EqualsAndHashCode(of = { "storage", "self" })
 76  
 @SuppressWarnings("PMD.TooManyMethods")
 77  
 public final class MkGithub implements Github {
 78  
 
 79  
     /**
 80  
      * Storage.
 81  
      */
 82  
     private final transient MkStorage storage;
 83  
 
 84  
     /**
 85  
      * Login of the user logged in.
 86  
      */
 87  
     private final transient String self;
 88  
 
 89  
     /**
 90  
      * Public ctor.
 91  
      * @throws IOException If there is any I/O problem
 92  
      */
 93  
     public MkGithub() throws IOException {
 94  252
         this("jeff");
 95  252
     }
 96  
 
 97  
     /**
 98  
      * Public ctor.
 99  
      * @param login User to login
 100  
      * @throws IOException If there is any I/O problem
 101  
      */
 102  
     public MkGithub(
 103  
         final String login
 104  
     ) throws IOException {
 105  284
         this(new MkStorage.Synced(new MkStorage.InFile()), login);
 106  284
     }
 107  
 
 108  
     /**
 109  
      * Public ctor.
 110  
      * @param stg Storage
 111  
      * @param login User to login
 112  
      */
 113  
     public MkGithub(
 114  
         final MkStorage stg,
 115  
         final String login
 116  330
     ) {
 117  330
         this.storage = stg;
 118  330
         this.self = login;
 119  329
     }
 120  
 
 121  
     @Override
 122  
     public String toString() {
 123  0
         return this.storage.toString();
 124  
     }
 125  
 
 126  
     @Override
 127  
     public Request entry() {
 128  3
         return new FakeRequest()
 129  1
             .withBody("{}")
 130  1
             .withStatus(HttpURLConnection.HTTP_OK);
 131  
     }
 132  
 
 133  
     @Override
 134  
     public Repos repos() {
 135  
         try {
 136  473
             return new MkRepos(this.storage, this.self);
 137  0
         } catch (final IOException ex) {
 138  0
             throw new IllegalStateException(ex);
 139  
         }
 140  
     }
 141  
 
 142  
     @Override
 143  
     public Gists gists() {
 144  
         try {
 145  16
             return new MkGists(this.storage, this.self);
 146  0
         } catch (final IOException ex) {
 147  0
             throw new IllegalStateException(ex);
 148  
         }
 149  
     }
 150  
 
 151  
     @Override
 152  
     public Users users() {
 153  
         try {
 154  88
             return new MkUsers(this.storage, this.self);
 155  0
         } catch (final IOException ex) {
 156  0
             throw new IllegalStateException(ex);
 157  
         }
 158  
     }
 159  
 
 160  
     @Override
 161  
     public Organizations organizations() {
 162  
         try {
 163  6
             return new MkOrganizations(this.storage);
 164  0
         } catch (final IOException ex) {
 165  0
             throw new IllegalStateException(ex);
 166  
         }
 167  
     }
 168  
 
 169  
     @Override
 170  
     public Limits limits() {
 171  2
         return new MkLimits(this.storage, this.self);
 172  
     }
 173  
 
 174  
     @Override
 175  
     public JsonObject meta() {
 176  0
         return Json.createObjectBuilder()
 177  0
             .add("hooks", Json.createArrayBuilder().build())
 178  0
             .add("git", Json.createArrayBuilder().build())
 179  0
             .build();
 180  
     }
 181  
 
 182  
     @Override
 183  
     public Search search() {
 184  8
         return new MkSearch(this.storage, this.self);
 185  
     }
 186  
 
 187  
     @Override
 188  
     public Gitignores gitignores() throws IOException {
 189  4
         return new MkGitignores(this);
 190  
     }
 191  
 
 192  
     @Override
 193  
     public JsonObject emojis() {
 194  0
         return Json.createObjectBuilder()
 195  0
             .add("+1", "http://locahost/up")
 196  0
             .add("-1", "http://locahost/down")
 197  0
             .build();
 198  
     }
 199  
 
 200  
     @Override
 201  
     public Markdown markdown() {
 202  4
         return new MkMarkdown(this);
 203  
     }
 204  
 
 205  
     /**
 206  
      * Relogin.
 207  
      * @param login User to login
 208  
      * @return Github
 209  
      * @throws IOException If there is any I/O problem
 210  
      */
 211  
     public Github relogin(final String login
 212  
     ) throws IOException {
 213  4
         return new MkGithub(this.storage, login);
 214  
     }
 215  
 
 216  
     /**
 217  
      * Create repo with random name.
 218  
      * @return Repo
 219  
      * @throws IOException If fails
 220  
      */
 221  
     public Repo randomRepo() throws IOException {
 222  633
         return this.repos().create(
 223  
             new Repos.RepoCreate(
 224  213
                 RandomStringUtils.randomAlphanumeric(Tv.TWENTY),
 225  
                 true
 226  
             )
 227  
         );
 228  
     }
 229  
 }