| 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.github.Coordinates; |
| 34 | |
import com.jcabi.github.Milestone; |
| 35 | |
import com.jcabi.github.Milestones; |
| 36 | |
import com.jcabi.github.Repo; |
| 37 | |
import com.jcabi.xml.XML; |
| 38 | |
import java.io.IOException; |
| 39 | |
import java.util.Map; |
| 40 | |
import org.xembly.Directives; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
@Immutable |
| 48 | |
final class MkMilestones implements Milestones { |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
private final transient MkStorage storage; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private final transient String self; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private final transient Coordinates coords; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
MkMilestones( |
| 73 | |
final MkStorage stg, |
| 74 | |
final String login, |
| 75 | |
final Coordinates rep |
| 76 | 6 | ) throws IOException { |
| 77 | 6 | this.storage = stg; |
| 78 | 6 | this.self = login; |
| 79 | 6 | this.coords = rep; |
| 80 | 12 | this.storage.apply( |
| 81 | 6 | new Directives().xpath( |
| 82 | 6 | String.format("/github/repos/repo[@coords='%s']", this.coords) |
| 83 | 6 | ).addIf("milestones") |
| 84 | |
); |
| 85 | 6 | } |
| 86 | |
@Override |
| 87 | |
public Repo repo() { |
| 88 | 1 | return new MkRepo(this.storage, this.self, this.coords); |
| 89 | |
} |
| 90 | |
|
| 91 | |
@Override |
| 92 | |
public Milestone create( |
| 93 | |
final String title |
| 94 | |
) throws IOException { |
| 95 | |
final int number; |
| 96 | 10 | number = 1 + this.storage.xml().xpath( |
| 97 | 5 | String.format("%s/milestone/number/text()", this.xpath()) |
| 98 | 5 | ).size(); |
| 99 | 10 | this.storage.apply( |
| 100 | 5 | new Directives().xpath(this.xpath()).add("milestone") |
| 101 | 5 | .add("number").set(Integer.toString(number)).up() |
| 102 | 5 | .add("title").set(title).up() |
| 103 | 5 | .add("state").set(Milestone.OPEN_STATE).up() |
| 104 | 5 | .add("description").set("mock milestone").up() |
| 105 | |
); |
| 106 | 5 | return this.get(number); |
| 107 | |
} |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public Milestone get(final int number) { |
| 111 | 8 | return new MkMilestone(this.storage, this.self, this.coords, number); |
| 112 | |
} |
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public Iterable<Milestone> iterate( |
| 116 | |
final Map<String, String> params |
| 117 | |
) { |
| 118 | 6 | return new MkIterable<Milestone>( |
| 119 | |
this.storage, |
| 120 | 3 | String.format("%s/milestone", this.xpath()), |
| 121 | 5 | new MkIterable.Mapping<Milestone>() { |
| 122 | |
@Override |
| 123 | |
public Milestone map(final XML xml) { |
| 124 | 4 | return MkMilestones.this.get( |
| 125 | 2 | Integer.parseInt(xml.xpath("number/text()").get(0)) |
| 126 | |
); |
| 127 | |
} |
| 128 | |
} |
| 129 | |
); |
| 130 | |
} |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public void remove(final int number) throws IOException { |
| 134 | 2 | this.storage.apply( |
| 135 | 1 | new Directives().xpath( |
| 136 | 1 | String.format("%s/milestone[number='%d']", this.xpath(), number) |
| 137 | 1 | ).remove() |
| 138 | |
); |
| 139 | 1 | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
private String xpath() { |
| 146 | 14 | return String.format( |
| 147 | |
"/github/repos/repo[@coords='%s']/milestones", |
| 148 | |
this.coords |
| 149 | |
); |
| 150 | |
} |
| 151 | |
} |