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