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

Add plugin validations. #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/com/tw/go/plugin/task/GoPluginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,25 @@ private GoPluginApiResponse handleConfiguration() {

private GoPluginApiResponse handleValidation(GoPluginApiRequest goPluginApiRequest) {
Map<String, Object> response = new HashMap<String, Object>();
Map<String, String> errors = new HashMap<String, String>();

Map<String, Map<String, String>> map = new GsonBuilder().create().fromJson(goPluginApiRequest.requestBody(), Map.class);
String scriptValue = map.get("script").get("value");
String shellType = map.get("shtype").get("value");

putInErrors(errors, scriptValue, "script", "Script can't be empty");
putInErrors(errors, shellType, "shtype", "Shell type can't be empty");

response.put("errors", errors);
return renderJSON(SUCCESS_RESPONSE_CODE, response);
}

private void putInErrors(Map<String, String> errors, String value, String name, String errorMsg) {
if (StringUtils.isBlank(value)) {
errors.put(name, errorMsg);
}
}

private GoPluginApiResponse handleView() throws IOException {
Map<String, Object> response = new HashMap<String, Object>();
response.put("displayValue", "Script Executor");
Expand Down
97 changes: 97 additions & 0 deletions test/com/tw/go/plugin/task/GoPluginImplTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.tw.go.plugin.task;

import com.google.gson.GsonBuilder;
import com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException;
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

Expand All @@ -18,4 +25,94 @@ public void shouldCleanupScript() {
assertThat(plugin.cleanupScript("a\rb"), is("a" + lineSeparator + "b"));
assertThat(plugin.cleanupScript("a\r\nb"), is("a" + lineSeparator + "b"));
}

private abstract class GoPluginApiRequestMock extends GoPluginApiRequest {

@Override
public String extension() {
return null;
}

@Override
public String extensionVersion() {
return null;
}

@Override
public String requestName() {
return GoPluginImpl.REQUEST_VALIDATION;
}

@Override
public Map<String, String> requestParameters() {
return null;
}

@Override
public Map<String, String> requestHeaders() {
return null;
}
}

private Map<String,Object> createResponse(Map<String,Object> errors){
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("errors",errors);
return expectedResponse;
}

@Test
public void shouldGiveEmptyErrorsWhenScriptAndShellTypeProvided() throws UnhandledRequestTypeException {
GoPluginApiRequest goPluginApiRequest = new GoPluginApiRequestMock() {
@Override
public String requestBody() {
return "{\"shtype\":{\"secure\":false,\"value\":\"bash\",\"required\":false},\"script\":{\"secure\":false,\"value\":\"ls\",\"required\":false}}";
}
};

GoPluginImpl plugin = new GoPluginImpl();
Map<String, Object> errors = new HashMap<>();
Map<String, Object> expectedResponse = createResponse(errors);
GoPluginApiResponse actualResponse = plugin.handle(goPluginApiRequest);

assertEquals(actualResponse.responseCode(),GoPluginImpl.SUCCESS_RESPONSE_CODE);
assertEquals(actualResponse.responseBody(), new GsonBuilder().create().toJson(expectedResponse));
}

@Test
public void shouldGiveShellTypeErrorWhenItisNotProvided() throws UnhandledRequestTypeException {
GoPluginApiRequest goPluginApiRequest = new GoPluginApiRequestMock() {
@Override
public String requestBody() {
return "{\"shtype\":{\"secure\":false,\"required\":false},\"script\":{\"secure\":false,\"value\":\"ls\",\"required\":false}}";
}
};

GoPluginImpl plugin = new GoPluginImpl();
Map<String, Object> errors = new HashMap<>();
errors.put("shtype","Shell type can't be empty");
Map<String, Object> expectedResponse = createResponse(errors);
GoPluginApiResponse actualResponse = plugin.handle(goPluginApiRequest);

assertEquals(actualResponse.responseCode(),GoPluginImpl.SUCCESS_RESPONSE_CODE);
assertEquals(actualResponse.responseBody(), new GsonBuilder().create().toJson(expectedResponse));
}

@Test
public void shouldGiveScriptErrorWhenItisNotProvided() throws UnhandledRequestTypeException {
GoPluginApiRequest goPluginApiRequest = new GoPluginApiRequestMock() {
@Override
public String requestBody() {
return "{\"shtype\":{\"secure\":false,\"value\":\"bash\",\"required\":false},\"script\":{\"secure\":false,\"required\":false}}";
}
};
GoPluginImpl plugin = new GoPluginImpl();

Map<String, Object> errors = new HashMap<>();
errors.put("script","Script can't be empty");
Map<String, Object> expectedResponse = createResponse(errors);
GoPluginApiResponse actualResponse = plugin.handle(goPluginApiRequest);

assertEquals(actualResponse.responseCode(),GoPluginImpl.SUCCESS_RESPONSE_CODE);
assertEquals(actualResponse.responseBody(), new GsonBuilder().create().toJson(expectedResponse));
}
}