| 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.Github; |
| 36 | |
import com.jcabi.github.Repo; |
| 37 | |
import com.jcabi.github.Repos; |
| 38 | |
import com.jcabi.log.Logger; |
| 39 | |
import com.jcabi.xml.XML; |
| 40 | |
import java.io.IOException; |
| 41 | |
import lombok.EqualsAndHashCode; |
| 42 | |
import lombok.ToString; |
| 43 | |
import org.xembly.Directives; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
@Immutable |
| 54 | |
@Loggable(Loggable.DEBUG) |
| 55 | 0 | @ToString |
| 56 | 0 | @EqualsAndHashCode(of = { "storage", "self" }) |
| 57 | 4 | final class MkRepos implements Repos { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private final transient MkStorage storage; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private final transient String self; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
MkRepos( |
| 76 | |
final MkStorage stg, |
| 77 | |
final String login |
| 78 | 245 | ) throws IOException { |
| 79 | 248 | this.storage = stg; |
| 80 | 248 | this.self = login; |
| 81 | 248 | this.storage.apply(new Directives().xpath("/github").addIf("repos")); |
| 82 | 245 | } |
| 83 | |
|
| 84 | |
@Override |
| 85 | |
public Github github() { |
| 86 | 0 | return new MkGithub(this.storage, this.self); |
| 87 | |
} |
| 88 | |
|
| 89 | |
@Override |
| 90 | |
public Repo create( |
| 91 | |
final RepoCreate settings |
| 92 | |
) throws IOException { |
| 93 | 474 | final Coordinates coords = new Coordinates.Simple( |
| 94 | |
this.self, |
| 95 | 237 | settings.name() |
| 96 | |
); |
| 97 | 479 | this.storage.apply( |
| 98 | 235 | new Directives().xpath(this.xpath()).add("repo") |
| 99 | 236 | .attr("coords", coords.toString()) |
| 100 | 236 | .add("name").set(settings.name()).up() |
| 101 | 236 | .add("description").set("test repository").up() |
| 102 | 237 | .add("private").set(settings.isPrivate()).up() |
| 103 | |
); |
| 104 | 239 | final Repo repo = this.get(coords); |
| 105 | 240 | repo.patch(settings.json()); |
| 106 | 241 | Logger.info( |
| 107 | |
this, "repository %s created by %s", |
| 108 | |
coords, this.self |
| 109 | |
); |
| 110 | 241 | return repo; |
| 111 | |
} |
| 112 | |
|
| 113 | |
@Override |
| 114 | |
public Repo get( |
| 115 | |
final Coordinates coords |
| 116 | |
) { |
| 117 | |
try { |
| 118 | 747 | final String xpath = String.format( |
| 119 | 249 | "%s/repo[@coords='%s']", this.xpath(), coords |
| 120 | |
); |
| 121 | 251 | if (this.storage.xml().nodes(xpath).isEmpty()) { |
| 122 | 1 | throw new IllegalArgumentException( |
| 123 | 1 | String.format("repository %s doesn't exist", coords) |
| 124 | |
); |
| 125 | |
} |
| 126 | 0 | } catch (final IOException ex) { |
| 127 | 0 | throw new IllegalStateException(ex); |
| 128 | 250 | } |
| 129 | 250 | return new MkRepo(this.storage, this.self, coords); |
| 130 | |
} |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public void remove( |
| 134 | |
final Coordinates coords) { |
| 135 | |
try { |
| 136 | 3 | this.storage.apply( |
| 137 | 1 | new Directives().xpath( |
| 138 | 1 | String.format("%s/repo[@coords='%s']", this.xpath(), coords) |
| 139 | 1 | ).remove() |
| 140 | |
); |
| 141 | 0 | } catch (final IOException ex) { |
| 142 | 0 | throw new IllegalStateException(ex); |
| 143 | 1 | } |
| 144 | 1 | } |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
@Override |
| 152 | |
public Iterable<Repo> iterate( |
| 153 | |
final String identifier) { |
| 154 | 2 | return new MkIterable<Repo>( |
| 155 | |
this.storage, |
| 156 | |
"/github/repos/repo", |
| 157 | 3 | new MkIterable.Mapping<Repo>() { |
| 158 | |
@Override |
| 159 | |
public Repo map(final XML xml) { |
| 160 | 4 | return new MkRepo( |
| 161 | 2 | MkRepos.this.storage, MkRepos.this.self, |
| 162 | 2 | new Coordinates.Simple(xml.xpath("@coords").get(0)) |
| 163 | |
); |
| 164 | |
} |
| 165 | |
} |
| 166 | |
); |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
private String xpath() { |
| 174 | 486 | return "/github/repos"; |
| 175 | |
} |
| 176 | |
|
| 177 | |
} |