Skip to content

Commit ffadd37

Browse files
committed
Upgrade to API version 2
1 parent ecb06b2 commit ffadd37

File tree

9 files changed

+222
-34
lines changed

9 files changed

+222
-34
lines changed

gooddata-java-model/src/main/java/com/gooddata/sdk/model/project/Project.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
@JsonInclude(JsonInclude.Include.NON_NULL)
3838
public class Project {
3939

40-
public static final String PROJECTS_URI = "/gdc/account/profile/{id}/projects";
4140
public static final String URI = Projects.URI + "/{id}";
4241
public static final UriTemplate TEMPLATE = new UriTemplate(URI);
4342
private static final Set<String> PREPARING_STATES = new HashSet<>(asList("PREPARING", "PREPARED", "LOADING"));

gooddata-java-model/src/main/java/com/gooddata/sdk/model/project/Projects.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,65 @@
55
*/
66
package com.gooddata.sdk.model.project;
77

8-
import com.fasterxml.jackson.annotation.JsonCreator;
9-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
10-
import com.fasterxml.jackson.annotation.JsonInclude;
11-
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.annotation.*;
9+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
10+
import com.gooddata.collections.PageableList;
11+
import com.gooddata.collections.PageableListDeserializer;
12+
import com.gooddata.collections.Paging;
1213
import com.gooddata.util.GoodDataToStringBuilder;
14+
import org.springframework.web.util.UriTemplate;
1315

1416
import java.util.Collection;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
import static com.gooddata.sdk.model.project.Projects.ROOT_NODE;
1521

1622
/**
23+
* Collection of GoodData projects being returned from API.
1724
*/
25+
@JsonDeserialize(using = Projects.Deserializer.class)
26+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
27+
@JsonTypeName(ROOT_NODE)
1828
@JsonIgnoreProperties(ignoreUnknown = true)
1929
@JsonInclude(JsonInclude.Include.NON_NULL)
20-
public class Projects {
30+
public class Projects extends PageableList<Project> {
2131

2232
public static final String URI = "/gdc/projects";
33+
public static final String LIST_PROJECTS_URI = "/gdc/account/profile/{id}/projects";
34+
public static final UriTemplate LIST_PROJECTS_TEMPLATE = new UriTemplate(LIST_PROJECTS_URI);
35+
36+
static final String ROOT_NODE = "projects";
37+
38+
static class Deserializer extends PageableListDeserializer<Projects, Project> {
2339

24-
private final Collection<Project> projects;
40+
protected Deserializer() {
41+
super(Project.class);
42+
}
2543

26-
@JsonCreator
27-
public Projects(@JsonProperty("projects") Collection<Project> projects) {
28-
this.projects = projects;
44+
@Override
45+
protected Projects createList(final List<Project> items, final Paging paging, final Map<String, String> links) {
46+
return new Projects(items, paging);
47+
}
2948
}
3049

50+
public Projects(List<Project> items, Paging paging) {
51+
super(items, paging);
52+
}
53+
54+
/**
55+
* @return the list of current page items only
56+
* @deprecated use {@link PageableList#getCurrentPageItems()} or other methods from {@link PageableList}.
57+
* Deprecated since version 3.0.0. Will be removed in one of future versions.
58+
*/
59+
@Deprecated
3160
public Collection<Project> getProjects() {
32-
return projects;
61+
return getCurrentPageItems();
3362
}
3463

3564
@Override
3665
public String toString() {
3766
return GoodDataToStringBuilder.defaultToString(this);
3867
}
68+
3969
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1
1+
2
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) 2004-2019, 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+
7+
package com.gooddata.sdk.model.project
8+
9+
import com.gooddata.util.ResourceUtils
10+
import spock.lang.Specification
11+
12+
class ProjectsTest extends Specification {
13+
14+
def "should deserialize"() {
15+
when:
16+
def projects = ResourceUtils.readObjectFromResource("/project/projects.json", Projects)
17+
18+
then:
19+
projects.projects.size() == 1
20+
projects.projects.first().title == "Project Name"
21+
projects.paging
22+
}
23+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"projects": {
3+
"paging": {
4+
"offset": 0,
5+
"limit": 5,
6+
"count": 1,
7+
"totalCount": 1
8+
},
9+
"items": [
10+
{
11+
"project": {
12+
"content": {
13+
"state": "ENABLED",
14+
"cluster": "",
15+
"environment": "TESTING",
16+
"driver": "Pg",
17+
"isPublic": "0",
18+
"guidedNavigation": "1"
19+
},
20+
"meta": {
21+
"title": "Project Name",
22+
"summary": "",
23+
"created": "2018-08-10 23:00:21",
24+
"updated": "2018-08-10 23:00:22",
25+
"author": "/gdc/account/profile/876ec68f5630b38de65852ed5d6236ff",
26+
"contributor": "/gdc/account/profile/876ec68f5630b38de65852ed5d6236ff"
27+
},
28+
"links": {
29+
"self": "/gdc/projects/defaultEmptyProject",
30+
"roles": "/gdc/projects/defaultEmptyProject/roles",
31+
"users": "/gdc/projects/defaultEmptyProject/users?link=1&offset=0&limit=100",
32+
"userRoles": "/gdc/projects/defaultEmptyProject/users/876ec68f5630b38de65852ed5d6236ff/roles",
33+
"userPermissions": "/gdc/projects/defaultEmptyProject/users/876ec68f5630b38de65852ed5d6236ff/permissions",
34+
"invitations": "/gdc/projects/defaultEmptyProject/invitations",
35+
"ldm": "/gdc/projects/defaultEmptyProject/ldm",
36+
"ldm_thumbnail": "/gdc/projects/defaultEmptyProject/ldm?thumbnail=1",
37+
"publicartifacts": "/gdc/projects/defaultEmptyProject/publicartifacts",
38+
"uploads": "/gdc/projects/defaultEmptyProject/uploads/",
39+
"metadata": "/gdc/md/defaultEmptyProject",
40+
"templates": "/gdc/md/defaultEmptyProject/templates",
41+
"connectors": "/gdc/projects/defaultEmptyProject/connectors",
42+
"schedules": "/gdc/projects/defaultEmptyProject/schedules",
43+
"dataload": "/gdc/projects/defaultEmptyProject/dataload",
44+
"eventstores": "/gdc/projects/defaultEmptyProject/dataload/eventstore/stores",
45+
"execute": "/gdc/projects/defaultEmptyProject/execute",
46+
"clearCaches": "/gdc/projects/defaultEmptyProject/clearCaches",
47+
"projectFeatureFlags": "/gdc/projects/defaultEmptyProject/projectFeatureFlags",
48+
"config": "/gdc/projects/defaultEmptyProject/config"
49+
}
50+
}
51+
}
52+
]
53+
}
54+
}

gooddata-java/src/main/java/com/gooddata/sdk/service/project/ProjectService.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,57 @@ public ProjectService(final RestTemplate restTemplate, final AccountService acco
7272
*
7373
* @return collection of all projects current user has access to
7474
* @throws com.gooddata.GoodDataException when projects can't be accessed
75+
* @deprecated use {@link #listProjects()} or {@link #listProjects(Page)} instead.
76+
* Deprecated since version 3.0.0. Will be removed in one of future versions.
7577
*/
78+
@Deprecated
7679
public Collection<Project> getProjects() {
80+
return listProjects().collectAll();
81+
}
82+
83+
/**
84+
* Get first page of paged list of projects that current user has access to.
85+
*
86+
* @return MultiPageList list of found projects or empty list
87+
*/
88+
public PageableList<Project> listProjects() {
89+
final String userId = accountService.getCurrent().getId();
90+
return new MultiPageList<>(page -> listProjects(getProjectsUri(userId, page)));
91+
}
92+
93+
/**
94+
* Get defined page of paged list of projects that current user has access to.
95+
*
96+
* @param startPage page to be retrieved first
97+
* @return MultiPageList list of found projects or empty list
98+
*/
99+
public PageableList<Project> listProjects(final Page startPage) {
100+
notNull(startPage, "startPage");
101+
final String userId = accountService.getCurrent().getId();
102+
return new MultiPageList<>(startPage, page -> listProjects(getProjectsUri(userId, page)));
103+
}
104+
105+
private PageableList<Project> listProjects(final URI uri) {
77106
try {
78-
final String id = accountService.getCurrent().getId();
79-
final Projects projects = restTemplate.getForObject(Project.PROJECTS_URI, Projects.class, id);
80-
return projects.getProjects();
107+
final Projects projects = restTemplate.getForObject(uri, Projects.class);
108+
if (projects == null) {
109+
return new PageableList<>();
110+
}
111+
return projects;
81112
} catch (GoodDataException | RestClientException e) {
82-
throw new GoodDataException("Unable to get projects", e);
113+
throw new GoodDataException("Unable to list projects", e);
83114
}
84115
}
85116

117+
private static URI getProjectsUri(final String userId) {
118+
notEmpty(userId, "userId");
119+
return Projects.LIST_PROJECTS_TEMPLATE.expand(userId);
120+
}
121+
122+
private static URI getProjectsUri(final String userId, final Page page) {
123+
return page.getPageUri(fromUri(getProjectsUri(userId)));
124+
}
125+
86126
/**
87127
* Create new project.
88128
*

gooddata-java/src/test/groovy/com/gooddata/sdk/service/GoodDataIT.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import static org.hamcrest.CoreMatchers.startsWith
1818
class GoodDataIT extends GoodDataITBase<GdcService> {
1919

2020
private static final String USER_AGENT = 'customAgent/X.Y'
21-
private static final String API_VERSION = '1'
21+
private static final String API_VERSION = '2'
2222

2323
@Override
2424
protected GoodDataSettings createGoodDataSettings() {

gooddata-java/src/test/java/com/gooddata/sdk/service/project/ProjectServiceAT.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,23 @@ public void createProject() {
4949
project = gd.getProjectService().createProject(p).get();
5050
}
5151

52+
@SuppressWarnings("deprecation")
5253
@Test(groups = "project", dependsOnMethods = "createProject")
53-
public void listProjects() {
54+
public void getProjects() {
5455
final ProjectService projectService = gd.getProjectService();
5556

5657
final Collection<Project> projects = projectService.getProjects();
5758
assertThat(projects, IsIterableContaining.hasItem(ProjectIdMatcher.hasSameIdAs(project)));
5859
}
5960

61+
@Test(groups = "project", dependsOnMethods = "createProject")
62+
public void listProjects() {
63+
final ProjectService projectService = gd.getProjectService();
64+
65+
final Collection<Project> projects = projectService.listProjects();
66+
assertThat(projects, IsIterableContaining.hasItem(ProjectIdMatcher.hasSameIdAs(project)));
67+
}
68+
6069
@Test(groups = "project", dependsOnMethods = "createProject")
6170
public void listProjectUsers() {
6271
final ProjectService projectService = gd.getProjectService();

0 commit comments

Comments
 (0)