Skip to content

Commit 578be2b

Browse files
committed
Add workspace timezone getting
1 parent c853547 commit 578be2b

8 files changed

Lines changed: 168 additions & 7 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2004-2021, 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.sdk.model.md;
7+
8+
import com.fasterxml.jackson.annotation.*;
9+
import com.gooddata.sdk.common.util.GoodDataToStringBuilder;
10+
11+
import java.io.Serializable;
12+
13+
/**
14+
* Represents project/workspace metadata configuration.
15+
*/
16+
@JsonTypeName("service")
17+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
18+
@JsonIgnoreProperties(ignoreUnknown = true)
19+
@JsonInclude(JsonInclude.Include.NON_NULL)
20+
public class Service implements Serializable {
21+
22+
public static final String TIMEZONE_URI = "/gdc/md/{projectId}/service/timezone";
23+
24+
private static final long serialVersionUID = -3382672258337809805L;
25+
26+
private final String timezone;
27+
28+
@JsonCreator
29+
private Service(@JsonProperty("timezone") String timezone) {
30+
this.timezone = timezone;
31+
}
32+
33+
/**
34+
* @return string identifier of the timezone (see IANA/Olson tz database for possible values)
35+
*/
36+
public String getTimezone() {
37+
return timezone;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return GoodDataToStringBuilder.defaultToString(this);
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2004-2021, 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.sdk.model.md;
7+
8+
import org.testng.annotations.Test;
9+
10+
import static com.gooddata.sdk.common.util.ResourceUtils.readObjectFromResource;
11+
import static org.hamcrest.CoreMatchers.is;
12+
import static org.hamcrest.CoreMatchers.notNullValue;
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
15+
public class ServiceTest {
16+
17+
@Test
18+
public void shouldDeserialize() throws Exception {
19+
final Service service = readObjectFromResource("/md/service-timezone.json", Service.class);
20+
21+
assertThat(service, is(notNullValue()));
22+
assertThat(service.getTimezone(), is("UTC"));
23+
}
24+
25+
@Test
26+
public void testToStringFormat() throws Exception {
27+
final Service service = readObjectFromResource("/md/service-timezone.json", Service.class);
28+
29+
assertThat(service.toString(), is("Service[timezone=UTC]"));
30+
}
31+
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"service": {
3+
"timezone": "UTC"
4+
}
5+
}

gooddata-java/src/main/java/com/gooddata/sdk/service/md/MetadataService.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2004-2019, GoodData(R) Corporation. All rights reserved.
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
33
* This source code is licensed under the BSD-style license found in the
44
* LICENSE.txt file in the root directory of this source tree.
55
*/
@@ -21,9 +21,8 @@
2121
import java.util.*;
2222
import java.util.stream.Collectors;
2323

24-
import static com.gooddata.sdk.common.util.Validate.noNullElements;
25-
import static com.gooddata.sdk.common.util.Validate.notNull;
26-
import static com.gooddata.sdk.common.util.Validate.notNullState;
24+
import static com.gooddata.sdk.common.util.Validate.*;
25+
import static com.gooddata.sdk.model.md.Service.TIMEZONE_URI;
2726
import static java.util.Arrays.asList;
2827

2928
/**
@@ -440,6 +439,32 @@ public List<AttributeElement> getAttributeElements(DisplayForm displayForm) {
440439
}
441440
}
442441

442+
/**
443+
* Get project/workspace timezone.
444+
*
445+
* @param project project from what to return the timezone
446+
* @return string identifier of the timezone (see IANA/Olson tz database for possible values)
447+
* @throws com.gooddata.sdk.common.GoodDataRestException if GoodData REST API returns unexpected status code
448+
* @throws com.gooddata.sdk.common.GoodDataException if no response from API or client-side HTTP error
449+
*/
450+
public String getTimezone(final Project project) {
451+
notNull(project, "project");
452+
notNull(project.getId(), "project.id");
453+
454+
try {
455+
final Service result = restTemplate.getForObject(TIMEZONE_URI, Service.class, project.getId());
456+
457+
if (result != null) {
458+
return result.getTimezone();
459+
} else {
460+
throw new GoodDataException("Received empty response from API call.");
461+
}
462+
} catch (RestClientException e) {
463+
throw new GoodDataException("Unable to get timezone of project/workspace " + project.getId(), e);
464+
}
465+
}
466+
467+
443468
private Collection<Entry> filterEntries(Collection<Entry> entries, Restriction... restrictions) {
444469
if (restrictions == null || restrictions.length == 0) {
445470
return entries;

gooddata-java/src/test/java/com/gooddata/sdk/service/md/MetadataServiceAT.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2004-2019, GoodData(R) Corporation. All rights reserved.
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
33
* This source code is licensed under the BSD-style license found in the
44
* LICENSE.txt file in the root directory of this source tree.
55
*/
@@ -218,4 +218,13 @@ public void getAttributeElements() throws Exception {
218218
titles.addAll(elements.stream().map(AttributeElement::getTitle).collect(Collectors.toList()));
219219
assertThat(titles, hasItems("DevOps", "HR"));
220220
}
221+
222+
@Test(groups = "md", dependsOnGroups = "project")
223+
public void getTimezone() {
224+
final MetadataService md = gd.getMetadataService();
225+
226+
final String tz = md.getTimezone(project);
227+
assertThat(tz, is("America/Los_Angeles"));
228+
}
229+
221230
}

gooddata-java/src/test/java/com/gooddata/sdk/service/md/MetadataServiceIT.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* Copyright (C) 2004-2019, GoodData(R) Corporation. All rights reserved.
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
33
* This source code is licensed under the BSD-style license found in the
44
* LICENSE.txt file in the root directory of this source tree.
55
*/
66
package com.gooddata.sdk.service.md;
77

8+
import com.gooddata.sdk.common.GoodDataException;
89
import com.gooddata.sdk.service.AbstractGoodDataIT;
910
import com.gooddata.sdk.model.gdc.UriResponse;
1011
import com.gooddata.sdk.model.md.*;
@@ -390,4 +391,26 @@ public void shouldGetAttributeElements() throws Exception {
390391
assertThat(attributeElements, hasSize(3));
391392
assertThat(attributeElements.get(0).getTitle(), is("1167"));
392393
}
394+
395+
@Test(expectedExceptions = GoodDataException.class, expectedExceptionsMessageRegExp = ".*request_id.*Unauthorized.*")
396+
public void getTimezoneShouldThrowGDEOnClientError() {
397+
onRequest()
398+
.havingMethodEqualTo("GET")
399+
.havingPathEqualTo("/gdc/md/PROJECT_ID/service/timezone")
400+
.respond()
401+
.withStatus(401);
402+
403+
gd.getMetadataService().getTimezone(project);
404+
}
405+
406+
@Test(expectedExceptions = GoodDataException.class, expectedExceptionsMessageRegExp = ".*request_id.*Server Error.*")
407+
public void getTimezoneShouldThrowGDEOnServerError() {
408+
onRequest()
409+
.havingMethodEqualTo("GET")
410+
.havingPathEqualTo("/gdc/md/PROJECT_ID/service/timezone")
411+
.respond()
412+
.withStatus(500);
413+
414+
gd.getMetadataService().getTimezone(project);
415+
}
393416
}

gooddata-java/src/test/java/com/gooddata/sdk/service/md/MetadataServiceTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2004-2019, GoodData(R) Corporation. All rights reserved.
2+
* Copyright (C) 2004-2021, GoodData(R) Corporation. All rights reserved.
33
* This source code is licensed under the BSD-style license found in the
44
* LICENSE.txt file in the root directory of this source tree.
55
*/
@@ -421,4 +421,22 @@ public void testGetAttributeElements() {
421421
assertThat(elements, allOf(hasItem(result1), hasItem(result2)));
422422
}
423423

424+
@Test(expectedExceptions = IllegalArgumentException.class)
425+
public void testGetTimezoneNullProject() {
426+
service.getTimezone(null);
427+
}
428+
429+
@Test(expectedExceptions = IllegalArgumentException.class)
430+
public void testGetTimezoneNullProjectId() {
431+
service.getTimezone(mock(Project.class));
432+
}
433+
434+
@Test
435+
public void testGetTimezone() {
436+
when(restTemplate.getForObject(Service.TIMEZONE_URI, Service.class, project.getId()))
437+
.thenReturn(readObjectFromResource("/md/service-timezone.json", Service.class));
438+
final String tz = service.getTimezone(project);
439+
assertThat(tz, is("UTC"));
440+
}
441+
424442
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"service": {
3+
"timezone": "UTC"
4+
}
5+
}

0 commit comments

Comments
 (0)