#2213 improve ApiAdminPluginAction

This commit is contained in:
igarashi 2019-08-20 13:56:59 +09:00
parent 1d2b962a1e
commit e84614f443
3 changed files with 96 additions and 53 deletions

View file

@ -19,6 +19,8 @@ import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.getAllIns
import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.getAllAvailableArtifacts;
import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.installArtifact;
import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.deleteArtifact;
import static org.codelibs.fess.helper.PluginHelper.getArtifactFromFileName;
import static org.codelibs.fess.helper.PluginHelper.getArtifactTypeFromFileName;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.helper.PluginHelper.Artifact;
@ -31,28 +33,40 @@ import java.util.Map;
public class ApiAdminPluginAction extends FessApiAdminAction {
@Execute
public JsonResponse<ApiResult> installedplugins() {
public JsonResponse<ApiResult> get$installed() {
final List<Map<String, String>> list = getAllInstalledArtifacts();
return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
}
@Execute
public JsonResponse<ApiResult> availableplugins() {
public JsonResponse<ApiResult> get$available() {
final List<Map<String, String>> list = getAllAvailableArtifacts();
return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
}
@Execute
public JsonResponse<ApiResult> install(final InstallBody body) {
public JsonResponse<ApiResult> put$index(final InstallBody body) {
validateApi(body, messages -> {});
installArtifact(new Artifact(body.name, body.version, body.url));
final Artifact artifact = new Artifact(body.name, body.version, body.url);
if (!isValidBody(artifact)) {
return asJson(new ApiResult.ApiErrorResponse().message("invalid name, version, or url").status(ApiResult.Status.BAD_REQUEST)
.result());
}
installArtifact(artifact);
return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
}
@Execute
public JsonResponse<ApiResult> delete(final DeleteBody body) {
public JsonResponse<ApiResult> delete$index(final DeleteBody body) {
validateApi(body, messages -> {});
deleteArtifact(new Artifact(body.name, body.version, null));
return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
}
protected Boolean isValidBody(final Artifact artifact) {
final String[] tokens = artifact.getUrl().split("/");
final String filenameFromUrl = tokens[tokens.length - 1];
return getArtifactFromFileName(getArtifactTypeFromFileName(filenameFromUrl), filenameFromUrl).getFileName().equals(
artifact.getFileName());
}
}

View file

@ -172,17 +172,25 @@ public class PluginHelper {
return list.toArray(new Artifact[list.size()]);
}
protected Artifact getArtifactFromFileName(final ArtifactType artifactType, final String fileName) {
public static Artifact getArtifactFromFileName(final ArtifactType artifactType, final String fileName) {
final String convertedFileName = fileName.substring(artifactType.getId().length() + 1, fileName.lastIndexOf('.'));
final int firstIndexOfDash = convertedFileName.indexOf("-");
final int firstIndexOfDash = convertedFileName.indexOf('-');
final String artifactName = artifactType.getId() + "-" + convertedFileName.substring(0, firstIndexOfDash);
final String artifactVersion = convertedFileName.substring(firstIndexOfDash + 1);
return new Artifact(artifactName, artifactVersion, null);
}
public static ArtifactType getArtifactTypeFromFileName(final String filename) {
return ArtifactType.getType(new Artifact(filename, null, null));
}
public void installArtifact(final Artifact artifact) {
final String fileName = artifact.getFileName();
try (final CurlResponse response = Curl.get(artifact.getUrl()).execute()) {
if (response.getHttpStatusCode() != 200) {
throw new PluginException("HTTP Status " + response.getHttpStatusCode() + " : failed to get the artifact from "
+ artifact.getUrl());
}
try (final InputStream in = response.getContentAsStream()) {
CopyUtil.copy(in, ResourceUtil.getPluginPath(fileName).toFile());
}

View file

@ -16,87 +16,108 @@
package org.codelibs.fess.it.admin;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
import io.restassured.RestAssured;
import org.codelibs.fess.it.ITBase;
import io.restassured.response.Response;
import org.codelibs.fess.it.CrudTestBase;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@Tag("it")
public class PluginTests extends ITBase {
public class PluginTests extends CrudTestBase {
// TODO delete
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CrudTestBase.class);
private static final String NAME_PREFIX = "pluginTests_";
private static final String API_PATH = "/api/admin/plugin";
private static final String INSTALLEDPLUGINS_ENDPOINT_SUFFIX = "installedplugins";
private static final String AVAILABLEPLUGINS_ENDPOINT_SUFFIX = "availableplugins";
private static final String INSTALL_ENDPOINT_SUFFIX = "install";
private static final String DELETE_ENDPOINT_SUFFIX = "delete";
private static final String INSTALLED_ENDPOINT_SUFFIX = "installed";
private static final String AVAILABLE_ENDPOINT_SUFFIX = "available";
@BeforeAll
protected static void initAll() {
RestAssured.baseURI = getFessUrl();
settingTestToken();
}
@BeforeEach
protected void init() {
}
@AfterEach
protected void tearDown() {
}
@AfterAll
protected static void tearDownAll() {
deleteTestToken();
private static final String LIST_ENDPOINT_SUFFIX = "";
private static final String ITEM_ENDPOINT_SUFFIX = "";
private static final String KEY_PROPERTY = "";
@Override
protected String getNamePrefix() {
return NAME_PREFIX;
}
@Override
protected String getApiPath() {
return API_PATH;
}
protected String getInstalledpluginsEndpointSuffix() {
return INSTALLEDPLUGINS_ENDPOINT_SUFFIX;
@Override
protected String getKeyProperty() {
return KEY_PROPERTY;
}
protected String getAvailablepluginsEndpointSuffix() {
return AVAILABLEPLUGINS_ENDPOINT_SUFFIX;
@Override
protected String getListEndpointSuffix() {
return LIST_ENDPOINT_SUFFIX;
}
protected String getInstallEndpointSuffix() {
return INSTALL_ENDPOINT_SUFFIX;
@Override
protected String getItemEndpointSuffix() {
return ITEM_ENDPOINT_SUFFIX;
}
protected String getDeleteEndpointSuffix() {
return DELETE_ENDPOINT_SUFFIX;
@Override
protected Map<String, Object> createTestParam(int id) {
final Map<String, Object> requestBody = new HashMap<>();
return requestBody;
}
@Override
protected Map<String, Object> getUpdateMap() {
final Map<String, Object> updateMap = new HashMap<>();
return updateMap;
}
@AfterEach
protected void tearDown() {
// do nothing
}
protected String getInstalledEndpointSuffix() {
return INSTALLED_ENDPOINT_SUFFIX;
}
protected String getAvailableEndpointSuffix() {
return AVAILABLE_ENDPOINT_SUFFIX;
}
protected Response checkDeleteMethod(final Map<String, Object> body) {
return checkMethodBase(body).delete(getApiPath() + "/");
}
@Test
void testInstalledplugins() {
checkMethodBase(Collections.emptyMap()).get(getApiPath() + "/" + getInstalledpluginsEndpointSuffix() + "/").then()
.body("response.status", equalTo(0));
void testInstalled() {
checkGetMethod(Collections.emptyMap(), getInstalledEndpointSuffix() + "/").then().body("response.status", equalTo(0));
}
@Test
void testAvailavleplugins() {
checkMethodBase(Collections.emptyMap()).get(getApiPath() + "/" + getAvailablepluginsEndpointSuffix() + "/").then()
.body("response.status", equalTo(0));
void testAvailable() {
checkGetMethod(Collections.emptyMap(), getAvailableEndpointSuffix() + "/").then().body("response.status", equalTo(0));
}
@Test
void testIntall() {
checkMethodBase(Collections.emptyMap()).get(getApiPath() + "/" + getInstallEndpointSuffix() + "/").then()
.body("response.status", equalTo(0));
checkPutMethod(Collections.emptyMap(), "").then().body("response.status", equalTo(1));
}
@Test
void testDelete() {
checkMethodBase(Collections.emptyMap()).get(getApiPath() + "/" + getDeleteEndpointSuffix() + "/").then()
.body("response.status", equalTo(0));
void testUninstall() {
checkDeleteMethod(Collections.emptyMap()).then().body("response.status", equalTo(1));
}
@Test
void testCRUD() {
// TODO
}
}