Skip to content

Commit 638cf9f

Browse files
Implement createSchema (#110)
1 parent 54aceae commit 638cf9f

File tree

7 files changed

+150
-2
lines changed

7 files changed

+150
-2
lines changed

server/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ tasks.jacocoTestCoverageVerification {
187187
violationRules {
188188
rule {
189189
limit {
190-
minimum = BigDecimal.valueOf(0.79)
190+
minimum = BigDecimal.valueOf(0.81)
191191
}
192192
}
193193
}

server/src/main/java/io/whitefox/api/server/ShareV1ApiImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ public Response addTableToSchema(String share, String schema, TableReference tab
3535

3636
@Override
3737
public Response createSchema(String share, String schema) {
38-
return Response.status(501).build();
38+
return wrapExceptions(
39+
() -> Response.status(Response.Status.CREATED)
40+
.entity(WhitefoxMappers.share2api(
41+
shareService.createSchema(share, schema, this.getRequestPrincipal())))
42+
.build(),
43+
exceptionToResponse);
3944
}
4045

4146
@Override

server/src/main/java/io/whitefox/core/Share.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ public Share addRecipients(List<Principal> recipients, Principal currentUser, lo
114114
owner);
115115
}
116116

117+
public Share addSchema(Schema schema, Principal currentUser, long now) {
118+
var newSchemas = new HashMap<>(schemas);
119+
newSchemas.put(schema.name(), schema);
120+
return new Share(
121+
name,
122+
id,
123+
Map.copyOf(newSchemas),
124+
comment,
125+
recipients,
126+
createdAt,
127+
createdBy,
128+
now,
129+
currentUser,
130+
owner);
131+
}
132+
117133
@Override
118134
@SkipCoverageGenerated
119135
public boolean equals(Object o) {

server/src/main/java/io/whitefox/core/services/ShareService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.whitefox.core.Schema;
55
import io.whitefox.core.Share;
66
import io.whitefox.core.actions.CreateShare;
7+
import io.whitefox.core.services.exceptions.SchemaAlreadyExists;
78
import io.whitefox.core.services.exceptions.ShareAlreadyExists;
89
import io.whitefox.core.services.exceptions.ShareNotFound;
910
import io.whitefox.persistence.StorageManager;
@@ -64,4 +65,16 @@ public Share addRecipientsToShare(
6465
public Optional<Share> getShare(String share) {
6566
return storageManager.getShare(share);
6667
}
68+
69+
public Share createSchema(String share, String schema, Principal requestPrincipal) {
70+
var shareObj = storageManager
71+
.getShare(share)
72+
.orElseThrow(() -> new ShareNotFound("Share " + share + "not found"));
73+
if (shareObj.schemas().containsKey(schema)) {
74+
throw new SchemaAlreadyExists("Schema " + schema + " already exists in share " + share);
75+
}
76+
var newSchema = new Schema(schema, Collections.emptyList(), share);
77+
var newShare = shareObj.addSchema(newSchema, requestPrincipal, clock.millis());
78+
return storageManager.updateShare(newShare);
79+
}
6780
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.whitefox.core.services.exceptions;
2+
3+
import io.whitefox.annotations.SkipCoverageGenerated;
4+
5+
@SkipCoverageGenerated
6+
public class SchemaAlreadyExists extends AlreadyExists {
7+
public SchemaAlreadyExists() {}
8+
9+
public SchemaAlreadyExists(String message) {
10+
super(message);
11+
}
12+
13+
public SchemaAlreadyExists(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public SchemaAlreadyExists(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
public SchemaAlreadyExists(
22+
String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
23+
super(message, cause, enableSuppression, writableStackTrace);
24+
}
25+
}

server/src/test/java/io/whitefox/api/server/ShareV1ApiImplTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,29 @@ public void addRecipientToUnknownShare() {
141141
addRecipientsToShare("share2", List.of("Paolo")).statusCode(404);
142142
}
143143

144+
@Test
145+
@Order(5)
146+
public void createSchema() {
147+
createSchemaInShare("share1", "schema1")
148+
.statusCode(201)
149+
.body("name", is("share1"))
150+
.body("comment", is(nullValue()))
151+
.body("recipients", is(hasSize(4)))
152+
.body("schemas", is(hasSize(1)))
153+
.body("schemas[0]", is("schema1"))
154+
.body("createdAt", is(0))
155+
.body("createdBy", is("Mr. Fox"))
156+
.body("updatedAt", is(0))
157+
.body("updatedBy", is("Mr. Fox"))
158+
.body("owner", is("Mr. Fox"));
159+
}
160+
161+
@Test
162+
@Order(6)
163+
public void createSameSchema() {
164+
createSchemaInShare("share1", "schema1").statusCode(409);
165+
}
166+
144167
ValidatableResponse createEmptyShare(String name) {
145168
return given()
146169
.when()
@@ -153,6 +176,14 @@ ValidatableResponse createEmptyShare(String name) {
153176
.then();
154177
}
155178

179+
ValidatableResponse createSchemaInShare(String share, String schema) {
180+
return given()
181+
.when()
182+
.filter(wfFilter)
183+
.post("/whitefox-api/v1/shares/{share}/{schema}", share, schema)
184+
.then();
185+
}
186+
156187
ValidatableResponse addRecipientsToShare(String share, List<String> recipients) {
157188
return given()
158189
.when()

server/src/test/java/io/whitefox/core/services/ShareServiceTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.whitefox.core.Schema;
77
import io.whitefox.core.Share;
88
import io.whitefox.core.actions.CreateShare;
9+
import io.whitefox.core.services.exceptions.SchemaAlreadyExists;
910
import io.whitefox.core.services.exceptions.ShareAlreadyExists;
1011
import io.whitefox.core.services.exceptions.ShareNotFound;
1112
import io.whitefox.persistence.StorageManager;
@@ -125,6 +126,63 @@ public void getShare() throws ExecutionException, InterruptedException {
125126
assertEquals("key", share.get().id());
126127
}
127128

129+
@Test
130+
public void createSchema() {
131+
var storage = new InMemoryStorageManager();
132+
var target = new ShareService(storage, testClock);
133+
target.createShare(emptyCreateShare(), testPrincipal);
134+
var result = target.createSchema("share1", "schema1", testPrincipal);
135+
assertEquals(
136+
new Share(
137+
"share1",
138+
"share1",
139+
Map.of("schema1", new Schema("schema1", Collections.emptyList(), "share1")),
140+
Optional.empty(),
141+
Set.of(),
142+
7,
143+
testPrincipal,
144+
7,
145+
testPrincipal,
146+
testPrincipal),
147+
result);
148+
}
149+
150+
@Test
151+
public void failToCreateSameSchema() {
152+
var storage = new InMemoryStorageManager();
153+
var target = new ShareService(storage, testClock);
154+
target.createShare(emptyCreateShare(), testPrincipal);
155+
target.createSchema("share1", "schema1", testPrincipal);
156+
assertThrows(
157+
SchemaAlreadyExists.class, () -> target.createSchema("share1", "schema1", testPrincipal));
158+
}
159+
160+
@Test
161+
public void createSecondSchema() {
162+
var storage = new InMemoryStorageManager();
163+
var target = new ShareService(storage, testClock);
164+
target.createShare(emptyCreateShare(), testPrincipal);
165+
target.createSchema("share1", "schema1", testPrincipal);
166+
var result = target.createSchema("share1", "schema2", testPrincipal);
167+
assertEquals(
168+
new Share(
169+
"share1",
170+
"share1",
171+
Map.of(
172+
"schema1",
173+
new Schema("schema1", Collections.emptyList(), "share1"),
174+
"schema2",
175+
new Schema("schema2", Collections.emptyList(), "share1")),
176+
Optional.empty(),
177+
Set.of(),
178+
7,
179+
testPrincipal,
180+
7,
181+
testPrincipal,
182+
testPrincipal),
183+
result);
184+
}
185+
128186
private Share createShare(String name, String key, Map<String, Schema> schemas) {
129187
return new Share(name, key, schemas, testPrincipal, 0L);
130188
}

0 commit comments

Comments
 (0)