Skip to content

Commit

Permalink
XWIKI-20409: Insert a LiveData as velocity macro
Browse files Browse the repository at this point in the history
* Invert execute and render method names
  • Loading branch information
manuelleduc committed Jan 9, 2024
1 parent fac51af commit f2eec83
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class LiveDataRenderer
private RenderingContext renderingContext;

/**
* Render the Live Data content.
* Execute the Live Data and return a {@link Block}.
*
* @param parameters the parameters used to render the Live Data
* @param advancedParameters a map containing the advanced parameters of the Live Data
Expand All @@ -96,11 +96,11 @@ public class LiveDataRenderer
* @throws LiveDataException in case of issue when resolving the Live Data configuration or when serializing the
* advanced parameters to json
*/
public Block render(LiveDataRendererParameters parameters, Map<?, ?> advancedParameters, boolean restricted)
public Block execute(LiveDataRendererParameters parameters, Map<?, ?> advancedParameters, boolean restricted)
throws LiveDataException
{
try {
return render(parameters, new ObjectMapper().writeValueAsString(advancedParameters), restricted);
return execute(parameters, new ObjectMapper().writeValueAsString(advancedParameters), restricted);
} catch (JsonProcessingException e) {
throw new LiveDataException(
"Failed to to serialize the advanced parameters [%s] to json.".formatted(advancedParameters),
Expand All @@ -109,7 +109,7 @@ public Block render(LiveDataRendererParameters parameters, Map<?, ?> advancedPar
}

/**
* Render the Live Data content.
* Execute the Live Data and return a {@link Block}.
*
* @param parameters the parameters used to render the Live Data
* @param advancedParameters a json string containing the advanced parameters of the Live Data
Expand All @@ -118,7 +118,7 @@ public Block render(LiveDataRendererParameters parameters, Map<?, ?> advancedPar
* @return the Live Data {@link Block}
* @throws LiveDataException in case of issue when resolving the Live Data configuration
*/
public Block render(LiveDataRendererParameters parameters, String advancedParameters, boolean restricted)
public Block execute(LiveDataRendererParameters parameters, String advancedParameters, boolean restricted)
throws LiveDataException
{
// Load the JavaScript code of the Live Data element.
Expand Down Expand Up @@ -162,13 +162,13 @@ public Block render(LiveDataRendererParameters parameters, String advancedParame
* @return the Live Data {@link Block}
* @throws LiveDataException in case of issue when resolving the Live Data configuration or during rendering
*/
public String execute(LiveDataRendererParameters parameters, Map<?, ?> advancedParameters, boolean restricted)
public String render(LiveDataRendererParameters parameters, Map<?, ?> advancedParameters, boolean restricted)
throws LiveDataException
{
String hint = this.renderingContext.getTargetSyntax().toIdString();
try {
BlockRenderer renderer = this.componentManagerProvider.get().getInstance(BlockRenderer.class, hint);
Block block = render(parameters, advancedParameters, restricted);
Block block = execute(parameters, advancedParameters, restricted);
WikiPrinter printer = new DefaultWikiPrinter();
renderer.render(block, printer);
return printer.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,21 @@ public String effectiveConfig(String liveDataConfigJSON)
}

/**
* Renders a Live Data as a {@link Block}.
* Execute the Live Data and return a {@link Block}.
*
* @param parameters the parameters to pass to the Live Data renderer
* @return the Live Data {@link Block}
* @throws LiveDataException in case of error when rendering the Live Data
* @since 16.0.0RC1
*/
@Unstable
public Block render(Map<String, Object> parameters) throws LiveDataException
public Block execute(Map<String, Object> parameters) throws LiveDataException
{
return render(parameters, null);
return execute(parameters, null);
}

/**
* Renders a Live Data as a {@link Block}.
* Execute the Live Data and return a {@link Block}.
*
* @param parameters the parameters to pass to the Live Data renderer
* @param advancedParameters the advanced parameters to pass to the Live Data renderer
Expand All @@ -185,38 +185,38 @@ public Block render(Map<String, Object> parameters) throws LiveDataException
* @since 16.0.0RC1
*/
@Unstable
public Block render(Map<String, Object> parameters, Map<?, ?> advancedParameters) throws LiveDataException
public Block execute(Map<String, Object> parameters, Map<?, ?> advancedParameters) throws LiveDataException
{
return this.liveDataRenderer.render(convertParams(parameters), advancedParameters, false);
return this.liveDataRenderer.execute(convertParams(parameters), advancedParameters, false);
}

/**
* Renders a Live Data as a {@link String}.
* Renders a Live Data.
*
* @param parameters the parameters to pass to the Live Data executor
* @return the result of {@link #render(Map)} in the current syntax
* @return the result of {@link #execute(Map)} in the current syntax
* @throws LiveDataException in case of error when rendering the Live Data
* @since 16.0.0RC1
*/
@Unstable
public String execute(Map<String, Object> parameters) throws LiveDataException
public String render(Map<String, Object> parameters) throws LiveDataException
{
return execute(parameters, null);
return render(parameters, null);
}

/**
* Renders a Live Data as a {@link String}.
* Renders a Live Data.
*
* @param parameters the parameters to pass to the Live Data executor
* @param advancedParameters the advanced parameters to pass to the Live Data executor
* @return the result of {@link #render(Map, Map)} in the current syntax
* @return the result of {@link #execute(Map, Map)} in the current syntax
* @throws LiveDataException in case of error when rendering the Live Data
* @since 16.0.0RC1
*/
@Unstable
public String execute(Map<String, Object> parameters, Map<?, ?> advancedParameters) throws LiveDataException
public String render(Map<String, Object> parameters, Map<?, ?> advancedParameters) throws LiveDataException
{
return this.liveDataRenderer.execute(convertParams(parameters), advancedParameters, false);
return this.liveDataRenderer.render(convertParams(parameters), advancedParameters, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ class LiveDataRendererTest
private RenderingContext renderingContext;

@Test
void render() throws Exception
void execute() throws Exception
{
LiveDataRendererParameters parameters = initParams();
Block block = this.renderer.render(parameters, ADVANCED_PARAMETERS_EMPTY, true);
Block block = this.renderer.execute(parameters, ADVANCED_PARAMETERS_EMPTY, true);
assertEquals(new GroupBlock(Map.of(
"class", "liveData loading",
"data-config", ADVANCED_PARAMETERS_EMPTY,
Expand All @@ -103,12 +103,12 @@ void render() throws Exception
}

@Test
void renderWithId() throws Exception
void executeWithId() throws Exception
{
String liveDataId = "ld-id";
LiveDataRendererParameters parameters = initParams(params -> params.setId(liveDataId));

Block block = this.renderer.render(parameters, ADVANCED_PARAMETERS_EMPTY, true);
Block block = this.renderer.execute(parameters, ADVANCED_PARAMETERS_EMPTY, true);
assertEquals(new GroupBlock(Map.of(
"class", "liveData loading",
"data-config", ADVANCED_PARAMETERS_EMPTY,
Expand All @@ -120,7 +120,7 @@ void renderWithId() throws Exception
}

@Test
void execute() throws Exception
void render() throws Exception
{
Syntax html50 = Syntax.HTML_5_0;

Expand All @@ -133,7 +133,7 @@ void execute() throws Exception
when(this.componentManagerProvider.get()).thenReturn(componentManager);
when(this.renderingContext.getTargetSyntax()).thenReturn(html50);

this.renderer.execute(new LiveDataRendererParameters(), Map.of(), true);
this.renderer.render(new LiveDataRendererParameters(), Map.of(), true);

verify(blockRenderer).render(eq(new GroupBlock(Map.of(
"class", "liveData loading",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ class LiveDataScriptServiceTest
private LiveDataRenderer liveDataRenderer;

@Test
void renderUnknownParam()
void executeUnknownParam()
{
Map<String, Object> parameters = Map.of("a", "b");
LiveDataException exception =
assertThrows(LiveDataException.class, () -> this.scriptService.render(parameters));
assertThrows(LiveDataException.class, () -> this.scriptService.execute(parameters));
assertThat(exception.getMessage(), matchesPattern(
"Failed to set property \\[a] with value \\[b] in object "
+ "\\[org.xwiki.livedata.internal.LiveDataRendererParameters@[^]]+]"));
}

@Test
void render() throws Exception
void execute() throws Exception
{
String liveDataId = "ld-id";
Map<String, Object> parameters = Map.of("id", liveDataId);
this.scriptService.render(parameters);
this.scriptService.execute(parameters);
LiveDataRendererParameters rendererParameters = new LiveDataRendererParameters();
rendererParameters.setId(liveDataId);
verify(this.liveDataRenderer).render(rendererParameters, (Map<?, ?>) null, false);
verify(this.liveDataRenderer).execute(rendererParameters, (Map<?, ?>) null, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public List<Block> execute(LiveDataMacroParameters parameters, String content, M
{
try {
boolean restricted = context.getTransformationContext().isRestricted();
Block block = this.liveDataRenderer.render(parameters, content, restricted);
return List.of(block);
return List.of(this.liveDataRenderer.execute(parameters, content, restricted));
} catch (LiveDataException e) {
throw new MacroExecutionException("Failed to render the content of the LiveData macro.", e);
}
Expand Down

0 comments on commit f2eec83

Please sign in to comment.