Coverage Report - com.jcabi.github.mock.MkMilestone
 
Classes in this File Line Coverage Branch Coverage Complexity
MkMilestone
41%
10/24
0%
0/22
1.222
 
 1  1
 /**
 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.Coordinates;
 34  
 import com.jcabi.github.Milestone;
 35  
 import com.jcabi.github.Repo;
 36  
 import java.io.IOException;
 37  
 import javax.json.JsonObject;
 38  
 import javax.validation.constraints.NotNull;
 39  
 
 40  
 /**
 41  
  * Mock Github milestone.
 42  
  * @author Mihai Andronache (amihaiemil@gmail.com)
 43  
  * @version $Id$
 44  
  * @checkstyle MultipleStringLiterals (500 lines)
 45  
  */
 46  1
 @Immutable
 47  
 final class MkMilestone implements Milestone {
 48  
 
 49  
     /**
 50  
      * Storage.
 51  
      */
 52  
     private final transient MkStorage storage;
 53  
 
 54  
     /**
 55  
      * Login of the user logged in.
 56  
      */
 57  
     private final transient String self;
 58  
 
 59  
     /**
 60  
      * Repo name.
 61  
      */
 62  
     private final transient Coordinates coords;
 63  
 
 64  
     /**
 65  
      * The number of the MkMilestone.
 66  
      */
 67  
     private final transient int code;
 68  
 
 69  
     /**
 70  
      * MkMilestone constructor.
 71  
      * @param strg The storage
 72  
      * @param login The user to login with
 73  
      * @param crds The repo
 74  
      * @param num The number of the MkMilestone
 75  
      * @checkstyle ParameterNumber (5 lines)
 76  
      */
 77  
     MkMilestone(
 78  
         @NotNull(message = "strg can't be NULL") final MkStorage strg,
 79  
         @NotNull(message = "login can't be NULL") final String login,
 80  
         @NotNull(message = "crds can't be NULL") final Coordinates crds,
 81  
         final int num
 82  9
     ) {
 83  9
         this.self = login;
 84  9
         this.coords = crds;
 85  9
         this.storage = strg;
 86  9
         this.code = num;
 87  9
     }
 88  
 
 89  
     @Override
 90  
     public boolean equals(
 91  
         @NotNull(message = "obj should not be NULL") final Object obj
 92  
     ) {
 93  0
         return obj instanceof Milestone
 94  
             && this.code == Milestone.class.cast(obj).number();
 95  
     }
 96  
 
 97  
     @Override
 98  
     public int hashCode() {
 99  0
         return this.code;
 100  
     }
 101  
 
 102  
     @Override
 103  
     public int compareTo(
 104  
         @NotNull(message = "milestone can't be NULL") final Milestone milestone
 105  
     ) {
 106  0
         assert this.self != null;
 107  0
         assert this.coords != null;
 108  0
         assert this.storage != null;
 109  0
         assert this.code != -1;
 110  0
         int result = this.coords.compareTo(
 111  
             milestone.repo().coordinates()
 112  
         );
 113  0
         if (result == 0) {
 114  0
             result = Integer.valueOf(this.code).compareTo(milestone.number());
 115  
         }
 116  0
         return result;
 117  
     }
 118  
 
 119  
     @Override
 120  
     @NotNull(message = "JSON is never NULL")
 121  
     public JsonObject json() throws IOException {
 122  0
         return new JsonNode(
 123  
             this.storage.xml().nodes(this.xpath()).get(0)
 124  
         ).json();
 125  
     }
 126  
 
 127  
     @Override
 128  
     public void patch(
 129  
         @NotNull(message = "json can't be NULL") final JsonObject json
 130  
     ) throws IOException {
 131  0
         new JsonPatch(
 132  
             this.storage
 133  
         ).patch(
 134  
             this.xpath(),
 135  
             json
 136  
         );
 137  0
     }
 138  
 
 139  
     @Override
 140  
     @NotNull(message = "repo is never NULL")
 141  
     public Repo repo() {
 142  1
         return new MkRepo(
 143  
             this.storage,
 144  
             this.self,
 145  
             this.coords
 146  
         );
 147  
     }
 148  
 
 149  
     @Override
 150  
     public int number() {
 151  2
         return this.code;
 152  
     }
 153  
 
 154  
     /**
 155  
      * XPath of this element in XML tree.
 156  
      * @return XPath
 157  
      */
 158  
     @NotNull(message = "Xpath is never NULL")
 159  
     private String xpath() {
 160  0
         return String.format(
 161  
             "/github/repos/repo[@coords='%s']/milestones[number='%d']",
 162  
             this.coords,
 163  
             this.code
 164  
         );
 165  
     }
 166  
 
 167  
 }