Skip to content

Commit 3d46c8a

Browse files
author
Ondřej Benkovský
committed
add support for warehouse schemas
close: #394
1 parent 714a764 commit 3d46c8a

File tree

11 files changed

+422
-0
lines changed

11 files changed

+422
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright (C) 2004-2016, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.warehouse;
7+
8+
import com.fasterxml.jackson.annotation.JsonCreator;
9+
import com.fasterxml.jackson.annotation.JsonIgnore;
10+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
11+
import com.fasterxml.jackson.annotation.JsonProperty;
12+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
13+
import com.fasterxml.jackson.annotation.JsonTypeName;
14+
import org.springframework.web.util.UriTemplate;
15+
16+
import java.util.Map;
17+
18+
/**
19+
* Warehouse schema.
20+
* Deserialization Only.
21+
*/
22+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
23+
@JsonTypeName("schema")
24+
@JsonIgnoreProperties(ignoreUnknown = true)
25+
public class WarehouseSchema {
26+
27+
private static final String SELF_LINK = "self";
28+
private static final String INSTANCE_LINK = "instance";
29+
30+
public static final String URI = WarehouseSchemas.URI + "/{name}";
31+
public static final UriTemplate TEMPLATE = new UriTemplate(URI);
32+
33+
private final String name;
34+
35+
private final String description;
36+
37+
private final Map<String, String> links;
38+
39+
@JsonCreator
40+
private WarehouseSchema(@JsonProperty("name") final String name,
41+
@JsonProperty("description") final String description,
42+
@JsonProperty("links") final Map<String, String> links) {
43+
this.name = name;
44+
this.description = description;
45+
this.links = links;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public String getDescription() {
53+
return description;
54+
}
55+
56+
public Map<String, String> getLinks() {
57+
return links;
58+
}
59+
60+
@JsonIgnore
61+
public String getUri() {
62+
return links != null ? links.get(SELF_LINK): null;
63+
}
64+
65+
@JsonIgnore
66+
public String getInstanceUri() {
67+
return links != null ? links.get(INSTANCE_LINK): null;
68+
}
69+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (C) 2004-2016, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.warehouse;
7+
8+
import com.gooddata.GoodDataException;
9+
import com.gooddata.GoodDataRestException;
10+
11+
/**
12+
* Warehouse schema doesn't exist.
13+
*/
14+
public class WarehouseSchemaNotFoundException extends GoodDataException {
15+
16+
private final String warehouseSchemaUri;
17+
18+
public WarehouseSchemaNotFoundException(String uri, GoodDataRestException cause) {
19+
super("Warehouse schema " + uri + " was not found", cause);
20+
this.warehouseSchemaUri = uri;
21+
}
22+
23+
public String getWarehouseSchemaUri() {
24+
return warehouseSchemaUri;
25+
}
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (C) 2004-2016, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.warehouse;
7+
8+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
9+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
10+
import com.fasterxml.jackson.annotation.JsonTypeName;
11+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
12+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
13+
import com.gooddata.collections.PageableList;
14+
import com.gooddata.collections.Paging;
15+
import org.springframework.web.util.UriTemplate;
16+
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
/**
21+
* List of warehouse schemas.
22+
* Deserialization Only.
23+
*/
24+
@JsonDeserialize(using = WarehouseSchemasDeserializer.class)
25+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
26+
@JsonTypeName(WarehouseSchemas.ROOT_NODE)
27+
@JsonIgnoreProperties(ignoreUnknown = true)
28+
public class WarehouseSchemas extends PageableList<WarehouseSchema> {
29+
30+
public static final String URI = Warehouse.URI + "/schemas";
31+
public static final UriTemplate TEMPLATE = new UriTemplate(URI);
32+
33+
static final String ROOT_NODE = "schemas";
34+
35+
public WarehouseSchemas(final List<WarehouseSchema> items, final Paging paging) {
36+
super(items, paging);
37+
}
38+
39+
public WarehouseSchemas(final List<WarehouseSchema> items, final Paging paging, final Map<String, String> links) {
40+
super(items, paging, links);
41+
}
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2004-2016, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.warehouse;
7+
8+
import com.gooddata.collections.PageableListDeserializer;
9+
import com.gooddata.collections.Paging;
10+
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
/**
15+
* Deserializer of WarehouseSchemas.
16+
*/
17+
class WarehouseSchemasDeserializer extends PageableListDeserializer<WarehouseSchemas, WarehouseSchema> {
18+
19+
protected WarehouseSchemasDeserializer() {
20+
super(WarehouseSchema.class);
21+
}
22+
23+
@Override
24+
protected WarehouseSchemas createList(final List<WarehouseSchema> items, final Paging paging, final Map<String, String> links) {
25+
return new WarehouseSchemas(items, paging, links);
26+
}
27+
}

src/main/java/com/gooddata/warehouse/WarehouseService.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
public class WarehouseService extends AbstractService {
3333

34+
private static final String DEFAULT_SCHEMA_NAME = "default";
35+
3436
/**
3537
* Sets RESTful HTTP Spring template. Should be called from constructor of concrete service extending
3638
* this abstract one.
@@ -303,4 +305,67 @@ public Warehouse updateWarehouse(Warehouse toUpdate) {
303305

304306
return getWarehouseByUri(toUpdate.getUri());
305307
}
308+
309+
/**
310+
* list schemas for Warehouse
311+
*
312+
* @param warehouse to list schemas for
313+
* @return pageable list of warehouse schemas
314+
*/
315+
public PageableList<WarehouseSchema> listWarehouseSchemas(final Warehouse warehouse) {
316+
try {
317+
final WarehouseSchemas result = restTemplate.getForObject(WarehouseSchemas.TEMPLATE.expand(warehouse.getId()), WarehouseSchemas.class);
318+
if (result == null) {
319+
return new PageableList<>();
320+
}
321+
return result;
322+
} catch (GoodDataException | RestClientException e) {
323+
throw new GoodDataException("Unable to list Warehouse schemas", e);
324+
}
325+
}
326+
327+
/**
328+
* get warehouse schema by name
329+
*
330+
* @param warehouse to get schema for
331+
* @param name of schema
332+
* @return warehouse schema
333+
*/
334+
public WarehouseSchema getWarehouseSchemaByName(final Warehouse warehouse, final String name) {
335+
notNull(warehouse, "warehouse");
336+
notEmpty(name, "name");
337+
final String uri = WarehouseSchema.TEMPLATE.expand(warehouse.getId(), name).toString();
338+
return getWarehouseSchemaByUri(uri);
339+
}
340+
341+
/**
342+
* get warehouse schema by uri
343+
*
344+
* @param uri of schema
345+
* @return warehouse schema
346+
*/
347+
public WarehouseSchema getWarehouseSchemaByUri(final String uri) {
348+
notEmpty(uri, "uri");
349+
try {
350+
return restTemplate.getForObject(uri, WarehouseSchema.class);
351+
} catch (GoodDataRestException e) {
352+
if (HttpStatus.NOT_FOUND.value() == e.getStatusCode()) {
353+
throw new WarehouseSchemaNotFoundException(uri.toString(), e);
354+
} else {
355+
throw e;
356+
}
357+
} catch (RestClientException e) {
358+
throw new GoodDataException("Unable to get Warehouse instance " + uri, e);
359+
}
360+
}
361+
362+
/**
363+
* get default warehouse schema
364+
*
365+
* @param warehouse to get default schema for
366+
* @return default warehouse schema
367+
*/
368+
public WarehouseSchema getDefaultWarehouseSchema(final Warehouse warehouse) {
369+
return getWarehouseSchemaByName(warehouse, DEFAULT_SCHEMA_NAME);
370+
}
306371
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (C) 2004-2016, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.warehouse;
7+
8+
import static org.hamcrest.CoreMatchers.equalTo;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.core.Is.is;
11+
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import org.testng.annotations.Test;
14+
15+
import java.io.InputStream;
16+
import java.util.LinkedHashMap;
17+
import java.util.Map;
18+
19+
public class WarehouseSchemaTest {
20+
21+
private static final String NAME = "default";
22+
private static final String DESCRIPTION = "Default schema for new ADS instance";
23+
private static final String SELF_LINK = "/gdc/datawarehouse/instances/instanceId/schemas/default";
24+
private static final String INSTANCE = "/gdc/datawarehouse/instances/instanceId";
25+
private static final String PARENT = "/gdc/datawarehouse/instances/warehouseId/schemas";
26+
private static final Map<String, String> LINKS = new LinkedHashMap<String, String>() {{
27+
put("self", SELF_LINK);
28+
put("instance", INSTANCE);
29+
put("parent", PARENT);
30+
}};
31+
32+
@Test
33+
public void testDeserialization() throws Exception {
34+
final InputStream stream = getClass().getResourceAsStream("/warehouse/schema.json");
35+
final WarehouseSchema warehouseSchema = new ObjectMapper().readValue(stream, WarehouseSchema.class);
36+
37+
assertThat(warehouseSchema.getName(), is(NAME));
38+
assertThat(warehouseSchema.getDescription(), is(DESCRIPTION));
39+
assertThat(warehouseSchema.getLinks(), is(equalTo(LINKS)));
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (C) 2004-2016, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
package com.gooddata.warehouse;
7+
8+
import static com.gooddata.util.ResourceUtils.readObjectFromResource;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.Matchers.notNullValue;
11+
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
12+
import static org.hamcrest.core.Is.is;
13+
14+
import org.testng.annotations.Test;
15+
16+
public class WarehouseSchemasTest {
17+
private final WarehouseSchemas warehouseSchemas = readObjectFromResource("/warehouse/schemas.json", WarehouseSchemas.class);
18+
19+
@Test
20+
public void testDeserialization() throws Exception {
21+
assertThat(warehouseSchemas, notNullValue());
22+
assertThat(warehouseSchemas, hasSize(1));
23+
assertThat(warehouseSchemas.get(0).getName(), is("default"));
24+
}
25+
}

src/test/java/com/gooddata/warehouse/WarehouseServiceAT.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
import static com.gooddata.warehouse.WarehouseIdMatcher.hasSameIdAs;
2323
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.hamcrest.Matchers.contains;
25+
import static org.hamcrest.Matchers.equalTo;
26+
import static org.hamcrest.Matchers.hasProperty;
2427
import static org.hamcrest.Matchers.hasSize;
2528
import static org.hamcrest.Matchers.is;
2629
import static org.hamcrest.Matchers.notNullValue;
@@ -34,6 +37,7 @@
3437
public class WarehouseServiceAT extends AbstractGoodDataAT {
3538

3639
private static final String LOGIN = "john.smith." + UUID.randomUUID() + "@gooddata.com";
40+
private static final String SCHEMA_NAME = "default";
3741

3842
private final String warehouseToken;
3943
private final WarehouseService service;
@@ -118,6 +122,24 @@ public void shouldRemoveUserFromWarehouse() {
118122
warehouseUser = null;
119123
}
120124

125+
@Test(groups = "warehouse", dependsOnMethods = "createWarehouse")
126+
public void listWarehouseSchemas() {
127+
PageableList<WarehouseSchema> warehouseSchemas = service.listWarehouseSchemas(warehouse);
128+
assertThat(warehouseSchemas, contains(hasProperty("name", equalTo(SCHEMA_NAME))));
129+
}
130+
131+
@Test(groups = "warehouse", dependsOnMethods = "createWarehouse")
132+
public void getWarehouseSchema() {
133+
WarehouseSchema warehouseSchema = service.getWarehouseSchemaByName(warehouse, SCHEMA_NAME);
134+
assertThat(warehouseSchema, is(notNullValue()));
135+
}
136+
137+
@Test(groups = "warehouse", dependsOnMethods = "createWarehouse")
138+
public void getDefaultWarehouseSchema() {
139+
WarehouseSchema warehouseSchema = service.getDefaultWarehouseSchema(warehouse);
140+
assertThat(warehouseSchema, is(notNullValue()));
141+
}
142+
121143
@Test(dependsOnGroups = "warehouse")
122144
public void removeWarehouse() throws Exception {
123145
service.removeWarehouse(warehouse);

0 commit comments

Comments
 (0)