Skip to content

Commit 0d57c51

Browse files
authored
Merge pull request #1850 from bibonix/fix-1145-mkcommits-persistence
#1145: persist commit fields in MkCommits.create
2 parents 5a6a832 + 981cb7c commit 0d57c51

2 files changed

Lines changed: 110 additions & 1 deletion

File tree

src/main/java/com/jcabi/github/mock/MkCommits.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import com.jcabi.github.Repo;
1313
import com.jcabi.github.Statuses;
1414
import jakarta.json.JsonObject;
15+
import jakarta.json.JsonString;
16+
import jakarta.json.JsonValue;
1517
import java.io.IOException;
18+
import java.util.Map;
1619
import lombok.EqualsAndHashCode;
1720
import org.xembly.Directives;
1821

@@ -74,7 +77,28 @@ public Repo repo() {
7477
@Override
7578
public Commit create(
7679
final JsonObject params
77-
) {
80+
) throws IOException {
81+
final Directives dirs = new Directives().xpath(this.xpath()).add("commit");
82+
for (final Map.Entry<String, JsonValue> entry : params.entrySet()) {
83+
final JsonValue value = entry.getValue();
84+
if (value.getValueType() == JsonValue.ValueType.ARRAY) {
85+
dirs.add(entry.getKey()).attr("array", "true");
86+
for (final JsonValue item : value.asJsonArray()) {
87+
dirs.add("item").set(MkCommits.asText(item)).up();
88+
}
89+
dirs.up();
90+
} else if (value.getValueType() == JsonValue.ValueType.OBJECT) {
91+
dirs.add(entry.getKey());
92+
for (final Map.Entry<String, JsonValue> sub
93+
: value.asJsonObject().entrySet()) {
94+
dirs.add(sub.getKey()).set(MkCommits.asText(sub.getValue())).up();
95+
}
96+
dirs.up();
97+
} else {
98+
dirs.add(entry.getKey()).set(MkCommits.asText(value)).up();
99+
}
100+
}
101+
this.storage.apply(dirs);
78102
return this.get(params.getString("sha"));
79103
}
80104

@@ -91,4 +115,30 @@ public Statuses statuses(
91115
) {
92116
return new MkStatuses(this.get(sha));
93117
}
118+
119+
/**
120+
* XPath of the commits element in the storage XML.
121+
* @return XPath
122+
*/
123+
private String xpath() {
124+
return String.format(
125+
"/github/repos/repo[@coords='%s']/git/commits",
126+
this.coords
127+
);
128+
}
129+
130+
/**
131+
* Render JSON value as plain text suitable for XML storage.
132+
* @param value JSON value
133+
* @return Plain string
134+
*/
135+
private static String asText(final JsonValue value) {
136+
final String result;
137+
if (value.getValueType() == JsonValue.ValueType.STRING) {
138+
result = ((JsonString) value).getString();
139+
} else {
140+
result = value.toString();
141+
}
142+
return result;
143+
}
94144
}

src/test/java/com/jcabi/github/mock/MkCommitsTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.jcabi.github.mock;
66

77
import com.jcabi.github.Commit;
8+
import com.jcabi.github.Commits;
89
import jakarta.json.Json;
910
import jakarta.json.JsonArray;
1011
import jakarta.json.JsonObject;
@@ -46,4 +47,62 @@ void createsMkCommit() throws IOException {
4647
Matchers.equalTo("12ahscba")
4748
);
4849
}
50+
51+
/**
52+
* MkCommits.create() must persist the new commit's sha in storage so
53+
* that subsequent calls (e.g. json(), get()) can find the commit.
54+
* @throws IOException if there is any I/O problem
55+
*/
56+
@Test
57+
void persistsCreatedCommitSha() throws IOException {
58+
final String sha = "9e1f3c7b8d4a5e6f7c2d1b0a9e8f7d6c5b4a3210";
59+
final Commits commits = new MkGitHub().randomRepo().git().commits();
60+
commits.create(
61+
Json.createObjectBuilder()
62+
.add("message", "persisted commit message")
63+
.add("sha", sha)
64+
.add("tree", "treesha9e1f3c7b8d4a")
65+
.add(
66+
"parents",
67+
Json.createArrayBuilder().add("parentsha0123456789").build()
68+
)
69+
.add(
70+
"author",
71+
Json.createObjectBuilder()
72+
.add("name", "Alice")
73+
.add("email", "alice@example.com")
74+
.add("date", "2024-01-15T12:30:00+00:00")
75+
.build()
76+
)
77+
.build()
78+
);
79+
MatcherAssert.assertThat(
80+
"sha must be persisted in storage",
81+
commits.get(sha).json().getString("sha"),
82+
Matchers.equalTo(sha)
83+
);
84+
}
85+
86+
/**
87+
* MkCommits.create() must persist the commit message in storage.
88+
* @throws IOException if there is any I/O problem
89+
*/
90+
@Test
91+
void persistsCreatedCommitMessage() throws IOException {
92+
final String sha = "abcdef0123456789abcdef0123456789abcdef01";
93+
final String message = "another persisted message";
94+
final Commits commits = new MkGitHub().randomRepo().git().commits();
95+
commits.create(
96+
Json.createObjectBuilder()
97+
.add("message", message)
98+
.add("sha", sha)
99+
.add("tree", "treesha-abcdef-0123")
100+
.build()
101+
);
102+
MatcherAssert.assertThat(
103+
"message must be persisted in storage",
104+
commits.get(sha).json().getString("message"),
105+
Matchers.equalTo(message)
106+
);
107+
}
49108
}

0 commit comments

Comments
 (0)