Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored some code and added module name information to Call classes #197

Merged
merged 2 commits into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/main/java/com/suse/salt/netapi/calls/AbstractCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.suse.salt.netapi.calls;

import com.google.gson.reflect.TypeToken;
import com.suse.salt.netapi.utils.ClientUtils;

/**
* Abstract class for all function calls in salt.
*
* @param <R> the return type of the called function
*/
public abstract class AbstractCall<R> implements Call<R> {

private final String moduleName;
private final String functionName;
private final TypeToken<R> returnType;

AbstractCall(String functionName, TypeToken<R> returnType) {
this.moduleName = ClientUtils.getModuleNameFromFunction(functionName);
this.functionName = functionName;
this.returnType = returnType;
}

AbstractCall(String moduleName, String functionName, TypeToken<R> returnType) {
this.moduleName = moduleName;
this.functionName = functionName;
this.returnType = returnType;
}

/**
* Returns the module name
*/
public String getModuleName()
{
return moduleName;
}

/**
* Return the function name
* @return functionName
*/
String getFunctionName() {
return functionName;
}

/**
* Reurn the type
* @return returnType
*/
public TypeToken<R> getReturnType() {
return returnType;
}


}
25 changes: 9 additions & 16 deletions src/main/java/com/suse/salt/netapi/calls/LocalCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@
*
* @param <R> the return type of the called function
*/
public class LocalCall<R> implements Call<R> {
public class LocalCall<R> extends AbstractCall<R> {

private final String functionName;
private final Optional<List<?>> arg;
private final Optional<Map<String, ?>> kwarg;
private final TypeToken<R> returnType;
private final Optional<?> metadata;
private final Optional<Integer> timeout;
private final Optional<Integer> gatherJobTimeout;
Expand All @@ -52,10 +50,9 @@ public LocalCall(String functionName, Optional<List<?>> arg,
Optional<Map<String, ?>> kwarg, TypeToken<R> returnType,
Optional<?> metadata, Optional<Integer> timeout,
Optional<Integer> gatherJobTimeout) {
this.functionName = functionName;
super(functionName, returnType);
this.arg = arg;
this.kwarg = kwarg;
this.returnType = returnType;
this.metadata = metadata;
this.timeout = timeout;
this.gatherJobTimeout = gatherJobTimeout;
Expand All @@ -81,37 +78,33 @@ public LocalCall(String functionName, Optional<List<?>> arg,
}

public LocalCall<R> withMetadata(Object metadata) {
return new LocalCall<>(functionName, arg, kwarg, returnType, Optional.of(metadata),
timeout, gatherJobTimeout);
return new LocalCall<>(getFunctionName(), arg, kwarg, getReturnType(),
Optional.of(metadata), timeout, gatherJobTimeout);
}

public LocalCall<R> withoutMetadata() {
return new LocalCall<>(functionName, arg, kwarg, returnType, Optional.empty(),
timeout, gatherJobTimeout);
return new LocalCall<>(getFunctionName(), arg, kwarg, getReturnType(),
Optional.empty(), timeout, gatherJobTimeout);
}

public LocalCall<R> withTimeouts(Optional<Integer> timeout,
Optional<Integer> gatherJobTimeout) {
return new LocalCall<>(functionName, arg, kwarg, returnType, metadata,
return new LocalCall<>(getFunctionName(), arg, kwarg, getReturnType(), metadata,
timeout, gatherJobTimeout);
}

public LocalCall<R> withoutTimeouts() {
return new LocalCall<>(functionName, arg, kwarg, returnType, metadata,
return new LocalCall<>(getFunctionName(), arg, kwarg, getReturnType(), metadata,
Optional.empty(), Optional.empty());
}

public TypeToken<R> getReturnType() {
return returnType;
}

/**
* {@inheritDoc}
*/
@Override
public Map<String, Object> getPayload() {
HashMap<String, Object> payload = new HashMap<>();
payload.put("fun", functionName);
payload.put("fun", getFunctionName());
arg.ifPresent(arg -> payload.put("arg", arg));
kwarg.ifPresent(kwarg -> payload.put("kwarg", kwarg));
metadata.ifPresent(m -> payload.put("metadata", m));
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/com/suse/salt/netapi/calls/RunnerCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@
*
* @param <R> the return type of the called function
*/
public class RunnerCall<R> implements Call<R> {
public class RunnerCall<R> extends AbstractCall<R> {

private final String functionName;
private final Optional<Map<String, ?>> kwargs;
private final TypeToken<R> returnType;

public RunnerCall(String functionName, Optional<Map<String, ?>> kwargs,
TypeToken<R> returnType) {
this.functionName = functionName;
super(functionName, returnType);
this.kwargs = kwargs;
this.returnType = returnType;
}

public TypeToken<R> getReturnType() {
return returnType;
}

/**
Expand All @@ -43,7 +36,7 @@ public TypeToken<R> getReturnType() {
@Override
public Map<String, Object> getPayload() {
HashMap<String, Object> payload = new HashMap<>();
payload.put("fun", functionName);
payload.put("fun", getFunctionName());
kwargs.ifPresent(kwargs -> payload.put("kwarg", kwargs));
return payload;
}
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/com/suse/salt/netapi/calls/WheelCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@
*
* @param <R> the return type of the called function
*/
public class WheelCall<R> implements Call<R> {
public class WheelCall<R> extends AbstractCall<R> {

private final String functionName;
private final Optional<Map<String, ?>> kwargs;
private final TypeToken<R> returnType;

public WheelCall(String functionName, Optional<Map<String, ?>> kwargs,
TypeToken<R> returnType) {
this.functionName = functionName;
super(functionName, returnType);
this.kwargs = kwargs;
this.returnType = returnType;
}

public TypeToken<R> getReturnType() {
return returnType;
}

/**
Expand All @@ -43,7 +37,7 @@ public TypeToken<R> getReturnType() {
@Override
public Map<String, Object> getPayload() {
HashMap<String, Object> payload = new HashMap<>();
payload.put("fun", functionName);
payload.put("fun", getFunctionName());
kwargs.ifPresent(payload::putAll);
return payload;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/suse/salt/netapi/utils/ClientUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,19 @@ public static ParameterizedType parameterizedType(Type ownerType, Type rawType,
Type... typeArguments) {
return newParameterizedTypeWithOwner(ownerType, rawType, typeArguments);
}

/**
* Get the name from function name e.g in case of 'state.high', this function will
* return 'state'.
* @param functionName
* @return module name
* @throws IllegalArgumentException
*/
public static String getModuleNameFromFunction(final String functionName)
throws IllegalArgumentException {
if (!functionName.contains(".")) {
throw new IllegalArgumentException(functionName);
}
return functionName.split("\\.")[0];
}
}
23 changes: 22 additions & 1 deletion src/test/java/com/suse/salt/netapi/calls/LocalCallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.suse.salt.netapi.datatypes.target.Target;
import com.suse.salt.netapi.exception.SaltException;
import com.suse.salt.netapi.utils.ClientUtils;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.gson.reflect.TypeToken;

import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -105,6 +105,27 @@ public void testWithTimeouts() {
assertEquals(runWithTimeouts.getPayload().get("timeout"), 4);
assertEquals(runWithTimeouts.getPayload().get("gather_job_timeout"), 1);
}
/**
* Verify that system return the correct module name
*/

@Test
public void testModuleName() {
LocalCall<String> run = Cmd.run("echo 'hello world'");
assertEquals(run.getModuleName(), "cmd");

}

/**
* Verify that system throw IllegalArgumentException when function name is not in right
* format.
*/
@Test(expected = IllegalArgumentException.class)
public void testFunctionName() {
LocalCall<String> run = new LocalCall<>("cmdrun", Optional.empty(),
Optional.empty(), new TypeToken<String>(){});
System.out.println(run.getModuleName());
}

/**
* Verify correctness of the request body with an exemplary synchronous call.
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/suse/salt/netapi/utils/ClientUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@ public void testStreamToString() {
assertEquals("Result doesn't match test string", TEST_STRING, result);
}

@Test
public void testGetModuleNameFromRightFunction() {
final String TEST_STRING = "state.high";
String result = ClientUtils.getModuleNameFromFunction(TEST_STRING);
assertEquals("Result doesn't match test string", result, "state");
}

@Test(expected = IllegalArgumentException.class)
public void testGetModuleNameFromWrongFunction() {
final String TEST_STRING = "statehigh";
ClientUtils.getModuleNameFromFunction(TEST_STRING);
}

}