| 1 | 2 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.github.mock; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.github.Coordinates; |
| 35 | |
import com.jcabi.github.Label; |
| 36 | |
import com.jcabi.github.Labels; |
| 37 | |
import com.jcabi.github.Repo; |
| 38 | |
import com.jcabi.xml.XML; |
| 39 | |
import java.io.IOException; |
| 40 | |
import lombok.EqualsAndHashCode; |
| 41 | |
import lombok.ToString; |
| 42 | |
import org.xembly.Directives; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
@Immutable |
| 52 | |
@Loggable(Loggable.DEBUG) |
| 53 | 0 | @ToString |
| 54 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords" }) |
| 55 | |
final class MkLabels implements Labels { |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private final transient MkStorage storage; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
private final transient String self; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private final transient Coordinates coords; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
MkLabels(final MkStorage stg, |
| 80 | |
final String login, |
| 81 | |
final Coordinates rep |
| 82 | 15 | ) throws IOException { |
| 83 | 15 | this.storage = stg; |
| 84 | 15 | this.self = login; |
| 85 | 15 | this.coords = rep; |
| 86 | 30 | this.storage.apply( |
| 87 | 15 | new Directives().xpath( |
| 88 | 15 | String.format( |
| 89 | |
"/github/repos/repo[@coords='%s']", |
| 90 | |
this.coords |
| 91 | |
) |
| 92 | 15 | ).addIf("labels") |
| 93 | |
); |
| 94 | 15 | } |
| 95 | |
|
| 96 | |
@Override |
| 97 | |
public Repo repo() { |
| 98 | 0 | return new MkRepo(this.storage, this.self, this.coords); |
| 99 | |
} |
| 100 | |
|
| 101 | |
@Override |
| 102 | |
public Label get(final String name |
| 103 | |
) { |
| 104 | 28 | return new MkLabel(this.storage, this.self, this.coords, name); |
| 105 | |
} |
| 106 | |
|
| 107 | |
@Override |
| 108 | |
public Label create(final String name, |
| 109 | |
final String color |
| 110 | |
) throws IOException { |
| 111 | 16 | if (!color.matches("[0-9a-f]{6}")) { |
| 112 | 0 | throw new IllegalArgumentException( |
| 113 | 0 | String.format( |
| 114 | |
"color '%s' is in wrong format, six hex letters expected", |
| 115 | |
color |
| 116 | |
) |
| 117 | |
); |
| 118 | |
} |
| 119 | 16 | this.storage.apply( |
| 120 | 8 | new Directives().xpath(this.xpath()).add("label") |
| 121 | 8 | .add("name").set(name).up() |
| 122 | 8 | .add("color").set(color).up() |
| 123 | |
); |
| 124 | 8 | return this.get(name); |
| 125 | |
} |
| 126 | |
|
| 127 | |
@Override |
| 128 | |
public Iterable<Label> iterate() { |
| 129 | 9 | return new MkIterable<Label>( |
| 130 | |
this.storage, |
| 131 | 3 | String.format("%s/label", this.xpath()), |
| 132 | 4 | new MkIterable.Mapping<Label>() { |
| 133 | |
@Override |
| 134 | |
public Label map(final XML xml) { |
| 135 | 2 | return MkLabels.this.get( |
| 136 | 1 | xml.xpath("name/text()").get(0) |
| 137 | |
); |
| 138 | |
} |
| 139 | |
} |
| 140 | |
); |
| 141 | |
} |
| 142 | |
|
| 143 | |
@Override |
| 144 | |
public void delete(final String name |
| 145 | |
) throws IOException { |
| 146 | 3 | this.storage.apply( |
| 147 | 1 | new Directives().xpath(this.xpath()) |
| 148 | 1 | .xpath(String.format("label[name='%s']", name)) |
| 149 | 1 | .remove() |
| 150 | 1 | .xpath("/github/repos") |
| 151 | 1 | .xpath(String.format("repo[@coords='%s']", this.coords)) |
| 152 | 1 | .xpath(String.format("issues/issue/labels/label[.='%s']", name)) |
| 153 | 1 | .remove() |
| 154 | |
); |
| 155 | 1 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
private String xpath() { |
| 162 | 12 | return String.format( |
| 163 | |
"/github/repos/repo[@coords='%s']/labels", |
| 164 | |
this.coords |
| 165 | |
); |
| 166 | |
} |
| 167 | |
} |