Skip to content

Commit

Permalink
Merge branch 'gh-1422-swagger-ui-configurable-title' into gh-1424-swa…
Browse files Browse the repository at this point in the history
…gger-ui-static-banner
  • Loading branch information
m55624 committed Nov 6, 2017
2 parents e904448 + 0e03ee2 commit 4cb437b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 42 deletions.
Expand Up @@ -203,6 +203,7 @@ private <O> O _execute(final StoreExecuter<O> storeExecuter, final OperationChai
final OperationChain clonedOpChain = operationChain.shallowClone();
O result = null;
try {
updateOperationChainView(clonedOpChain);
for (final GraphHook graphHook : config.getHooks()) {
graphHook.preExecute(clonedOpChain, context);
}
Expand Down
34 changes: 30 additions & 4 deletions core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphTest.java
Expand Up @@ -55,6 +55,7 @@
import uk.gov.gchq.gaffer.operation.Operation;
import uk.gov.gchq.gaffer.operation.OperationChain;
import uk.gov.gchq.gaffer.operation.OperationException;
import uk.gov.gchq.gaffer.operation.graph.OperationView;
import uk.gov.gchq.gaffer.operation.impl.Limit;
import uk.gov.gchq.gaffer.operation.impl.add.AddElements;
import uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds;
Expand Down Expand Up @@ -995,10 +996,15 @@ public void shouldSetGraphViewOnOperationAndDelegateDoOperationToStore()
final Store store = mock(Store.class);
given(store.getSchema()).willReturn(new Schema());
given(store.getProperties()).willReturn(new StoreProperties());
final View view = mock(View.class);
final View view = new View.Builder()
.entity(TestGroups.ENTITY)
.edge(TestGroups.EDGE)
.build();
final GraphHook hook = new ViewCheckerGraphHook();
final Graph graph = new Graph.Builder()
.config(new GraphConfig.Builder()
.graphId(GRAPH_ID)
.addHook(hook)
.view(view)
.build())
.store(store)
Expand All @@ -1008,8 +1014,7 @@ public void shouldSetGraphViewOnOperationAndDelegateDoOperationToStore()
given(context.getUser()).willReturn(user);
given(store.createContext(user)).willReturn(context);
final Integer expectedResult = 5;
final GetElements operation = mock(GetElements.class);
given(operation.getView()).willReturn(null);
final GetElements operation = new GetElements();

final OperationChain<Integer> opChain = mock(OperationChain.class);
final OperationChain clonedOpChain = mock(OperationChain.class);
Expand All @@ -1023,7 +1028,7 @@ public void shouldSetGraphViewOnOperationAndDelegateDoOperationToStore()
// Then
assertEquals(expectedResult, result);
verify(store).execute(clonedOpChain, context);
verify(operation).setView(view);
JsonAssert.assertEquals(view.toJson(false), operation.getView().toJson(false));
}

@Test
Expand Down Expand Up @@ -1978,4 +1983,25 @@ public InputStream getInput() throws IOException {
return StreamUtil.openStream(getClass(), "/schema/" + schemaFile);
}
}

