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