1 | 322 | |
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.Issue; |
37 | |
import com.jcabi.github.Issues; |
38 | |
import com.jcabi.github.Repo; |
39 | |
import com.jcabi.github.Search; |
40 | |
import com.jcabi.log.Logger; |
41 | |
import com.jcabi.xml.XML; |
42 | |
import java.io.IOException; |
43 | |
import java.util.EnumMap; |
44 | |
import java.util.HashMap; |
45 | |
import java.util.Map; |
46 | |
import lombok.EqualsAndHashCode; |
47 | |
import lombok.ToString; |
48 | |
import org.xembly.Directives; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
@Immutable |
58 | |
@Loggable(Loggable.DEBUG) |
59 | 0 | @ToString |
60 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords" }) |
61 | |
final class MkIssues implements Issues { |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private final transient MkStorage storage; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private final transient String self; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private final transient Coordinates coords; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
MkIssues( |
86 | |
final MkStorage stg, |
87 | |
final String login, |
88 | |
final Coordinates rep |
89 | 168 | ) throws IOException { |
90 | 168 | this.storage = stg; |
91 | 168 | this.self = login; |
92 | 168 | this.coords = rep; |
93 | 335 | this.storage.apply( |
94 | 166 | new Directives().xpath( |
95 | 167 | String.format( |
96 | |
"/github/repos/repo[@coords='%s']", |
97 | |
this.coords |
98 | |
) |
99 | 166 | ).addIf("issues") |
100 | |
); |
101 | 168 | } |
102 | |
|
103 | |
@Override |
104 | |
public Repo repo() { |
105 | 322 | return new MkRepo(this.storage, this.self, this.coords); |
106 | |
} |
107 | |
|
108 | |
@Override |
109 | |
public Issue get(final int number) { |
110 | 538 | return new MkIssue(this.storage, this.self, this.coords, number); |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public Issue create(final String title, |
115 | |
final String body |
116 | |
) |
117 | |
throws IOException { |
118 | 322 | this.storage.lock(); |
119 | |
final int number; |
120 | |
try { |
121 | 322 | number = 1 + this.storage.xml().xpath( |
122 | 161 | String.format("%s/issue/number/text()", this.xpath()) |
123 | 161 | ).size(); |
124 | 322 | this.storage.apply( |
125 | 161 | new Directives().xpath(this.xpath()).add("issue") |
126 | 161 | .add("number").set(Integer.toString(number)).up() |
127 | 161 | .add("state").set(Issue.OPEN_STATE).up() |
128 | 161 | .add("title").set(title).up() |
129 | 161 | .add("body").set(body).up() |
130 | 161 | .add("created_at").set(new Github.Time().toString()).up() |
131 | 161 | .add("updated_at").set(new Github.Time().toString()).up() |
132 | 161 | .add("url").set("http://localhost/1").up() |
133 | 161 | .add("html_url").set("http://localhost/2").up() |
134 | 161 | .add("user").add("login").set(this.self).up().up() |
135 | |
); |
136 | |
} finally { |
137 | 161 | this.storage.unlock(); |
138 | 161 | } |
139 | 322 | Logger.info( |
140 | |
this, "issue #%d created in %s by %s: %[text]s", |
141 | 161 | number, this.repo().coordinates(), this.self, title |
142 | |
); |
143 | 161 | return this.get(number); |
144 | |
} |
145 | |
|
146 | |
@Override |
147 | |
public Iterable<Issue> iterate(final Map<String, String> params |
148 | |
) { |
149 | 6 | return new MkIterable<Issue>( |
150 | |
this.storage, |
151 | 2 | String.format("%s/issue", this.xpath()), |
152 | 105 | new MkIterable.Mapping<Issue>() { |
153 | |
@Override |
154 | |
public Issue map(final XML xml) { |
155 | 206 | return MkIssues.this.get( |
156 | 103 | Integer.parseInt(xml.xpath("number/text()").get(0)) |
157 | |
); |
158 | |
} |
159 | |
} |
160 | |
); |
161 | |
} |
162 | |
|
163 | |
@Override |
164 | |
@SuppressWarnings("PMD.UseConcurrentHashMap") |
165 | |
public Iterable<Issue> search( |
166 | |
final Sort sort, |
167 | |
final Search.Order direction, |
168 | |
final EnumMap<Qualifier, String> qualifiers) |
169 | |
throws IOException { |
170 | 0 | final Map<String, String> params = new HashMap<String, String>(); |
171 | 0 | for (final EnumMap.Entry<Qualifier, String> entry : qualifiers |
172 | 0 | .entrySet()) { |
173 | 0 | params.put(entry.getKey().identifier(), entry.getValue()); |
174 | 0 | } |
175 | 0 | params.put("sort", sort.identifier()); |
176 | 0 | params.put("direction", direction.identifier()); |
177 | 0 | return this.iterate(params); |
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
private String xpath() { |
185 | 324 | return String.format( |
186 | |
"/github/repos/repo[@coords='%s']/issues", |
187 | |
this.coords |
188 | |
); |
189 | |
} |
190 | |
|
191 | |
} |