private static class ViewCheckerGraphHook implements GraphHook {
@Override
public void preExecute(final OperationChain<?> opChain, final Context context) {
for (Operation operation : opChain.getOperations()) {
if (operation instanceof OperationView && null == ((OperationView) operation).getView()) {
throw new IllegalArgumentException("View should not be null");
}
}
}

@Override
public <T> T postExecute(final T result, final OperationChain<?> opChain, final Context context) {
return result;
}

@Override
public <T> T onFailure(final T result, final OperationChain<?> opChain, final Context context, final Exception e) {
return result;
}
}
}
9 changes: 9 additions & 0 deletions example/basic/basic-rest/pom.xml
Expand Up @@ -112,6 +112,15 @@
<path>/${standalone-path}</path>
<port>${standalone-port}</port>
<systemProperties>
<gaffer.properties>
gaffer.properties.app.description,gaffer.properties.app.title
</gaffer.properties>
<gaffer.properties.app.title>
Basic Gaffer Example
</gaffer.properties.app.title>
<gaffer.properties.app.description>
Example using a very basic schema
</gaffer.properties.app.description>
<gaffer.rest-api.basePath>
${standalone-path}
</gaffer.rest-api.basePath>
Expand Down
9 changes: 9 additions & 0 deletions example/federated-demo/pom.xml
Expand Up @@ -127,6 +127,15 @@
<path>/${standalone-path}</path>
<port>${standalone-port}</port>
<systemProperties>
<gaffer.properties>
gaffer.properties.app.description,gaffer.properties.app.title
</gaffer.properties>
<gaffer.properties.app.title>
Federated Gaffer Example
</gaffer.properties.app.title>
<gaffer.properties.app.description>
Example of a Gaffer Federated Store
</gaffer.properties.app.description>
<gaffer.rest-api.basePath>${standalone-path}
</gaffer.rest-api.basePath>
<gaffer.graph.config>
Expand Down
Expand Up @@ -42,6 +42,9 @@ public final class ServiceConstants {
public static final String JOB_SERVICE_UNAVAILABLE = "The job service is not available";

// REST - Other
public static final String DESCRIPTION_DEFAULT = "The Gaffer REST service.";
public static final String CONTACT_DEFAULT = "Gaffer Developers";
public static final String CONTACT_URL_DEFAULT = "https://github.com/gchq/Gaffer";
public static final String GAFFER_DOCUMENTATION_URL = "https://gchq.github.io/gaffer-doc/";

static {
Expand Down
Expand Up @@ -32,12 +32,12 @@
*/
@SwaggerDefinition(
info = @Info(
description = "The Gaffer REST service.",
description = ServiceConstants.DESCRIPTION_DEFAULT,
version = "v1",
title = "Gaffer REST API",
title = "",
contact = @Contact(
name = "Gaffer Developers",
url = "https://github.com/gchq/Gaffer"
name = ServiceConstants.CONTACT_DEFAULT,
url = ServiceConstants.CONTACT_URL_DEFAULT
),
license = @License(
name = "Apache 2.0",
Expand Down
Expand Up @@ -32,12 +32,12 @@
*/
@SwaggerDefinition(
info = @Info(
description = "The Gaffer REST service.",
description = ServiceConstants.DESCRIPTION_DEFAULT,
version = "v2",
title = "Gaffer REST API",
title = "",
contact = @Contact(
name = "Gaffer Developers",
url = "https://github.com/gchq/Gaffer"
name = ServiceConstants.CONTACT_DEFAULT,
url = ServiceConstants.CONTACT_URL_DEFAULT
),
license = @License(
name = "Apache 2.0",
Expand Down
6 changes: 3 additions & 3 deletions rest-api/core-rest/src/main/webapp/index.html
@@ -1,9 +1,9 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Gaffer REST API</title>
<title>Gaffer REST</title>
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" />
<link href='css/typography.css' media='screen' rel='stylesheet' type='text/css'/>
Expand Down Expand Up @@ -42,7 +42,7 @@
<body class="swagger-section">
<div id='header'>
<div class="swagger-ui-wrap">
<a id="logo" href="https://github.com/gchq/Gaffer"><img class="logo__img" alt="swagger" height="30" width="30" src="images/logo_small.png" /><span id="title" class="logo__title">Swagger</span></a>
<a id="logo" href="https://github.com/gchq/Gaffer"><img class="logo__img" alt="swagger" height="30" width="30" src="images/logo_small.png" /><span id="title" class="logo__title">Gaffer REST</span></a>
<form id='api_selector'>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
<div id='auth_container'></div>
Expand Down
69 changes: 42 additions & 27 deletions rest-api/core-rest/src/main/webapp/lib/gaffer.js
Expand Up @@ -19,30 +19,6 @@ function getVersion() {
return footer.substr(footer.lastIndexOf(':') + 2, 2);
}

function updateElementTextUsingProperties(elementPropertiesObj) {
Object.keys(elementPropertiesObj).forEach(function(elementId){
$.get(
getVersion() + '/properties/' + elementPropertiesObj[elementId],
null,
function(propertyValue){
$('#'+ elementId).text(propertyValue);
}
)
})
}

function updateElementColourUsingProperties(elementPropertiesObj) {
Object.keys(elementPropertiesObj).forEach(function(elementId){
$.get(
getVersion() + '/properties/' + elementPropertiesObj[elementId],
null,
function(propertyBannerColour){
$('#' + elementId).css({'background-color': propertyBannerColour});
}
)
})
}

function addExampleButtons(){

var tables = $("#resource_operations .operation-params")
Expand Down Expand Up @@ -150,16 +126,15 @@ function initExampleOperations() {
)
}

function init(onSwaggerComplete){
function init(onSwaggerComplete, onPropertiesLoad){
window.swaggerUi = new SwaggerUi({
url: "latest/swagger.json",
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){
log("Loaded swagger");
$('pre code').each(function(i,e){hljs.highlightBlock(e)});
updateElementColourUsingProperties({"banner": "gaffer.properties.app.banner.colour"});
updateElementTextUsingProperties({"title": "gaffer.properties.app.title", "banner": "gaffer.properties.app.banner.description"});
initFromProperties(onPropertiesLoad);
addExampleButtons();
if(onSwaggerComplete) {
onSwaggerComplete();
Expand All @@ -181,3 +156,43 @@ function init(onSwaggerComplete){

window.swaggerUi.load();
}

function initFromProperties(onPropertiesLoad) {
var onSuccess = function(properties) {
updateTitle(properties);
updateElementTextWithId('banner', 'gaffer.properties.app.banner.description', properties);
updateElementColourWithId('banner', 'gaffer.properties.app.banner.colour', properties);
if(onPropertiesLoad) {
onPropertiesLoad(properties);
}
};
$.get(getVersion() + '/properties', null, onSuccess);
}

function updateTitle(properties) {
updateElementText('gaffer.properties.app.title', properties, function(value, id) {
document.title = value;
})
}

function updateElementText(key, properties, onSuccess) {
updateElementTextWithId(key.split('.').pop(), key, properties, onSuccess);
}

function updateElementColourWithId(id, key, properties, onSuccess) {
if(key in properties) {
$('#' + id).css({'background-color': properties[key]});
if(onSuccess) {
onSuccess(properties[key], id)
}
}
}

function updateElementTextWithId(id, key, properties, onSuccess) {
if(key in properties) {
$('#' + id).text(properties[key]);
if(onSuccess) {
onSuccess(properties[key], id);
}
}
}

0 comments on commit 4cb437b

Please sign in to comment.