Browse Source

fix #1017 Admin API Test: /api/admin/badword

Keiichi Watanabe 8 years ago
parent
commit
456b391404

+ 32 - 20
src/main/java/org/codelibs/fess/app/web/api/admin/badword/ApiAdminBadwordAction.java

@@ -36,6 +36,8 @@ import org.codelibs.fess.app.service.BadWordService;
 import org.codelibs.fess.app.web.CrudMode;
 import org.codelibs.fess.app.web.admin.badword.UploadForm;
 import org.codelibs.fess.app.web.api.ApiResult;
+import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
+import org.codelibs.fess.app.web.api.ApiResult.Status;
 import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
 import org.codelibs.fess.es.config.exentity.BadWord;
 import org.codelibs.fess.exception.FessSystemException;
@@ -52,8 +54,8 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
     @Resource
     protected SuggestHelper suggestHelper;
 
-    // GET /api/admin/badword
-    // POST /api/admin/badword
+    // GET /api/admin/badword/settings
+    // POST /api/admin/badword/settings
     @Execute
     public JsonResponse<ApiResult> settings(final SearchBody body) {
         validateApi(body, messages -> {});
@@ -64,6 +66,19 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
                 .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
     }
 
+    // GET /api/admin/badword/{id}
+    @Execute
+    public JsonResponse<ApiResult> get$setting(final String id) {
+
+        final BadWord entity = badWordService.getBadWord(id).orElseGet(() -> {
+            throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
+            return null;
+        });
+
+        final EditBody body = createEditBody(entity);
+        return asJson(new ApiResult.ApiConfigResponse().setting(body).status(ApiResult.Status.OK).result());
+    }
+
     // PUT /api/admin/badword/setting
     @Execute
     public JsonResponse<ApiResult> put$setting(final CreateBody body) {
@@ -89,19 +104,20 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
     public JsonResponse<ApiResult> post$setting(final EditBody body) {
         validateApi(body, messages -> {});
         body.crudMode = CrudMode.EDIT;
-        final BadWord entity = getBadWord(body).orElseGet(() -> {
-            throwValidationErrorApi(messages -> {
-                messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
-            });
+        final BadWord badWord = getBadWord(body).map(entity -> {
+            try {
+                badWordService.store(entity);
+                suggestHelper.storeAllBadWords();
+            } catch (final Exception e) {
+                throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
+            }
+            return entity;
+        }).orElseGet(() -> {
+            throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
             return null;
         });
-        try {
-            badWordService.store(entity);
-            suggestHelper.storeAllBadWords();
-        } catch (final Exception e) {
-            throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
-        }
-        return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
+
+        return asJson(new ApiUpdateResponse().id(badWord.getId()).created(false).status(Status.OK).result());
     }
 
     // DELETE /api/admin/badword/setting/{id}
@@ -163,13 +179,9 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
 
     protected EditBody createEditBody(final BadWord entity) {
         final EditBody body = new EditBody();
-        body.id = entity.getId();
-        body.versionNo = entity.getVersionNo();
-        body.createdBy = entity.getCreatedBy();
-        body.createdTime = entity.getCreatedTime();
-        body.suggestWord = entity.getSuggestWord();
-        body.updatedBy = entity.getUpdatedBy();
-        body.updatedTime = entity.getUpdatedTime();
+        copyBeanToBean(entity, body, copyOp -> {
+            copyOp.excludeNull();
+        });
         return body;
     }
 

+ 82 - 0
src/test/java/org/codelibs/fess/it/admin/BadWordTests.java

@@ -0,0 +1,82 @@
+/*
+ * Copyright 2012-2017 CodeLibs Project and the Others.
+ *
+ * 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 org.codelibs.fess.it.admin;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codelibs.fess.it.CrudTestBase;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+@Tag("it")
+public class BadWordTests extends CrudTestBase {
+
+    private static final String NAME_PREFIX = "badWordTest_";
+    private static final String API_PATH = "/api/admin/badword";
+    private static final String LIST_ENDPOINT_SUFFIX = "settings";
+    private static final String ITEM_ENDPOINT_SUFFIX = "setting";
+
+    private static final String KEY_PROPERTY = "suggest_word";
+
+    @Override
+    protected String getNamePrefix() {
+        return NAME_PREFIX;
+    }
+
+    @Override
+    protected String getApiPath() {
+        return API_PATH;
+    }
+
+    @Override
+    protected String getKeyProperty() {
+        return KEY_PROPERTY;
+    }
+
+    @Override
+    protected String getListEndpointSuffix() {
+        return LIST_ENDPOINT_SUFFIX;
+    }
+
+    @Override
+    protected String getItemEndpointSuffix() {
+        return ITEM_ENDPOINT_SUFFIX;
+    }
+
+    @Override
+    protected Map<String, Object> createTestParam(int id) {
+        final Map<String, Object> requestBody = new HashMap<>();
+        final String keyProp = NAME_PREFIX + id;
+        requestBody.put(KEY_PROPERTY, keyProp);
+        return requestBody;
+    }
+
+    @Override
+    protected Map<String, Object> getUpdateMap() {
+        final Map<String, Object> updateMap = new HashMap<>();
+        updateMap.put(KEY_PROPERTY, NAME_PREFIX + "new");
+        return updateMap;
+    }
+
+    @Test
+    void crudTest() {
+        testCreate();
+        testRead();
+        testUpdate();
+        testDelete();
+    }
+}