Instance type filter polishing and tests

This commit is contained in:
Arnold Galovics
2022-06-06 13:59:11 +02:00
committed by Arnold Gálovics
parent 367321d76d
commit ef2d42fe32
21 changed files with 809 additions and 94 deletions
@@ -0,0 +1,149 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fineract.integrationtests;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import org.apache.fineract.client.models.GetOfficesResponse;
import org.apache.fineract.client.models.PostClientsRequest;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.apache.fineract.integrationtests.common.OfficeHelper;
import org.apache.fineract.integrationtests.common.SchedulerJobHelper;
import org.apache.fineract.integrationtests.common.Utils;
import org.apache.fineract.integrationtests.support.instancemode.ConfigureInstanceMode;
import org.apache.fineract.integrationtests.support.instancemode.InstanceModeSupportExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith(InstanceModeSupportExtension.class)
public class InstanceModeIntegrationTest {
private ResponseSpecification responseSpec200;
private ResponseSpecification responseSpec405;
private RequestSpecification requestSpec;
private SchedulerJobHelper schedulerJobHelper;
private int jobId;
@BeforeEach
public void setup() throws InterruptedException {
Utils.initializeRESTAssured();
requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build();
requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey());
responseSpec200 = new ResponseSpecBuilder().expectStatusCode(200).build();
responseSpec405 = new ResponseSpecBuilder().expectStatusCode(405).build();
schedulerJobHelper = new SchedulerJobHelper(requestSpec);
String jobName = "Apply Annual Fee For Savings";
jobId = schedulerJobHelper.getSchedulerJobIdByName(jobName);
}
@ConfigureInstanceMode(readEnabled = true, writeEnabled = false, batchEnabled = false)
@Test
public void testGetHeadOfficeWorks_WhenInstanceModeIsReadOnly() {
// given
// when
GetOfficesResponse result = OfficeHelper.getHeadOffice(requestSpec, responseSpec200);
// then
assertNotNull(result);
}
@ConfigureInstanceMode(readEnabled = false, writeEnabled = true, batchEnabled = false)
@Test
public void testGetHeadOfficeWorks_WhenInstanceModeIsWriteOnly() {
// given
// when
GetOfficesResponse result = OfficeHelper.getHeadOffice(requestSpec, responseSpec200);
// then
assertNotNull(result);
}
@ConfigureInstanceMode(readEnabled = false, writeEnabled = false, batchEnabled = true)
@Test
public void testGetHeadOfficeDoesntWork_WhenInstanceModeIsBatchOnly() {
// given
// when
OfficeHelper.getHeadOffice(requestSpec, responseSpec405);
// then no exception is thrown
}
@ConfigureInstanceMode(readEnabled = true, writeEnabled = false, batchEnabled = false)
@Test
public void testCreateClientDoesntWork_WhenReadOnly() {
// given
PostClientsRequest request = ClientHelper.defaultClientCreationRequest();
// when
ClientHelper.createClient(requestSpec, responseSpec405, request);
// then no exception thrown
}
@ConfigureInstanceMode(readEnabled = false, writeEnabled = true, batchEnabled = false)
@Test
public void testCreateClientWorks_WhenWriteOnly() {
// given
PostClientsRequest request = ClientHelper.defaultClientCreationRequest();
// when
Integer result = ClientHelper.createClient(requestSpec, responseSpec200, request);
// then
assertNotNull(result);
}
@ConfigureInstanceMode(readEnabled = false, writeEnabled = false, batchEnabled = true)
@Test
public void testCreateClientDoesntWork_WhenBatchOnly() {
// given
PostClientsRequest request = ClientHelper.defaultClientCreationRequest();
// when
ClientHelper.createClient(requestSpec, responseSpec405, request);
// then no exception thrown
}
@ConfigureInstanceMode(readEnabled = true, writeEnabled = false, batchEnabled = false)
@Test
public void testRunSchedulerJobDoesntWork_WhenReadOnly() {
// given
// when
schedulerJobHelper.runSchedulerJob(jobId, responseSpec405);
// then no exception thrown
}
@ConfigureInstanceMode(readEnabled = false, writeEnabled = true, batchEnabled = false)
@Test
public void testRunSchedulerJobDoesntWork_WhenWriteOnly() {
// given
// when
schedulerJobHelper.runSchedulerJob(jobId, responseSpec405);
// then no exception thrown
}
@ConfigureInstanceMode(readEnabled = false, writeEnabled = false, batchEnabled = true)
@Test
public void testRunSchedulerJobWorks_WhenBatchOnly() {
// given
// when
schedulerJobHelper.runSchedulerJob(jobId);
// then no exception thrown
}
}
@@ -113,8 +113,12 @@ public class SchedulerJobHelper {
return new Gson().toJson(map);
}
private void runSchedulerJob(int jobId) {
public void runSchedulerJob(int jobId) {
final ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(202).build();
runSchedulerJob(jobId, responseSpec);
}
public void runSchedulerJob(int jobId, ResponseSpecification responseSpec) {
final String RUN_SCHEDULER_JOB_URL = "/fineract-provider/api/v1/jobs/" + jobId + "?command=executeJob&" + Utils.TENANT_IDENTIFIER;
LOG.info("------------------------ RUN SCHEDULER JOB -------------------------");
Utils.performServerPost(requestSpec, responseSpec, RUN_SCHEDULER_JOB_URL, runSchedulerJobAsJSON(), null);
@@ -127,7 +131,7 @@ public class SchedulerJobHelper {
return runSchedulerJob;
}
private int getSchedulerJobIdByName(String jobName) {
public int getSchedulerJobIdByName(String jobName) {
List<Map<String, Object>> allSchedulerJobsData = getAllSchedulerJobs();
for (Integer jobIndex = 0; jobIndex < allSchedulerJobsData.size(); jobIndex++) {
if (allSchedulerJobsData.get(jobIndex).get("displayName").equals(jobName)) {
@@ -204,10 +204,18 @@ public final class Utils {
return (T) JsonPath.from(json).get(jsonAttributeToGetBack);
}
public static String performServerPut(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String putURL, final String jsonBodyToSend) {
return performServerPut(requestSpec, responseSpec, putURL, jsonBodyToSend, null);
}
public static <T> T performServerPut(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String putURL, final String jsonBodyToSend, final String jsonAttributeToGetBack) {
final String json = given().spec(requestSpec).body(jsonBodyToSend).expect().spec(responseSpec).log().ifError().when().put(putURL)
.andReturn().asString();
if (jsonAttributeToGetBack == null) {
return (T) json;
}
return (T) JsonPath.from(json).get(jsonAttributeToGetBack);
}
@@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fineract.integrationtests.support.instancemode;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target({ METHOD })
public @interface ConfigureInstanceMode {
boolean readEnabled();
boolean writeEnabled();
boolean batchEnabled();
}
@@ -0,0 +1,40 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fineract.integrationtests.support.instancemode;
import com.google.gson.Gson;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import org.apache.fineract.client.models.ChangeInstanceModeRequest;
import org.apache.fineract.client.util.JSON;
import org.apache.fineract.integrationtests.common.Utils;
@SuppressWarnings({ "HideUtilityClassConstructor" })
public class InstanceModeHelper {
private static final Gson GSON = new JSON().getGson();
public static void changeMode(RequestSpecification requestSpec, ResponseSpecification responseSpec, boolean readEnabled,
boolean writeEnabled, boolean batchEnabled) {
ChangeInstanceModeRequest request = new ChangeInstanceModeRequest().readEnabled(readEnabled).writeEnabled(writeEnabled)
.batchEnabled(batchEnabled);
String requestStr = GSON.toJson(request);
Utils.performServerPut(requestSpec, responseSpec, "/fineract-provider/api/v1/instance-mode?" + Utils.TENANT_IDENTIFIER, requestStr);
}
}
@@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fineract.integrationtests.support.instancemode;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import org.apache.fineract.integrationtests.common.Utils;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
public class InstanceModeSupportExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private static final Namespace INSTANCE_MODE_NAMESPACE = Namespace.create(InstanceModeSupportExtension.class);
private static final String AUTH_KEY = "AUTH_KEY";
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
context.getTestMethod().ifPresent(m -> {
ConfigureInstanceMode annotation = m.getAnnotation(ConfigureInstanceMode.class);
if (annotation != null) {
boolean readEnabled = annotation.readEnabled();
boolean writeEnabled = annotation.writeEnabled();
boolean batchEnabled = annotation.batchEnabled();
changeInstanceMode(context, readEnabled, writeEnabled, batchEnabled);
}
});
}
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
context.getTestMethod().ifPresent(m -> {
ConfigureInstanceMode annotation = m.getAnnotation(ConfigureInstanceMode.class);
if (annotation != null) {
changeInstanceMode(context, true, true, true);
}
});
}
private void changeInstanceMode(ExtensionContext extensionContext, boolean readEnabled, boolean writeEnabled, boolean batchEnabled) {
Store store = extensionContext.getStore(INSTANCE_MODE_NAMESPACE);
String authKey = store.getOrComputeIfAbsent(AUTH_KEY, k -> Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey(),
String.class);
RequestSpecification requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build();
ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
requestSpec.header("Authorization", "Basic " + authKey);
InstanceModeHelper.changeMode(requestSpec, responseSpec, readEnabled, writeEnabled, batchEnabled);
}
}