Skip to content

Commit

Permalink
Fixed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashi1993 committed Mar 26, 2024
1 parent 0a8b134 commit 910e968
Showing 1 changed file with 3 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.wso2.openbanking.accelerator.common.config.OpenBankingConfigParser;
import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException;
import com.wso2.openbanking.accelerator.common.util.CarbonUtils;
import com.wso2.openbanking.accelerator.consent.extensions.internal.ConsentExtensionsDataHolder;
import com.wso2.openbanking.accelerator.consent.extensions.manage.model.ConsentManageData;
import com.wso2.openbanking.accelerator.consent.mgt.dao.models.DetailedConsentResource;
Expand All @@ -30,12 +29,12 @@
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Field;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -47,7 +46,7 @@
*/
@PrepareForTest({OpenBankingConfigParser.class, ConsentExtensionsDataHolder.class})
@PowerMockIgnore("jdk.internal.reflect.*")
public class IdempotencyValidatorTests {
public class IdempotencyValidatorTests extends PowerMockTestCase {

@Mock
private ConsentManageData consentManageData;
Expand Down Expand Up @@ -114,15 +113,9 @@ public class IdempotencyValidatorTests {


@BeforeClass
public void beforeTest() throws ReflectiveOperationException {
public void beforeTest() {
configs = new HashMap<>();

//to execute util class initialization
new CarbonUtils();
System.setProperty("some.property", "property.value");
System.setProperty("carbon.home", ".");
injectEnvironmentVariable("CARBON_HOME", ".");

headers = new HashMap<>();
headers.put(IdempotencyConstants.X_IDEMPOTENCY_KEY, "123456");
headers.put(IdempotencyConstants.CONTENT_TYPE_TAG, "application/json");
Expand Down Expand Up @@ -287,35 +280,4 @@ private DetailedConsentResource getConsent(long createdTime) {
consent.setCreatedTime(createdTime);
return consent;
}

public static void injectEnvironmentVariable(String key, String value)
throws ReflectiveOperationException {

Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment");

Field unmodifiableMapField = getAccessibleField(processEnvironment, "theUnmodifiableEnvironment");
Object unmodifiableMap = unmodifiableMapField.get(null);
injectIntoUnmodifiableMap(key, value, unmodifiableMap);

Field mapField = getAccessibleField(processEnvironment, "theEnvironment");
Map<String, String> map = (Map<String, String>) mapField.get(null);
map.put(key, value);
}

private static Field getAccessibleField(Class<?> clazz, String fieldName)
throws NoSuchFieldException {

Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
}

private static void injectIntoUnmodifiableMap(String key, String value, Object map)
throws ReflectiveOperationException {

Class unmodifiableMap = Class.forName("java.util.Collections$UnmodifiableMap");
Field field = getAccessibleField(unmodifiableMap, "m");
Object obj = field.get(map);
((Map<String, String>) obj).put(key, value);
}
}

0 comments on commit 910e968

Please sign in to comment.