Skip to content

Commit

Permalink
Merge pull request #465 from dcrissman/issue464
Browse files Browse the repository at this point in the history
fixes #464 - allow datasource to be changed in-memory
  • Loading branch information
dcrissman committed Sep 15, 2015
2 parents cc3fff2 + 9d9a677 commit 9a8dec7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.redhat.lightblue.Request;
import com.redhat.lightblue.config.DataSourcesConfiguration;
import com.redhat.lightblue.config.JsonTranslator;
Expand Down Expand Up @@ -103,9 +105,13 @@ public AbstractCRUDTestController(boolean loadStatically) throws Exception {

Metadata metadata = lightblueFactory.getMetadata();

String datasource = getDatasource();
JsonNode[] metadataNodes = getMetadataJsonNodes();
if (metadataNodes != null) {
for (JsonNode metadataJson : metadataNodes) {
if (datasource != null) {
ensureDatasource(metadataJson, datasource);
}
if (isGrantAnyoneAccess()) {
grantAnyoneAccess(metadataJson);
}
Expand Down Expand Up @@ -193,6 +199,21 @@ protected JsonNode getLightblueMetadataJson() throws Exception {
*/
protected abstract JsonNode[] getMetadataJsonNodes() throws Exception;

public static void ensureDatasource(JsonNode node, String datasource) {
ObjectNode datastoreNode = (ObjectNode) node.get("entityInfo").get("datastore");
datastoreNode.replace("datasource", new TextNode(datasource));
}

/**
* Override to set the datasource values for all metadata to the returned
* value of this method. By default <code>null</code> will be returned
* disabling this feature.
* @return String name of datasource to use in all metadata.
*/
protected String getDatasource() {
return null;
}

/**
* @return <code>true</code> if access settings on metadata should be altered to
* 'anyone', otherwise <code>false</code>. Defaults to <code>true</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@

public class AbstractCRUDTestControllerTest {

@Test
public void testEnsureDatasource() throws Exception {
JsonNode node = loadJsonNode("./metadata/datasource.json");
AbstractCRUDTestController.ensureDatasource(node, "anotherdatasource");

assertEquals("anotherdatasource", node.get("entityInfo").get(
"datastore").get("datasource").asText());
}

@Test
public void testGrantAnyoneAccess() throws Exception {
JsonNode node = loadJsonNode("./metadata/access.json");
AbstractCRUDTestController.grantAnyoneAccess(node);

assertEquals("anyone",
assertEquals("anyone",
node.get("schema").get("access").get("insert").get(0).textValue());
assertEquals("anyone",
assertEquals("anyone",
node.get("schema").get("fields").get("anArray").get("access").get("insert").get(0).textValue());
assertEquals("anyone",
assertEquals("anyone",
node.get("schema").get("fields").get("anArray").get("items").get("fields").get("name").get("access").get("insert").get(0).textValue());
assertEquals("anyone",
assertEquals("anyone",
node.get("schema").get("fields").get("someField").get("access").get("insert").get(0).textValue());
}

Expand Down
7 changes: 7 additions & 0 deletions test/src/test/resources/metadata/datasource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"entityInfo": {
"datastore": {
"datasource": "somedatasource"
}
}
}

0 comments on commit 9a8dec7

Please sign in to comment.