Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MkPublicKeys |
|
| 1.3333333333333333;1.333 | ||||
MkPublicKeys$1 |
|
| 1.3333333333333333;1.333 | ||||
MkPublicKeys$AjcClosure1 |
|
| 1.3333333333333333;1.333 | ||||
MkPublicKeys$AjcClosure3 |
|
| 1.3333333333333333;1.333 | ||||
MkPublicKeys$AjcClosure5 |
|
| 1.3333333333333333;1.333 | ||||
MkPublicKeys$AjcClosure7 |
|
| 1.3333333333333333;1.333 | ||||
MkPublicKeys$AjcClosure9 |
|
| 1.3333333333333333;1.333 |
1 | 0 | /** |
2 | * Copyright (c) 2013-2017, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
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.PublicKey; | |
35 | import com.jcabi.github.PublicKeys; | |
36 | import com.jcabi.github.User; | |
37 | import com.jcabi.xml.XML; | |
38 | import java.io.IOException; | |
39 | import lombok.EqualsAndHashCode; | |
40 | import lombok.ToString; | |
41 | import org.xembly.Directives; | |
42 | ||
43 | /** | |
44 | * Mock github public keys. | |
45 | * | |
46 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
47 | * @version $Id: a9aa15961520d78cdcc7dec870f62b76c2205fef $ | |
48 | * @checkstyle MultipleStringLiteralsCheck (200 lines) | |
49 | */ | |
50 | @Immutable | |
51 | @Loggable(Loggable.DEBUG) | |
52 | 0 | @ToString |
53 | 0 | @EqualsAndHashCode(of = { "storage", "self" }) |
54 | final class MkPublicKeys implements PublicKeys { | |
55 | ||
56 | /** | |
57 | * Storage. | |
58 | */ | |
59 | private final transient MkStorage storage; | |
60 | ||
61 | /** | |
62 | * Login of the user logged in. | |
63 | */ | |
64 | private final transient String self; | |
65 | ||
66 | /** | |
67 | * Public ctor. | |
68 | * @param stg Storage | |
69 | * @param login User to login | |
70 | * @throws IOException If there is any I/O problem | |
71 | */ | |
72 | public MkPublicKeys( | |
73 | final MkStorage stg, | |
74 | final String login | |
75 | 6 | ) throws IOException { |
76 | 6 | this.storage = stg; |
77 | 6 | this.self = login; |
78 | 12 | this.storage.apply( |
79 | 6 | new Directives().xpath(this.userXpath()).addIf("keys") |
80 | ); | |
81 | 6 | } |
82 | ||
83 | @Override | |
84 | public User user() { | |
85 | try { | |
86 | 0 | return new MkUser(this.storage, this.self); |
87 | 0 | } catch (final IOException ex) { |
88 | 0 | throw new IllegalStateException(ex); |
89 | } | |
90 | } | |
91 | ||
92 | @Override | |
93 | public Iterable<PublicKey> iterate() { | |
94 | 9 | return new MkIterable<PublicKey>( |
95 | this.storage, | |
96 | 3 | String.format("%s/key", this.xpath()), |
97 | 5 | new MkIterable.Mapping<PublicKey>() { |
98 | @Override | |
99 | public PublicKey map(final XML xml) { | |
100 | 4 | return MkPublicKeys.this.get( |
101 | 2 | Integer.parseInt(xml.xpath("id/text()").get(0)) |
102 | ); | |
103 | } | |
104 | } | |
105 | ); | |
106 | } | |
107 | ||
108 | @Override | |
109 | public PublicKey get(final int number) { | |
110 | 18 | return new MkPublicKey(this.storage, this.self, number); |
111 | } | |
112 | ||
113 | @Override | |
114 | public PublicKey create( | |
115 | final String title, | |
116 | final String key | |
117 | ) throws IOException { | |
118 | 10 | this.storage.lock(); |
119 | final int number; | |
120 | try { | |
121 | 10 | number = 1 + this.storage.xml().xpath( |
122 | 5 | String.format("%s/key/id/text()", this.xpath()) |
123 | 5 | ).size(); |
124 | 10 | this.storage.apply( |
125 | 5 | new Directives().xpath(this.xpath()) |
126 | 5 | .add("key") |
127 | 5 | .add("id").set(String.valueOf(number)).up() |
128 | 5 | .add("title").set(title).up() |
129 | 5 | .add("key").set(key) |
130 | ); | |
131 | } finally { | |
132 | 5 | this.storage.unlock(); |
133 | 5 | } |
134 | 5 | return this.get(number); |
135 | } | |
136 | ||
137 | @Override | |
138 | public void remove(final int number) throws IOException { | |
139 | 3 | this.storage.apply( |
140 | 1 | new Directives().xpath( |
141 | 1 | String.format("%s/key[id='%d']", this.xpath(), number) |
142 | 1 | ).remove() |
143 | ); | |
144 | 1 | } |
145 | ||
146 | /** | |
147 | * XPath of user element in XML tree. | |
148 | * @return XPath | |
149 | */ | |
150 | private String userXpath() { | |
151 | 20 | return String.format("/github/users/user[login='%s']", this.self); |
152 | } | |
153 | ||
154 | /** | |
155 | * XPath of user element in XML tree. | |
156 | * @return XPath | |
157 | */ | |
158 | private String xpath() { | |
159 | 14 | return String.format("%s/keys", this.userXpath()); |
160 | } | |
161 | } |