1 | 12 | |
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.Github; |
35 | |
import com.jcabi.github.Notifications; |
36 | |
import com.jcabi.github.PublicKeys; |
37 | |
import com.jcabi.github.User; |
38 | |
import com.jcabi.github.UserEmails; |
39 | |
import com.jcabi.github.UserOrganizations; |
40 | |
import com.jcabi.xml.XML; |
41 | |
import java.io.IOException; |
42 | |
import java.util.Date; |
43 | |
import javax.json.Json; |
44 | |
import javax.json.JsonObject; |
45 | |
import lombok.EqualsAndHashCode; |
46 | |
import lombok.ToString; |
47 | |
import org.xembly.Directives; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
@Immutable |
58 | |
@Loggable(Loggable.DEBUG) |
59 | 3 | @ToString |
60 | 6 | @EqualsAndHashCode(of = { "storage", "self" }) |
61 | |
final class MkUser implements User { |
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 | |
|
77 | |
|
78 | |
|
79 | 56 | MkUser(final MkStorage stg, final String login) throws IOException { |
80 | 56 | this.storage = stg; |
81 | 56 | this.self = login; |
82 | 112 | this.storage.apply( |
83 | |
new Directives() |
84 | 56 | .xpath( |
85 | 56 | String.format( |
86 | |
"/github/users[not(user[login='%s'])]", login |
87 | |
) |
88 | |
) |
89 | 56 | .add("user") |
90 | 56 | .add("login").set(login).up() |
91 | 56 | .add("type").set("User").up() |
92 | 56 | .add("name").set(login).up() |
93 | 56 | .add("notifications").up() |
94 | |
); |
95 | 56 | } |
96 | |
|
97 | |
@Override |
98 | |
public Github github() { |
99 | 0 | return new MkGithub(this.storage, this.self); |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public String login() { |
104 | 76 | return this.self; |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
public UserOrganizations organizations() { |
109 | |
try { |
110 | 4 | return new MkUserOrganizations(this.storage, this.self); |
111 | 0 | } catch (final IOException ex) { |
112 | 0 | throw new IllegalStateException(ex); |
113 | |
} |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public PublicKeys keys() { |
118 | |
try { |
119 | 11 | return new MkPublicKeys(this.storage, this.self); |
120 | 0 | } catch (final IOException ex) { |
121 | 0 | throw new IllegalStateException(ex); |
122 | |
} |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public UserEmails emails() { |
127 | |
try { |
128 | 8 | return new MkUserEmails(this.storage, this.self); |
129 | 0 | } catch (final IOException ex) { |
130 | 0 | throw new IllegalStateException(ex); |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public Notifications notifications() { |
136 | 3 | return new MkNotifications( |
137 | |
this.storage, |
138 | 1 | this.xpath().concat("/notifications/notification") |
139 | |
); |
140 | |
} |
141 | |
|
142 | |
@Override |
143 | |
public void markAsRead(final Date lastread) throws IOException { |
144 | 3 | final Iterable<XML> ids = this.storage.xml().nodes( |
145 | 2 | this.xpath() + String.format( |
146 | |
"/notifications/notification[date <= %s]/id", |
147 | 1 | lastread.getTime() |
148 | |
) |
149 | |
); |
150 | 1 | final JsonPatch json = new JsonPatch(this.storage); |
151 | 1 | final JsonObject read = Json.createObjectBuilder() |
152 | 1 | .add("read", true).build(); |
153 | 1 | for (final XML nid : ids) { |
154 | 2 | json.patch( |
155 | 1 | String.format( |
156 | 1 | this.xpath().concat("/notifications/notification[id = %s]"), |
157 | 1 | nid.xpath("text()").get(0) |
158 | |
), |
159 | |
read |
160 | |
); |
161 | 1 | } |
162 | 1 | } |
163 | |
|
164 | |
@Override |
165 | |
public void patch( |
166 | |
final JsonObject json |
167 | |
) throws IOException { |
168 | 0 | new JsonPatch(this.storage).patch(this.xpath(), json); |
169 | 0 | } |
170 | |
|
171 | |
@Override |
172 | |
public JsonObject json() throws IOException { |
173 | 0 | return new JsonNode( |
174 | 0 | this.storage.xml().nodes(this.xpath()).get(0) |
175 | 0 | ).json(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private String xpath() { |
183 | 3 | return String.format("/github/users/user[login='%s']", this.self); |
184 | |
} |
185 | |
|
186 | |
} |