| 1 | 10 | |
| 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.Release; |
| 37 | |
import com.jcabi.github.ReleaseAsset; |
| 38 | |
import com.jcabi.github.ReleaseAssets; |
| 39 | |
import com.jcabi.xml.XML; |
| 40 | |
import java.io.IOException; |
| 41 | |
import javax.xml.bind.DatatypeConverter; |
| 42 | |
import lombok.EqualsAndHashCode; |
| 43 | |
import lombok.ToString; |
| 44 | |
import org.xembly.Directives; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
@Immutable |
| 54 | |
@Loggable(Loggable.DEBUG) |
| 55 | 0 | @ToString |
| 56 | 0 | @EqualsAndHashCode(of = { "storage", "coords", "rel" }) |
| 57 | |
final class MkReleaseAssets implements ReleaseAssets { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
private final transient MkStorage storage; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
private final transient String self; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
private final transient Coordinates coords; |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
private final transient int rel; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
MkReleaseAssets( |
| 88 | |
final MkStorage stg, |
| 89 | |
final String login, |
| 90 | |
final Coordinates rep, |
| 91 | |
final int number |
| 92 | 12 | ) throws IOException { |
| 93 | 12 | this.storage = stg; |
| 94 | 12 | this.self = login; |
| 95 | 12 | this.coords = rep; |
| 96 | 12 | this.rel = number; |
| 97 | 24 | this.storage.apply( |
| 98 | 12 | new Directives().xpath( |
| 99 | 12 | String.format( |
| 100 | |
|
| 101 | |
"/github/repos/repo[@coords='%s']/releases/release[id='%d']", |
| 102 | 12 | this.coords, this.rel |
| 103 | |
) |
| 104 | 12 | ).addIf("assets") |
| 105 | |
); |
| 106 | 12 | } |
| 107 | |
|
| 108 | |
@Override |
| 109 | |
public Release release() { |
| 110 | 2 | return new MkRelease( |
| 111 | |
this.storage, |
| 112 | |
this.self, |
| 113 | |
this.coords, |
| 114 | |
this.rel |
| 115 | |
); |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public Iterable<ReleaseAsset> iterate() { |
| 120 | 15 | return new MkIterable<ReleaseAsset>( |
| 121 | |
this.storage, |
| 122 | 5 | String.format("%s/asset", this.xpath()), |
| 123 | 9 | new MkIterable.Mapping<ReleaseAsset>() { |
| 124 | |
@Override |
| 125 | |
public ReleaseAsset map(final XML xml) { |
| 126 | 8 | return MkReleaseAssets.this.get( |
| 127 | 4 | Integer.parseInt(xml.xpath("id/text()").get(0)) |
| 128 | |
); |
| 129 | |
} |
| 130 | |
} |
| 131 | |
); |
| 132 | |
} |
| 133 | |
|
| 134 | |
@Override |
| 135 | |
public ReleaseAsset upload( |
| 136 | |
final byte[] content, |
| 137 | |
final String type, |
| 138 | |
final String name |
| 139 | |
) throws IOException { |
| 140 | 22 | this.storage.lock(); |
| 141 | |
final int number; |
| 142 | |
try { |
| 143 | 22 | number = 1 + this.storage.xml().xpath( |
| 144 | 11 | String.format("%s/asset/id/text()", this.xpath()) |
| 145 | 11 | ).size(); |
| 146 | 21 | this.storage.apply( |
| 147 | 11 | new Directives().xpath(this.xpath()).add("asset") |
| 148 | 11 | .add("id").set(Integer.toString(number)).up() |
| 149 | 11 | .add("name").set(name).up() |
| 150 | 22 | .add("content").set( |
| 151 | 11 | DatatypeConverter.printBase64Binary(content) |
| 152 | 11 | ).up() |
| 153 | 11 | .add("content_type").set(type).up() |
| 154 | 11 | .add("size").set(Integer.toString(content.length)).up() |
| 155 | 11 | .add("download_count").set("42").up() |
| 156 | 11 | .add("created_at").set(new Github.Time().toString()).up() |
| 157 | 10 | .add("updated_at").set(new Github.Time().toString()).up() |
| 158 | 10 | .add("url").set("http://localhost/1").up() |
| 159 | 11 | .add("html_url").set("http://localhost/2").up() |
| 160 | |
); |
| 161 | |
} finally { |
| 162 | 11 | this.storage.unlock(); |
| 163 | 11 | } |
| 164 | 11 | return this.get(number); |
| 165 | |
} |
| 166 | |
|
| 167 | |
@Override |
| 168 | |
public ReleaseAsset get(final int number) { |
| 169 | 36 | return new MkReleaseAsset( |
| 170 | |
this.storage, |
| 171 | |
this.self, |
| 172 | |
this.coords, |
| 173 | |
this.rel, |
| 174 | |
number |
| 175 | |
); |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
private String xpath() { |
| 183 | 54 | return String.format( |
| 184 | |
"/github/repos/repo[@coords='%s']/releases/release[id='%d']/assets", |
| 185 | 27 | this.coords, this.rel |
| 186 | |
); |
| 187 | |
} |
| 188 | |
} |