| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MkPullComment |
|
| 1.0;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.Coordinates; | |
| 34 | import com.jcabi.github.Pull; | |
| 35 | import com.jcabi.github.PullComment; | |
| 36 | import java.io.IOException; | |
| 37 | import javax.json.JsonObject; | |
| 38 | import lombok.EqualsAndHashCode; | |
| 39 | import lombok.ToString; | |
| 40 | ||
| 41 | /** | |
| 42 | * Mock Github pull comment. | |
| 43 | * | |
| 44 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
| 45 | * @version $Id: 2cde027561025d841ed1c4caaeaef518d86313f7 $ | |
| 46 | */ | |
| 47 | 0 | @Immutable |
| 48 | 0 | @ToString |
| 49 | 0 | @EqualsAndHashCode(of = { "storage", "repo", "owner", "num" }) |
| 50 | final class MkPullComment implements PullComment { | |
| 51 | /** | |
| 52 | * Storage. | |
| 53 | */ | |
| 54 | private final transient MkStorage storage; | |
| 55 | ||
| 56 | /** | |
| 57 | * Repo name. | |
| 58 | */ | |
| 59 | private final transient Coordinates repo; | |
| 60 | ||
| 61 | /** | |
| 62 | * Owner of comments. | |
| 63 | */ | |
| 64 | private final transient Pull owner; | |
| 65 | ||
| 66 | /** | |
| 67 | * Comment number. | |
| 68 | */ | |
| 69 | private final transient int num; | |
| 70 | ||
| 71 | /** | |
| 72 | * Public ctor. | |
| 73 | * @param stg Storage | |
| 74 | * @param rep Repo | |
| 75 | * @param pull Pull | |
| 76 | * @param number Comment number | |
| 77 | * @checkstyle ParameterNumber (5 lines) | |
| 78 | */ | |
| 79 | MkPullComment( | |
| 80 | final MkStorage stg, | |
| 81 | final Coordinates rep, | |
| 82 | final Pull pull, | |
| 83 | final int number | |
| 84 | 21 | ) { |
| 85 | 21 | this.storage = stg; |
| 86 | 21 | this.repo = rep; |
| 87 | 21 | this.owner = pull; |
| 88 | 21 | this.num = number; |
| 89 | 21 | } |
| 90 | ||
| 91 | @Override | |
| 92 | public JsonObject json() throws IOException { | |
| 93 | 8 | return new JsonNode( |
| 94 | 4 | this.storage.xml().nodes(this.xpath()).get(0) |
| 95 | 4 | ).json(); |
| 96 | } | |
| 97 | ||
| 98 | @Override | |
| 99 | public void patch( | |
| 100 | final JsonObject json | |
| 101 | ) throws IOException { | |
| 102 | 2 | new JsonPatch(this.storage).patch(this.xpath(), json); |
| 103 | 2 | } |
| 104 | ||
| 105 | @Override | |
| 106 | public Pull pull() { | |
| 107 | 0 | return this.owner; |
| 108 | } | |
| 109 | ||
| 110 | @Override | |
| 111 | public int number() { | |
| 112 | 5 | return this.num; |
| 113 | } | |
| 114 | ||
| 115 | @Override | |
| 116 | public int compareTo( | |
| 117 | final PullComment other | |
| 118 | ) { | |
| 119 | 0 | return this.number() - other.number(); |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * XPath of this element in XML tree. | |
| 124 | * @return XPath | |
| 125 | */ | |
| 126 | private String xpath() { | |
| 127 | 12 | return String.format( |
| 128 | // @checkstyle LineLength (1 line) | |
| 129 | "/github/repos/repo[@coords='%s']/pulls/pull[number='%d']/comments/comment[id='%d']", | |
| 130 | 6 | this.repo, this.owner.number(), this.num |
| 131 | ); | |
| 132 | } | |
| 133 | } |