Skip to content

Commit

Permalink
FunctionType enum introduced.
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmolev committed Nov 22, 2023
1 parent 7549748 commit 1078135
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 30 deletions.
1 change: 0 additions & 1 deletion docs/_docs/hivemq/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ $ mqtt hivemq script get --id hello_world
"functionType": "TRANSFORMATION",
"source": "ZnVuY3Rpb24gdHJhbnNmb3JtKHBlcnNvbikgeyByZXR1cm4gJ2hlbGxvICcgKyBwZXJzb24gfQ=="
}
```

***
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019-present HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hivemq.cli.commands.hivemq.datahub;

import org.jetbrains.annotations.NotNull;

public enum FunctionType {

TRANSFORMATION();

public static @NotNull FunctionType valueOfIgnoreCase(final @NotNull String typeName) throws Exception {
for (final FunctionType value : values()) {
if (value.name().equalsIgnoreCase(typeName)) {
return value;
}
}
throw new Exception("Value must be transformation only");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.commands.hivemq.datahub.DataHubOptions;
import com.hivemq.cli.commands.hivemq.datahub.FunctionType;
import com.hivemq.cli.commands.hivemq.datahub.OutputFormatter;
import com.hivemq.cli.commands.hivemq.datahub.ScriptDefinitionOptions;
import com.hivemq.cli.converters.ScriptTypeConverter;
import com.hivemq.cli.converters.FunctionTypeConverter;
import com.hivemq.cli.hivemq.scripts.CreateScriptTask;
import com.hivemq.cli.openapi.hivemq.DataHubScriptsApi;
import com.hivemq.cli.rest.HiveMQRestService;
Expand Down Expand Up @@ -53,9 +54,9 @@ public class ScriptCreateCommand implements Callable<Integer> {
@SuppressWarnings({"NotNullFieldNotInitialized", "unused"})
@CommandLine.Option(names = {"--type"},
required = true,
converter = ScriptTypeConverter.class,
converter = FunctionTypeConverter.class,
description = "The function type")
private @NotNull String functionType;
private @NotNull FunctionType functionType;

@SuppressWarnings("unused")
@CommandLine.Option(names = {"--print-version"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.commands.hivemq.datahub.DataHubOptions;
import com.hivemq.cli.commands.hivemq.datahub.FunctionType;
import com.hivemq.cli.commands.hivemq.datahub.OutputFormatter;
import com.hivemq.cli.converters.ScriptTypeConverter;
import com.hivemq.cli.converters.FunctionTypeConverter;
import com.hivemq.cli.hivemq.scripts.ListScriptsTask;
import com.hivemq.cli.openapi.hivemq.DataHubScriptsApi;
import com.hivemq.cli.rest.HiveMQRestService;
Expand All @@ -45,9 +46,9 @@ public class ScriptListCommand implements Callable<Integer> {

@SuppressWarnings("unused")
@CommandLine.Option(names = {"-t", "--type"},
converter = ScriptTypeConverter.class,
converter = FunctionTypeConverter.class,
description = "Filter by script type")
private @Nullable String @Nullable [] functionTypes;
private @Nullable FunctionType @Nullable [] functionTypes;

@SuppressWarnings("unused")
@CommandLine.Option(names = {"-i", "--id"}, description = "Filter by script id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@

package com.hivemq.cli.converters;

import com.hivemq.cli.commands.hivemq.datahub.FunctionType;
import org.jetbrains.annotations.NotNull;
import picocli.CommandLine;

public class ScriptTypeConverter implements CommandLine.ITypeConverter<String> {

static final @NotNull String WRONG_INPUT_MESSAGE = "Value must be transformation only";
public class FunctionTypeConverter implements CommandLine.ITypeConverter<FunctionType> {

@Override
public @NotNull String convert(final @NotNull String s) throws Exception {
switch (s.toLowerCase()) {
case "transformation":
return "TRANSFORMATION";
default:
throw new Exception(WRONG_INPUT_MESSAGE);
}
public @NotNull FunctionType convert(final @NotNull String s) throws Exception {
return FunctionType.valueOfIgnoreCase(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.hivemq.cli.commands.hivemq.datahub.FunctionType;
import com.hivemq.cli.commands.hivemq.datahub.OutputFormatter;
import com.hivemq.cli.openapi.ApiException;
import com.hivemq.cli.openapi.hivemq.DataHubScriptsApi;
import com.hivemq.cli.openapi.hivemq.Script;
import org.jetbrains.annotations.NotNull;

import java.nio.ByteBuffer;
import java.util.Base64;

public class CreateScriptTask {

private final @NotNull OutputFormatter outputFormatter;
private final @NotNull DataHubScriptsApi scriptsApi;
private final @NotNull String scriptId;
private final @NotNull String functionType;
private final @NotNull FunctionType functionType;
private final @NotNull String description;
private final @NotNull ByteBuffer definition;
private final boolean printVersion;
Expand All @@ -40,7 +42,7 @@ public CreateScriptTask(
final @NotNull OutputFormatter outputFormatter,
final @NotNull DataHubScriptsApi scriptsApi,
final @NotNull String scriptId,
final @NotNull String functionType,
final @NotNull FunctionType functionType,
final @NotNull String description,
final boolean printVersion,
final @NotNull ByteBuffer definition) {
Expand All @@ -54,8 +56,8 @@ public CreateScriptTask(
}

public boolean execute() {
final String definitionBase64 = java.util.Base64.getEncoder().encodeToString(definition.array());
final Script script = new Script().id(scriptId).functionType(functionType).description(description).source(definitionBase64);
final String definitionBase64 = Base64.getEncoder().encodeToString(definition.array());
final Script script = new Script().id(scriptId).functionType(functionType.name()).description(description).source(definitionBase64);

final Script createdScript;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hivemq.cli.hivemq.scripts;

import com.hivemq.cli.commands.hivemq.datahub.FunctionType;
import com.hivemq.cli.commands.hivemq.datahub.OutputFormatter;
import com.hivemq.cli.openapi.ApiException;
import com.hivemq.cli.openapi.hivemq.DataHubScriptsApi;
Expand All @@ -26,6 +27,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -37,15 +39,15 @@ public class ListScriptsTask {

private final @NotNull OutputFormatter outputFormatter;
private final @NotNull DataHubScriptsApi scriptsApi;
private final @Nullable String @Nullable [] functionTypes;
private final @Nullable FunctionType @Nullable [] functionTypes;
private final @Nullable String @Nullable [] scriptIds;
private final @Nullable String @Nullable [] fields;
private final @Nullable Integer limit;

public ListScriptsTask(
final @NotNull OutputFormatter outputFormatter,
final @NotNull DataHubScriptsApi scriptApi,
final @Nullable String @Nullable [] functionTypes,
final @Nullable FunctionType @Nullable [] functionTypes,
final @Nullable String @Nullable [] scriptIds,
final @Nullable String @Nullable [] fields,
final @Nullable Integer limit) {
Expand Down Expand Up @@ -76,7 +78,8 @@ public boolean execute() {
if (functionTypes == null) {
functionTypesQueryParam = null;
} else {
functionTypesQueryParam = String.join(",", functionTypes);
functionTypesQueryParam = Arrays.stream(functionTypes)
.map(Enum::name).collect(Collectors.joining(","));
}

List<Script> allScripts = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.hivemq.cli.commands.hivemq.datahub.FunctionType;
import com.hivemq.cli.commands.hivemq.datahub.OutputFormatter;
import com.hivemq.cli.openapi.ApiException;
import com.hivemq.cli.openapi.hivemq.DataHubScriptsApi;
Expand Down Expand Up @@ -57,7 +58,7 @@ void execute_validScript_created() throws ApiException {
final CreateScriptTask task = new CreateScriptTask(outputFormatter,
scriptsApi,
"script-1",
"TRANSFORMATION",
FunctionType.TRANSFORMATION,
"Sample Script",
false,
ByteBuffer.wrap(SCRIPT_DEFINITION.getBytes()));
Expand All @@ -80,7 +81,7 @@ void execute_exceptionThrown_printError() throws ApiException {
final CreateScriptTask task = new CreateScriptTask(outputFormatter,
scriptsApi,
"script-1",
"TRANSFORMATION",
FunctionType.TRANSFORMATION,
"Sample Script",
false,
ByteBuffer.wrap(new byte[]{}));
Expand All @@ -95,7 +96,7 @@ void execute_printVersion_printsVersion() throws ApiException {
final CreateScriptTask task = new CreateScriptTask(outputFormatter,
scriptsApi,
"script-1",
"TRANSFORMATION",
FunctionType.TRANSFORMATION,
"Sample Script",
true,
ByteBuffer.wrap(SCRIPT_DEFINITION.getBytes()));
Expand All @@ -109,7 +110,7 @@ void execute_printVersion_printsVersion() throws ApiException {
assertEquals("script-1", createdScript.getId());
assertEquals("TRANSFORMATION", createdScript.getFunctionType());
assertEquals("Sample Script", createdScript.getDescription());
final String createdScriptDefinition = new String(java.util.Base64.getDecoder().decode(createdScript.getSource()));
final String createdScriptDefinition = new String(Base64.getDecoder().decode(createdScript.getSource()));
assertEquals(SCRIPT_DEFINITION, createdScriptDefinition);

final JsonObject versionObject = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hivemq.cli.hivemq.scripts;

import com.hivemq.cli.commands.hivemq.datahub.FunctionType;
import com.hivemq.cli.commands.hivemq.datahub.OutputFormatter;
import com.hivemq.cli.openapi.ApiException;
import com.hivemq.cli.openapi.hivemq.DataHubScriptsApi;
Expand Down Expand Up @@ -59,13 +60,13 @@ void execute_scriptIdsProvided_usedAsUrlParameter() throws ApiException {

@Test
void execute_functionTypesProvided_usedAsUrlParameter() throws ApiException {
final String[] functionTypes = {"type-a", "type-b", "type-c"};
final FunctionType[] functionTypes = {FunctionType.TRANSFORMATION, FunctionType.TRANSFORMATION, FunctionType.TRANSFORMATION};
final ListScriptsTask task = new ListScriptsTask(outputFormatter, scriptsApi, functionTypes, null, null, null);

when(scriptsApi.getAllScripts(any(), any(), any(), any(), any())).thenReturn(new ScriptList());

assertTrue(task.execute());
final String functionTypesQueryParam = "type-a,type-b,type-c";
final String functionTypesQueryParam = "TRANSFORMATION,TRANSFORMATION,TRANSFORMATION";
verify(scriptsApi, times(1)).getAllScripts(isNull(), eq(functionTypesQueryParam), isNull(), any(), isNull());
}

Expand Down

0 comments on commit 1078135

Please sign in to comment.