Skip to content

Commit

Permalink
make warehouse serializable and pagable
Browse files Browse the repository at this point in the history
  • Loading branch information
martiner committed Jul 30, 2015
1 parent 318b262 commit 71fe188
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 51 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/gooddata/warehouse/Warehouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Warehouse(String title, String authToken, String description) {
}

@JsonCreator
Warehouse(@JsonProperty("title") String title, @JsonProperty("authorizationToken") String authToken,
public Warehouse(@JsonProperty("title") String title, @JsonProperty("authorizationToken") String authToken,
@JsonProperty("description") String description,
@JsonProperty("created") @JsonDeserialize(using = ISODateTimeDeserializer.class) DateTime created,
@JsonProperty("updated") @JsonDeserialize(using = ISODateTimeDeserializer.class) DateTime updated,
Expand Down Expand Up @@ -143,6 +143,10 @@ public void setEnvironment(final Environment environment) {
setEnvironment(environment.name());
}

public Map<String, String> getLinks() {
return links;
}

/**
* Get jdbc connection string. Works only on Warehouse loaded from API (using WarehouseService).
* @return jdbc connection string
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/gooddata/warehouse/WarehouseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
import com.gooddata.AbstractPollHandler;
import com.gooddata.AbstractService;
import com.gooddata.FutureResult;
import com.gooddata.PollResult;
import com.gooddata.GoodDataException;
import com.gooddata.GoodDataRestException;
import com.gooddata.PollResult;
import com.gooddata.collections.Page;
import com.gooddata.collections.PageableList;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.util.Collection;
import java.net.URI;

import static com.gooddata.util.Validate.notEmpty;
import static com.gooddata.util.Validate.notNull;
import static java.util.Collections.emptyList;

/**
* Provide access to warehouse API - create, update, list and delete warehouses.
Expand Down Expand Up @@ -143,16 +145,32 @@ private static String uriFromId(String id) {

/**
* Lists Warehouses. Returns empty list in case there are no warehouses.
* Returns only first page if there's more instances than page limit. Use {@link #listWarehouses(Page)} to get other pages.
*
* @return first page of list of warehouse instances or empty list
*/
public PageableList<Warehouse> listWarehouses() {
return listWarehouses(URI.create(Warehouses.URI));
}

/**
* Lists Warehouses. Returns empty list in case there are no warehouses.
* Returns requested page (by page limit and offset). Use {@link #listWarehouses()} to get first page with default setting.
*
* @return collection of instances or empty list
* @return requested page of list of instances or empty list
*/
public Collection<Warehouse> listWarehouses() {
public PageableList<Warehouse> listWarehouses(Page page) {
notNull(page, "page");
return listWarehouses(page.getPageUri(UriComponentsBuilder.fromUriString(Warehouses.URI)));
}

private PageableList<Warehouse> listWarehouses(final URI uri) {
try {
final Warehouses result = restTemplate.getForObject(Warehouses.URI, Warehouses.class);
if (result == null || result.getItems() == null) {
return emptyList();
final Warehouses result = restTemplate.getForObject(uri, Warehouses.class);
if (result == null) {
return new PageableList<>();
}
return setWarehouseConnection(result.getItems());
return setWarehouseConnection(result);
} catch (GoodDataException | RestClientException e) {
throw new GoodDataException("Unable to list Warehouses", e);
}
Expand Down Expand Up @@ -183,7 +201,7 @@ private Warehouse setWarehouseConnection(Warehouse warehouse) {
return warehouse;
}

private Collection<Warehouse> setWarehouseConnection(Collection<Warehouse> warehouses) {
private PageableList<Warehouse> setWarehouseConnection(PageableList<Warehouse> warehouses) {
notNull(warehouses, "warehouses");
for (Warehouse warehouse : warehouses) {
setWarehouseConnection(warehouse);
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/com/gooddata/warehouse/Warehouses.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
package com.gooddata.warehouse;

import org.codehaus.jackson.annotate.JsonCreator;
import com.gooddata.collections.PageableList;
import com.gooddata.collections.PageableListSerializer;
import com.gooddata.collections.Paging;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.annotate.JsonTypeName;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.List;
import java.util.Map;

/**
* List of warehouses. Deserialization only.
* List of warehouses.
*/
@JsonDeserialize(using = WarehousesDeserializer.class)
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("instances")
@JsonTypeName(Warehouses.ROOT_NODE)
@JsonIgnoreProperties(ignoreUnknown = true)
class Warehouses {
@JsonSerialize(using = WarehousesSerializer.class)
public class Warehouses extends PageableList<Warehouse> {

static final String URI = "/gdc/datawarehouse/instances";
public static final String URI = "/gdc/datawarehouse/instances";

private final List<Warehouse> items;
static final String ROOT_NODE = "instances";

@JsonCreator
private Warehouses(@JsonProperty("items") List<Warehouse> items) {
this.items = items;
public Warehouses(final List<Warehouse> items, final Paging paging) {
super(items, paging);
}

List<Warehouse> getItems() {
return items;
public Warehouses(final List<Warehouse> items, final Paging paging, final Map<String, String> links) {
super(items, paging, links);
}

}
22 changes: 22 additions & 0 deletions src/main/java/com/gooddata/warehouse/WarehousesDeserializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2007-2015, GoodData(R) Corporation. All rights reserved.
*/
package com.gooddata.warehouse;

import com.gooddata.collections.PageableListDeserializer;
import com.gooddata.collections.Paging;

import java.util.List;
import java.util.Map;

class WarehousesDeserializer extends PageableListDeserializer<Warehouses, Warehouse> {

protected WarehousesDeserializer() {
super(Warehouse.class);
}

@Override
protected Warehouses createList(final List<Warehouse> items, final Paging paging, final Map<String, String> links) {
return new Warehouses(items, paging, links);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/gooddata/warehouse/WarehousesSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2007-2015, GoodData(R) Corporation. All rights reserved.
*/
package com.gooddata.warehouse;

import com.gooddata.collections.PageableListSerializer;

import static com.gooddata.warehouse.Warehouses.ROOT_NODE;

/**
* Serializer of Warehouses object into JSON.
*/
class WarehousesSerializer extends PageableListSerializer {

public WarehousesSerializer() {
super(ROOT_NODE);
}

}
4 changes: 4 additions & 0 deletions src/test/java/com/gooddata/util/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class ResourceUtils {

public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public static <T> T readObjectFromResource(String resourcePath, Class<T> objectClass) {
return readObjectFromResource(ResourceUtils.class, resourcePath, objectClass);
}

public static <T> T readObjectFromResource(Class testClass, String resourcePath, Class<T> objectClass) {
notNull(objectClass, "objectClass");

Expand Down
62 changes: 54 additions & 8 deletions src/test/java/com/gooddata/warehouse/WarehouseServiceAT.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.gooddata.warehouse;

import com.gooddata.AbstractGoodDataAT;
import com.gooddata.collections.Page;
import com.gooddata.collections.PageRequest;
import com.gooddata.collections.PageableList;
import com.gooddata.project.Environment;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import java.util.Collection;

import static com.gooddata.warehouse.WarehouseIdMatcher.hasSameIdAs;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;

Expand All @@ -17,38 +22,79 @@
public class WarehouseServiceAT extends AbstractGoodDataAT {

private final String warehouseToken;
private final WarehouseService service;

private Warehouse warehouse;
private Warehouse warehouse2;

public WarehouseServiceAT() {
warehouseToken = getProperty("warehouseToken");
service = gd.getWarehouseService();
}

@Test(groups = "warehouse", dependsOnGroups = "account")
public void createWarehouse() throws Exception {
final WarehouseService warehouseService = gd.getWarehouseService();
final Warehouse wh = new Warehouse(title, warehouseToken);
wh.setEnvironment(Environment.TESTING);
warehouse = warehouseService.createWarehouse(wh).get();
warehouse = service.createWarehouse(wh).get();
String jdbc = warehouse.getJdbcConnectionString();
}

@Test(groups = "warehouse", dependsOnMethods = "createWarehouse")
public void getWarehouse() throws Exception {
final WarehouseService warehouseService = gd.getWarehouseService();
final Warehouse warehouse = warehouseService.getWarehouseById(this.warehouse.getId());
final Warehouse warehouse = service.getWarehouseById(this.warehouse.getId());
assertThat(warehouse, is(hasSameIdAs(warehouse)));
}

@Test(groups = "warehouse", dependsOnMethods = "createWarehouse")
public void listWarehouses() throws Exception {
final WarehouseService warehouseService = gd.getWarehouseService();
final Collection<Warehouse> warehouses = warehouseService.listWarehouses();
final Collection<Warehouse> warehouses = service.listWarehouses();
assertThat(warehouses, hasItem(hasSameIdAs(warehouse)));
}

@Test(groups = "warehouse", dependsOnMethods = {"createWarehouse", "listWarehouses"})
public void shouldPageList() throws Exception {
final String title = this.title + " second";
final Warehouse wh = new Warehouse(title, warehouseToken);
wh.setEnvironment(Environment.TESTING);
warehouse2 = service.createWarehouse(wh).get();

final PageableList<Warehouse> firstPage = service.listWarehouses(new PageRequest(1));
assertThat(firstPage, hasSize(1));

PageableList<Warehouse> page = service.listWarehouses(firstPage.getNextPage());
assertThat(page, hasSize(1));
}

@Test(groups = "warehouse", dependsOnMethods = "shouldPageList")
public void shouldReturnNullOnEndOfPaging() throws Exception {
PageableList<Warehouse> page = service.listWarehouses();
Page nextPage;
while ((nextPage = page.getNextPage()) != null) {
page = service.listWarehouses(nextPage);
}
}

@Test(dependsOnGroups = "warehouse")
public void removeWarehouse() throws Exception {
final WarehouseService warehouseService = gd.getWarehouseService();
warehouseService.removeWarehouse(warehouse);
service.removeWarehouse(warehouse);
warehouse = null;
service.removeWarehouse(warehouse2);
warehouse2 = null;
}

@AfterClass
public void tearDown() throws Exception {
try {
if (warehouse != null) {
service.removeWarehouse(warehouse);
}
} catch (Exception ignored) {}
try {
if (warehouse2 != null) {
service.removeWarehouse(warehouse2);
}
} catch (Exception ignored) {}

}
}
5 changes: 2 additions & 3 deletions src/test/java/com/gooddata/warehouse/WarehouseServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.gooddata.AbstractGoodDataIT;
import com.gooddata.GoodDataException;
import com.gooddata.util.ResourceUtils;
import com.gooddata.collections.PageableList;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.Collection;

import static com.gooddata.util.ResourceUtils.readFromResource;
import static net.jadler.Jadler.onRequest;
Expand Down Expand Up @@ -97,7 +96,7 @@ public void shouldListWarehouses() throws Exception {
.withBody(readFromResource("/warehouse/warehouses.json"))
.withStatus(200);

final Collection<Warehouse> list = gd.getWarehouseService().listWarehouses();
final PageableList<Warehouse> list = gd.getWarehouseService().listWarehouses();
assertThat(list, notNullValue());
assertThat(list, hasSize(2));
}
Expand Down
45 changes: 35 additions & 10 deletions src/test/java/com/gooddata/warehouse/WarehouseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,54 @@
import org.testng.annotations.Test;

import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

public class WarehouseTest {

public static final String TITLE = "Test";
public static final String TOKEN = "{Token}";
public static final String DESCRIPTION = "Storage";
private static final String ENVIRONMENT = "TESTING";
public static final DateTime CREATED = new DateTime(2014, 5, 5, 8, 27, 33, DateTimeZone.UTC);
public static final DateTime UPDATED = new DateTime(2014, 5, 5, 8, 27, 34, DateTimeZone.UTC);
public static final String CREATED_BY = "/gdc/account/profile/createdBy";
public static final String UPDATED_BY = "/gdc/account/profile/updatedBy";
public static final String STATUS = "ENABLED";
public static final Map<String, String> LINKS = new LinkedHashMap<String, String>() {{
put("self", "/gdc/datawarehouse/instances/instanceId");
put("parent", "/gdc/datawarehouse/instances");
put("users", "/gdc/datawarehouse/instances/instanceId/users");
put("jdbc", "/gdc/datawarehouse/instances/instanceId/jdbc");
}};

@Test
public void testSerialization() throws Exception {
public void testSerializationForInstanceCreation() throws Exception {
final Warehouse warehouse = new Warehouse("New ADS", "Your-ADS-Token", "ADS Description");
warehouse.setEnvironment(Environment.TESTING);
assertThat(warehouse, serializesToJson("/warehouse/warehouse-create.json"));
}

@Test
public void testSerialization() throws Exception {
final Warehouse warehouse = new Warehouse(TITLE, TOKEN, DESCRIPTION, CREATED, UPDATED, CREATED_BY, UPDATED_BY, STATUS, ENVIRONMENT, LINKS);
assertThat(warehouse, serializesToJson("/warehouse/warehouse.json"));
}

@Test
public void testDeserialization() throws Exception {
final InputStream stream = getClass().getResourceAsStream("/warehouse/warehouse.json");
final Warehouse warehouse = new ObjectMapper().readValue(stream, Warehouse.class);

assertThat(warehouse.getTitle(), is("Test"));
assertThat(warehouse.getDescription(), is("Storage"));
assertThat(warehouse.getAuthorizationToken(), is("{Token}"));
assertThat(warehouse.getEnvironment(), is("TESTING"));
assertThat(warehouse.getCreatedBy(), is("/gdc/account/profile/createdBy"));
assertThat(warehouse.getUpdatedBy(), is("/gdc/account/profile/updatedBy"));
assertThat(warehouse.getCreated(), is(new DateTime(2014, 5, 5, 8, 27, 33, DateTimeZone.UTC)));
assertThat(warehouse.getUpdated(), is(new DateTime(2014, 5, 5, 8, 27, 34, DateTimeZone.UTC)));
assertThat(warehouse.getAuthorizationToken(), is("{Token}"));
assertThat(warehouse.getTitle(), is(TITLE));
assertThat(warehouse.getDescription(), is(DESCRIPTION));
assertThat(warehouse.getAuthorizationToken(), is(TOKEN));
assertThat(warehouse.getEnvironment(), is(ENVIRONMENT));
assertThat(warehouse.getCreatedBy(), is(CREATED_BY));
assertThat(warehouse.getUpdatedBy(), is(UPDATED_BY));
assertThat(warehouse.getCreated(), is(CREATED));
assertThat(warehouse.getUpdated(), is(UPDATED));
assertThat(warehouse.getStatus(), is(STATUS));
assertThat(warehouse.getLinks(), is(LINKS));
}
}
Loading

0 comments on commit 71fe188

Please sign in to comment.