Skip to content

Commit

Permalink
#1 Get/SetDatasetPublicPopupText
Browse files Browse the repository at this point in the history
Add a new Operations class, `InfoOperations`.  
Setting will return a String representation of the updated text.
  • Loading branch information
Richard Adams committed Oct 9, 2016
1 parent 8c865f2 commit 0813d55
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public interface DataverseAPI {
* @param config
*/
void configure (DataverseConfig config);

InfoOperations getInfoOperations();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.researchspace.dataverse.api.v1;

import com.researchspace.dataverse.entities.DataverseResponse;
import com.researchspace.dataverse.entities.DvMessage;

public interface InfoOperations {

DvMessage getDatasetPublishPopupCustomText () ;

DataverseResponse<Object> setDatasetPublishPopupCustomText (String text) ;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.researchspace.dataverse.api.v1.DataverseAPI;
import com.researchspace.dataverse.api.v1.DataverseConfig;
import com.researchspace.dataverse.api.v1.DataverseOperations;
import com.researchspace.dataverse.api.v1.InfoOperations;
import com.researchspace.dataverse.api.v1.MetadataOperations;
/** Copyright 2016 ResearchSpace
Expand Down Expand Up @@ -43,4 +44,9 @@ public void configure(DataverseConfig config) {
dvOperationsImpl.setApiKey(config.getApiKey());
dvOperationsImpl.setServerURL(config.getServerURL().toString());
}

@Override
public InfoOperations getInfoOperations() {
return dvOperationsImpl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.researchspace.dataverse.api.v1.DatasetOperations;
import com.researchspace.dataverse.api.v1.DataverseOperations;
import com.researchspace.dataverse.api.v1.InfoOperations;
import com.researchspace.dataverse.api.v1.MetadataOperations;
import com.researchspace.dataverse.entities.Dataset;
import com.researchspace.dataverse.entities.DatasetVersion;
Expand Down Expand Up @@ -62,11 +63,12 @@
*/
@Slf4j
public class DataverseOperationsImplV1 implements DatasetOperations, MetadataOperations, DataverseOperations {
public class DataverseOperationsImplV1 implements DatasetOperations, MetadataOperations, InfoOperations, DataverseOperations {

private String apiKey = "";
private String serverURL = "";
String serverAPIURL = serverURL +"/api/v1";
String serverAPIURL = serverURL +"/api";
String serverAPIv1URL = serverAPIURL +"/v1";

final String apiHeader = "X-Dataverse-key";
public void setApiKey(String apiKey) {
Expand All @@ -75,7 +77,8 @@ public void setApiKey(String apiKey) {

public void setServerURL(String serverURL) {
this.serverURL = serverURL;
this.serverAPIURL = this.serverURL +"/api/v1";
this.serverAPIURL = serverURL + "/api";
this.serverAPIv1URL = this.serverAPIURL +"/v1";
}


Expand All @@ -84,7 +87,7 @@ public void setServerURL(String serverURL) {
*/
@Override
public Dataverse getDataverseById(String dataverseAlias) {
String url = createUrl("dataverses" , dataverseAlias);
String url = createV1Url("dataverses" , dataverseAlias);
log.debug(url);
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
Expand All @@ -95,15 +98,20 @@ public Dataverse getDataverseById(String dataverseAlias) {
return resp.getBody().getData();
}

private String createUrl(String ... pathComponents) {
private String createV1Url(String ... pathComponents) {
String url = serverAPIv1URL + "/" + StringUtils.join(pathComponents, "/") ;
log.info("URL is {}", url);
return url;
}
private String createAdminUrl(String ... pathComponents) {
String url = serverAPIURL + "/" + StringUtils.join(pathComponents, "/") ;
log.info("URL is {}", url);
return url;
}

@Override
public DataverseResponse<DvMessage> deleteDataverse(String dataverseAlias) {
String url = createUrl("dataverses", dataverseAlias);
String url = createV1Url("dataverses", dataverseAlias);
log.debug(url);
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
Expand All @@ -121,7 +129,7 @@ public DataverseResponse<Dataverse> createNewDataverse(String parentDv, Datavers
isTrue(!isEmpty(toCreate.getName()), "Name must be specified");
noNullElements(toCreate.getDataverseContacts(), "At least 1 email contact must be provided");
isTrue(!isEmpty(toCreate.getAlias()), "Alias must be specified");
String url = createUrl("dataverses", parentDv);
String url = createV1Url("dataverses", parentDv);

RestTemplate template = createTemplate();
String json = marshalDataset(toCreate);
Expand All @@ -140,7 +148,7 @@ public DataverseResponse<Dataverse> createNewDataverse(String parentDv, Datavers
*/
@Override
public Identifier createDataset(DatasetFacade facade, String dataverseAlias) {
String url = createUrl("dataverses", dataverseAlias,"datasets");
String url = createV1Url("dataverses", dataverseAlias,"datasets");
RestTemplate template = createTemplate();

String json = getJsonFromFacade(facade);
Expand All @@ -159,7 +167,7 @@ public Identifier createDataset(DatasetFacade facade, String dataverseAlias) {
*/
@Override
public DatasetVersion updateDataset(DatasetFacade facade, Identifier id) {
String url = createUrl("datasets", id.getId() +"", "versions",":draft");
String url = createV1Url("datasets", id.getId() +"", "versions",":draft");

Dataset ds = new DatasetBuilder().build(facade);
String json = marshalDataset(ds.getDatasetVersion());
Expand All @@ -180,7 +188,7 @@ public DatasetVersion updateDataset(DatasetFacade facade, Identifier id) {
*/
@Override
public Dataset getDataset(Identifier dsIdentifier) {
String url = createUrl("datasets", dsIdentifier.getId() +"");
String url = createV1Url("datasets", dsIdentifier.getId() +"");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<Dataset>> type = new ParameterizedTypeReference<DataverseResponse<Dataset>>() {
Expand All @@ -195,7 +203,7 @@ public Dataset getDataset(Identifier dsIdentifier) {
*/
@Override
public List<DatasetVersion> getDatasetVersions (Identifier dsIdentifier) {
String url = createUrl("datasets", dsIdentifier.getId() +"", "versions");
String url = createV1Url("datasets", dsIdentifier.getId() +"", "versions");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<List<DatasetVersion>>> type = new ParameterizedTypeReference<DataverseResponse<List<DatasetVersion>>>() {
Expand Down Expand Up @@ -225,7 +233,7 @@ public void uploadFile (String doi, File file) {
*/
@Override
public DvMessage deleteDataset(Identifier dsIdentifier) {
String url = createUrl("datasets", dsIdentifier.getId() +"" );
String url = createV1Url("datasets", dsIdentifier.getId() +"" );
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<DvMessage>> type = new ParameterizedTypeReference<DataverseResponse<DvMessage>>() {
Expand Down Expand Up @@ -263,7 +271,7 @@ private String marshalDataset(Object object) {
*/
@Override
public List<DataverseObject> getDataverseContents(String dataverseAlias) {
String url = createUrl("dataverses", dataverseAlias, "contents" );
String url = createV1Url("dataverses", dataverseAlias, "contents" );
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<List<DataverseObject>>> type = new ParameterizedTypeReference<DataverseResponse<List<DataverseObject>>>() {
Expand Down Expand Up @@ -292,7 +300,7 @@ private HttpHeaders addAPIKeyToHeader() {
*/
@Override
public List<MetadataBlock> getMetadataBlockInfo() {
String url = createUrl("metadatablocks" );
String url = createV1Url("metadatablocks" );
RestTemplate template = createTemplate();
HttpHeaders headers = addAPIKeyToHeader();
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
Expand All @@ -309,7 +317,7 @@ public List<MetadataBlock> getMetadataBlockInfo() {
*/
@Override
public MetadataBlock getMetadataById(String name) {
String url = createUrl("metadatablocks", name );
String url = createV1Url("metadatablocks", name );
RestTemplate template = createTemplate();
HttpHeaders headers = addAPIKeyToHeader();
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
Expand Down Expand Up @@ -343,7 +351,7 @@ RestTemplate createTemplate() {
*/
@Override
public void publishDataset(Identifier dsIdentifier, Version version) {
String url = createUrl("datasets", dsIdentifier.getId() + "", "actions", ":publish") + "?type="
String url = createV1Url("datasets", dsIdentifier.getId() + "", "actions", ":publish") + "?type="
+ version.name().toLowerCase();
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
Expand All @@ -358,7 +366,7 @@ public void publishDataset(Identifier dsIdentifier, Version version) {

@Override
public DataverseResponse<Dataverse> publishDataverse(String dvName) {
String url = createUrl("dataverses", dvName, "actions", ":publish");
String url = createV1Url("dataverses", dvName, "actions", ":publish");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<Dataverse>> type = new ParameterizedTypeReference<DataverseResponse<Dataverse>>() {
Expand All @@ -367,5 +375,30 @@ public DataverseResponse<Dataverse> publishDataverse(String dvName) {
log.debug(resp.getBody().toString());
return resp.getBody();

}

@Override
public DvMessage getDatasetPublishPopupCustomText() {
String url = createAdminUrl("admin", "settings", ":DatasetPublishPopupCustomText");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<DvMessage>> type = new ParameterizedTypeReference<DataverseResponse<DvMessage>>() {
};
ResponseEntity<DataverseResponse<DvMessage>> resp = template.exchange(url, HttpMethod.GET, entity, type);
log.debug(resp.getBody().toString());
return resp.getBody().getData();

}

@Override
public DataverseResponse<Object> setDatasetPublishPopupCustomText(String text) {
String url = createAdminUrl("admin", "settings", ":DatasetPublishPopupCustomText");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity(text);
ParameterizedTypeReference<DataverseResponse<Object>> type = new ParameterizedTypeReference<DataverseResponse<Object>>() {
};
ResponseEntity<DataverseResponse<Object>> resp = template.exchange(url, HttpMethod.PUT, entity, type);
log.debug(resp.getBody().toString());
return resp.getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.researchspace.dataverse.api.v1.DataverseAPI;
import com.researchspace.dataverse.api.v1.DataverseConfig;
import com.researchspace.dataverse.api.v1.DataverseOperations;
import com.researchspace.dataverse.api.v1.InfoOperations;
import com.researchspace.dataverse.api.v1.MetadataOperations;
import com.researchspace.dataverse.spring.config.DataverseSpringConfig;

Expand All @@ -30,6 +31,7 @@ public class AbstractIntegrationTest extends AbstractJUnit4SpringContextTests {
DatasetOperations datasetOps;
DataverseOperations dataverseOps;
MetadataOperations metadataOPs;
InfoOperations infoOps;
@Autowired
DataverseAPI dataverseAPI;
@Value("#{systemProperties['dataverseAlias']}")
Expand All @@ -56,6 +58,7 @@ public void setUp() throws Exception {
datasetOps = dataverseAPI.getDatasetOperations();
dataverseOps = dataverseAPI.getDataverseOperations();
metadataOPs = dataverseAPI.getMetadataOperations();
infoOps = dataverseAPI.getInfoOperations();
}

private void validateServerCredentials() {
Expand All @@ -67,10 +70,4 @@ private void validateServerCredentials() {
Validate.notEmpty(serverURL,
"Dataverse server URL must be set via command line -DdataverseServerURL option or in test.properties)");
}

@Test
public void test() {
assertTrue(1 == 1);// stop complaints about no methods
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.researchspace.dataverse.http;

import static org.junit.Assert.*;

import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.researchspace.dataverse.entities.DataverseResponse;
import com.researchspace.dataverse.entities.DvMessage;

public class InfoOperationsTest extends AbstractIntegrationTest {

private static final String NEW_TEXT = "Do you want to publish";

@Before
public void setUp() throws Exception {
super.setUp();
}

@After
public void tearDown() throws Exception {
}

@Test
public void testGetSetDatasetPublishPopupCustomText() {
DvMessage originalText = infoOps.getDatasetPublishPopupCustomText();
assertTrue("Message text was empty", !StringUtils.isEmpty(originalText.getMessage()));
DataverseResponse<Object> response = infoOps.setDatasetPublishPopupCustomText(NEW_TEXT);
assertTrue("New text was not set", response.getData().toString().contains(NEW_TEXT));
response = infoOps.setDatasetPublishPopupCustomText(originalText.getMessage());
assertTrue("Originl text was not restored", response.getData().toString().contains(originalText.getMessage()));
}


}

0 comments on commit 0813d55

Please sign in to comment.