Skip to content

Commit aba0196

Browse files
author
Ondřej Benkovský
committed
introduce output stage
1 parent b02163f commit aba0196

11 files changed

Lines changed: 622 additions & 1 deletion

File tree

src/main/java/com/gooddata/GoodData.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.gooddata.account.AccountService;
99
import com.gooddata.connector.ConnectorService;
10+
import com.gooddata.dataload.OutputStageService;
1011
import com.gooddata.dataload.processes.ProcessService;
1112
import com.gooddata.featureflag.FeatureFlagService;
1213
import com.gooddata.md.maintenance.ExportImportService;
@@ -78,6 +79,7 @@ public class GoodData {
7879
private final NotificationService notificationService;
7980
private final ExportImportService exportImportService;
8081
private final FeatureFlagService featureFlagService;
82+
private final OutputStageService outputStageService;
8183

8284
/**
8385
* Create instance configured to communicate with GoodData Platform under user with given credentials.
@@ -209,6 +211,7 @@ protected GoodData(GoodDataEndpoint endpoint, Authentication authentication, Goo
209211
notificationService = new NotificationService(getRestTemplate());
210212
exportImportService = new ExportImportService(getRestTemplate());
211213
featureFlagService = new FeatureFlagService(restTemplate);
214+
outputStageService = new OutputStageService(restTemplate);
212215
}
213216

214217
static RestTemplate createRestTemplate(GoodDataEndpoint endpoint, HttpClient httpClient) {
@@ -422,4 +425,13 @@ public ExportImportService getExportImportService() {
422425
public FeatureFlagService getFeatureFlagService() {
423426
return featureFlagService;
424427
}
428+
429+
/**
430+
* Get initialized service for output stage management.
431+
*
432+
* @return initialized service for output stage management
433+
*/
434+
public OutputStageService getOutputStageService() {
435+
return outputStageService;
436+
}
425437
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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.dataload;
7+
8+
import static com.gooddata.util.Validate.notNullState;
9+
10+
import com.fasterxml.jackson.annotation.JsonCreator;
11+
import com.fasterxml.jackson.annotation.JsonIgnore;
12+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
13+
import com.fasterxml.jackson.annotation.JsonInclude;
14+
import com.fasterxml.jackson.annotation.JsonProperty;
15+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
16+
import com.fasterxml.jackson.annotation.JsonTypeName;
17+
import org.springframework.web.util.UriTemplate;
18+
19+
import java.util.Map;
20+
21+
/**
22+
* Output stage.
23+
* For each project there is always one output stage, which always exists.
24+
*/
25+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
26+
@JsonTypeName("outputStage")
27+
@JsonIgnoreProperties(ignoreUnknown = true)
28+
@JsonInclude(JsonInclude.Include.NON_NULL)
29+
public class OutputStage {
30+
31+
public static final String URI = "/gdc/dataload/projects/{id}/outputStage";
32+
public static final UriTemplate TEMPLATE = new UriTemplate(URI);
33+
34+
private static final String SELF_LINK = "self";
35+
private static final String OUTPUT_STAGE_DIFF = "outputStageDiff";
36+
private static final String DATALOAD_PROCESS = "dataloadProcess";
37+
38+
private String schema;
39+
private String clientId;
40+
private String outputStagePrefix;
41+
private final Map<String,String> links;
42+
43+
@JsonCreator
44+
private OutputStage(@JsonProperty("schema") final String schema,
45+
@JsonProperty("clientId") final String clientId,
46+
@JsonProperty("outputStagePrefix") final String outputStagePrefix,
47+
@JsonProperty("links") final Map<String, String> links) {
48+
this.schema = schema;
49+
this.clientId = clientId;
50+
this.outputStagePrefix = outputStagePrefix;
51+
this.links = links;
52+
}
53+
54+
public Map<String, String> getLinks() {
55+
return links;
56+
}
57+
58+
/**
59+
* get datawarehouse schema uri {@link com.gooddata.warehouse.WarehouseSchema}
60+
*
61+
* @return warehouse schema, can be null.
62+
*/
63+
@JsonProperty("schema")
64+
public String getSchemaUri() {
65+
return schema;
66+
}
67+
68+
@JsonProperty("schema")
69+
public void setSchemaUri(final String schemaUri) {
70+
this.schema = schemaUri;
71+
}
72+
73+
/**
74+
* check if there is associated schema {@link com.gooddata.warehouse.WarehouseSchema} with this output stage
75+
*
76+
* @return true if there is associated schema, else false
77+
*/
78+
public boolean hasSchemaUri() {
79+
return schema != null;
80+
}
81+
82+
/**
83+
* get client ID
84+
*
85+
* @return client ID, can be null.
86+
*/
87+
public String getClientId() {
88+
return clientId;
89+
}
90+
91+
public void setClientId(final String clientId) {
92+
this.clientId = clientId;
93+
}
94+
95+
/**
96+
* check if there is associated client id with this output stage
97+
*
98+
* @return true if there is associated client id, else false
99+
*/
100+
public boolean hasClientId() {
101+
return clientId != null;
102+
}
103+
104+
/**
105+
* get output stage prefix
106+
*
107+
* @return output stage prefix, can be null.
108+
*/
109+
public String getOutputStagePrefix() {
110+
return outputStagePrefix;
111+
}
112+
113+
public void setOutputStagePrefix(final String outputStagePrefix) {
114+
this.outputStagePrefix = outputStagePrefix;
115+
}
116+
117+
/**
118+
* check if there is associated output stage prefix with this output stage
119+
*
120+
* @return true if there is associated output stage prefix, else false
121+
*/
122+
public boolean hasOutputStagePrefix() {
123+
return outputStagePrefix != null;
124+
}
125+
126+
@JsonIgnore
127+
public String getUri() {
128+
return notNullState(links, "links").get(SELF_LINK);
129+
}
130+
131+
@JsonIgnore
132+
public String getOutputStageDiffUri() {
133+
return notNullState(links, "links").get(OUTPUT_STAGE_DIFF);
134+
}
135+
136+
@JsonIgnore
137+
public String getDataloadProcessUri() {
138+
return notNullState(links, "links").get(DATALOAD_PROCESS);
139+
}
140+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.dataload;
7+
8+
import static com.gooddata.util.Validate.isTrue;
9+
import static com.gooddata.util.Validate.notEmpty;
10+
import static com.gooddata.util.Validate.notNull;
11+
12+
import com.gooddata.AbstractService;
13+
import com.gooddata.GoodDataException;
14+
import com.gooddata.GoodDataRestException;
15+
import com.gooddata.project.Project;
16+
import org.springframework.web.client.RestClientException;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
/**
20+
* Service to manage output stage.
21+
*/
22+
public class OutputStageService extends AbstractService {
23+
24+
/**
25+
* Sets RESTful HTTP Spring template. Should be called from constructor of concrete service extending
26+
* this abstract one.
27+
* @param restTemplate RESTful HTTP Spring template
28+
*/
29+
public OutputStageService(final RestTemplate restTemplate) {
30+
super(restTemplate);
31+
}
32+
33+
/**
34+
* Get output stage by given URI.
35+
* @param uri output stage uri
36+
* @return output stage object
37+
* @throws com.gooddata.dataload.processes.ProcessNotFoundException when the process doesn't exist
38+
*/
39+
public OutputStage getOutputStageByUri(final String uri) {
40+
notEmpty(uri, "uri");
41+
isTrue(OutputStage.TEMPLATE.matches(uri), "uri does not match output stage pattern: " + OutputStage.TEMPLATE.toString());
42+
try {
43+
return restTemplate.getForObject(uri, OutputStage.class);
44+
} catch (RestClientException e) {
45+
throw new GoodDataException("Unable to get output stage " + uri, e);
46+
}
47+
}
48+
49+
/**
50+
* Get output stage by given project.
51+
* @param project project to which the process belongs
52+
* @return output stage
53+
* @throws com.gooddata.dataload.processes.ProcessNotFoundException when the process doesn't exist
54+
*/
55+
public OutputStage getOutputStage(final Project project) {
56+
notNull(project, "project");
57+
return getOutputStageByUri(OutputStage.TEMPLATE.expand(project.getId()).toString());
58+
}
59+
60+
/**
61+
* Update output stage.
62+
*
63+
* @param outputStage output stage
64+
* @return updated output stage
65+
*/
66+
public OutputStage updateOutputStage(final OutputStage outputStage) {
67+
notNull(outputStage, "outputStage");
68+
try {
69+
restTemplate.put(outputStage.getUri(), outputStage);
70+
} catch (GoodDataRestException | RestClientException e) {
71+
throw new GoodDataException("Unable to update output stage, uri: " + outputStage.getUri());
72+
}
73+
74+
return getOutputStageByUri(outputStage.getUri());
75+
}
76+
}

src/main/java/com/gooddata/util/Validate.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static <T extends Collection> T noNullElements(T collection, String argum
8282

8383
public static <T> T[] noNullElements(T[] array, String argument) {
8484
notNull(array, argument);
85-
for (int i = 0; i<array.length; i++) {
85+
for (int i = 0; i < array.length; i++) {
8686
if (array[i] == null) {
8787
throw new IllegalArgumentException(argument + " contains null element at index: " + i);
8888
}
@@ -105,4 +105,15 @@ public static <T> T notNullState(T value, String argumentName) {
105105
return value;
106106
}
107107

108+
/**
109+
* throws new {@link IllegalArgumentException} if expression is false
110+
*
111+
* @param expression boolean expression
112+
* @param message of exception
113+
*/
114+
public static void isTrue(boolean expression, String message) {
115+
if (!expression) {
116+
throw new IllegalArgumentException(message);
117+
}
118+
}
108119
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.dataload;
7+
8+
import com.gooddata.AbstractGoodDataAT;
9+
import com.gooddata.project.Environment;
10+
import com.gooddata.warehouse.Warehouse;
11+
import com.gooddata.warehouse.WarehouseSchema;
12+
import org.testng.annotations.AfterClass;
13+
import org.testng.annotations.Test;
14+
15+
import static org.hamcrest.CoreMatchers.equalTo;
16+
import static org.hamcrest.CoreMatchers.nullValue;
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.hamcrest.core.Is.is;
19+
20+
import java.util.concurrent.TimeUnit;
21+
22+
public class OutputStageServiceAT extends AbstractGoodDataAT {
23+
24+
private static final String CLIENT_ID = "clientId";
25+
private static final String PREFIX = "prefix";
26+
27+
private final Warehouse warehouse;
28+
private final WarehouseSchema warehouseSchema;
29+
30+
public OutputStageServiceAT() {
31+
final String warehouseToken = getProperty("warehouseToken");
32+
final Warehouse wh = new Warehouse(title, warehouseToken);
33+
wh.setEnvironment(Environment.TESTING);
34+
warehouse = gd.getWarehouseService().createWarehouse(wh).get(60, TimeUnit.MINUTES);
35+
warehouseSchema = gd.getWarehouseService().getDefaultWarehouseSchema(warehouse);
36+
}
37+
38+
@Test(groups = "output_stage", dependsOnGroups = {"warehouse", "project"})
39+
public void shouldReturnNullObjectWhenNoOutputStage() {
40+
final OutputStage outputStage = gd.getOutputStageService().getOutputStage(project);
41+
42+
assertThat(outputStage.getSchemaUri(), is(nullValue()));
43+
}
44+
45+
@Test(groups = "output_stage", dependsOnMethods = "shouldReturnNullObjectWhenNoOutputStage")
46+
public void shouldUpdateOutputStage() {
47+
final OutputStage outputStage = gd.getOutputStageService().getOutputStage(project);
48+
outputStage.setSchemaUri(warehouseSchema.getUri());
49+
outputStage.setClientId(CLIENT_ID);
50+
outputStage.setOutputStagePrefix(PREFIX);
51+
52+
final OutputStage updateOutputStage = gd.getOutputStageService().updateOutputStage(outputStage);
53+
54+
assertThat(updateOutputStage.getSchemaUri(), is(equalTo(warehouseSchema.getUri())));
55+
assertThat(updateOutputStage.getClientId(), is(equalTo(CLIENT_ID)));
56+
assertThat(updateOutputStage.getOutputStagePrefix(), is(equalTo(PREFIX)));
57+
}
58+
59+
@Test(groups = "output_stage", dependsOnMethods = "shouldUpdateOutputStage")
60+
public void shouldUpdateOutputStageToNullValues() {
61+
final OutputStage outputStage = gd.getOutputStageService().getOutputStage(project);
62+
outputStage.setSchemaUri(null);
63+
outputStage.setClientId(null);
64+
outputStage.setOutputStagePrefix(null);
65+
66+
final OutputStage updateOutputStage = gd.getOutputStageService().updateOutputStage(outputStage);
67+
68+
assertThat(updateOutputStage.getSchemaUri(), is(nullValue()));
69+
assertThat(updateOutputStage.getClientId(), is(nullValue()));
70+
assertThat(updateOutputStage.getOutputStagePrefix(), is(nullValue()));
71+
}
72+
73+
@AfterClass
74+
public void removeWarehouse() {
75+
if(warehouse != null) {
76+
gd.getWarehouseService().removeWarehouse(warehouse);
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)