| 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.aspects.Loggable; |
| 34 | |
import com.jcabi.github.CommitsComparison; |
| 35 | |
import com.jcabi.github.Coordinates; |
| 36 | |
import com.jcabi.github.RepoCommit; |
| 37 | |
import com.jcabi.github.RepoCommits; |
| 38 | |
import com.jcabi.xml.XML; |
| 39 | |
import java.io.IOException; |
| 40 | |
import java.util.Map; |
| 41 | |
import javax.json.JsonObject; |
| 42 | |
import lombok.EqualsAndHashCode; |
| 43 | |
import lombok.ToString; |
| 44 | |
import org.apache.commons.lang3.StringUtils; |
| 45 | |
import org.xembly.Directives; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
@Immutable |
| 53 | |
@Loggable(Loggable.DEBUG) |
| 54 | 0 | @ToString |
| 55 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords" }) |
| 56 | |
final class MkRepoCommits implements RepoCommits { |
| 57 | |
|
| 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 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
MkRepoCommits( |
| 81 | |
final MkStorage stg, |
| 82 | |
final String login, |
| 83 | |
final Coordinates repo |
| 84 | 7 | ) throws IOException { |
| 85 | 7 | this.storage = stg; |
| 86 | 7 | this.self = login; |
| 87 | 7 | this.coords = repo; |
| 88 | 13 | this.storage.apply( |
| 89 | 6 | new Directives().xpath( |
| 90 | 7 | String.format("/github/repos/repo[@coords='%s']", this.coords) |
| 91 | 6 | ).addIf("commits") |
| 92 | |
); |
| 93 | 7 | } |
| 94 | |
|
| 95 | |
@Override |
| 96 | |
public Iterable<RepoCommit> iterate( |
| 97 | |
final Map<String, String> params |
| 98 | |
) { |
| 99 | 3 | return new MkIterable<RepoCommit>( |
| 100 | 1 | this.storage, String.format("%s/commit", this.xpath()), |
| 101 | 1 | new MkIterable.Mapping<RepoCommit>() { |
| 102 | |
@Override |
| 103 | |
public RepoCommit map(final XML xml) { |
| 104 | 0 | return MkRepoCommits.this.get( |
| 105 | 0 | xml.xpath("sha/text()").get(0) |
| 106 | |
); |
| 107 | |
} |
| 108 | |
} |
| 109 | |
); |
| 110 | |
} |
| 111 | |
|
| 112 | |
@Override |
| 113 | |
public RepoCommit get( |
| 114 | |
final String sha |
| 115 | |
) { |
| 116 | 2 | return new MkRepoCommit( |
| 117 | |
this.storage, new MkRepo(this.storage, this.self, this.coords), sha |
| 118 | |
); |
| 119 | |
} |
| 120 | |
|
| 121 | |
@Override |
| 122 | |
public CommitsComparison compare( |
| 123 | |
final String base, |
| 124 | |
final String head |
| 125 | |
) { |
| 126 | 2 | return new MkCommitsComparison(this.storage, this.self, this.coords); |
| 127 | |
} |
| 128 | |
|
| 129 | |
@Override |
| 130 | |
public String diff( |
| 131 | |
final String base, |
| 132 | |
final String head |
| 133 | |
) { |
| 134 | 3 | return |
| 135 | 1 | String.format( |
| 136 | |
"%s%sindex %s..%s", |
| 137 | |
"diff --git a/README b/README", |
| 138 | 1 | System.getProperty("line.separator"), base, head |
| 139 | |
); |
| 140 | |
} |
| 141 | |
|
| 142 | |
@Override |
| 143 | |
public String patch( |
| 144 | |
final String base, |
| 145 | |
final String head |
| 146 | |
) { |
| 147 | 3 | return StringUtils.join( |
| 148 | 1 | String.format("From %s Mon Sep 17 00:00:00 2001\n", head), |
| 149 | |
"From: Some Author <some_author@email.com>\n", |
| 150 | |
"Date: Tue, 11 Feb 2014 20:33:49 +0200\n", |
| 151 | |
"Subject: Some subject\n", "\n", "---\n", |
| 152 | |
" .../java/com/jcabi/github/CommitsComparison.java | 6 +-\n", |
| 153 | |
" src/main/java/com/jcabi/github/RepoCommit.java | 131 +++++", |
| 154 | |
"++++++++++++++++\n", |
| 155 | |
" src/main/java/com/jcabi/github/RepoCommits.java | 15 +--\n", |
| 156 | |
" src/main/java/com/jcabi/github/RtRepoCommit.java | 110 +++++", |
| 157 | |
"++++++++++++\n", |
| 158 | |
" src/main/java/com/jcabi/github/RtRepoCommits.java | 6 +-\n", |
| 159 | |
" .../java/com/jcabi/github/mock/MkRepoCommits.java | 6 +-\n", |
| 160 | |
" src/test/java/com/jcabi/github/RepoCommitTest.java | 84 +++++", |
| 161 | |
"++++++++\n", |
| 162 | |
" .../java/com/jcabi/github/RtRepoCommitsITCase.java | 7 +-\n", |
| 163 | |
" 8 files changed, 346 insertions(+), 19 deletions(-)\n", |
| 164 | |
" create mode 100644 src/main/java/com/jcabi/github/", |
| 165 | |
"RepoCommit.java\n", |
| 166 | |
" create mode 100644 src/main/java/com/jcabi/github/RtRepoCommit", |
| 167 | |
".java\n", |
| 168 | |
" create mode 100644 src/test/java/com/jcabi/github/", |
| 169 | |
"RepoCommitTest.java" |
| 170 | |
); |
| 171 | |
} |
| 172 | |
|
| 173 | |
@Override |
| 174 | |
public JsonObject json() throws IOException { |
| 175 | 0 | return new JsonNode( |
| 176 | 0 | this.storage.xml().nodes(this.xpath()).get(0) |
| 177 | 0 | ).json(); |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
private String xpath() { |
| 185 | 1 | return String.format( |
| 186 | |
"/github/repos/repo[@coords='%s']/commits", |
| 187 | |
this.coords |
| 188 | |
); |
| 189 | |
} |
| 190 | |
} |