Search in sources :

Example 16 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project struts by apache.

the class ConfigurationTest method testInterceptorParamOverride.

public void testInterceptorParamOverride() {
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", "TestInterceptorParamOverride", null, null);
        assertEquals(1, proxy.getConfig().getInterceptors().size());
        MockInterceptor testInterceptor = (MockInterceptor) proxy.getConfig().getInterceptors().get(0).getInterceptor();
        assertEquals("foo123", testInterceptor.getExpectedFoo());
        proxy.execute();
        assertTrue(testInterceptor.isExecuted());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor)

Example 17 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project struts by apache.

the class DefaultActionInvocationTester method testInvoke.

/**
 * Tests interceptor chain invoke.
 *
 * @throws Exception when action throws exception
 */
public void testInvoke() throws Exception {
    List<InterceptorMapping> interceptorMappings = new ArrayList<>();
    MockInterceptor mockInterceptor1 = new MockInterceptor();
    mockInterceptor1.setFoo("test1");
    mockInterceptor1.setExpectedFoo("test1");
    interceptorMappings.add(new InterceptorMapping("test1", mockInterceptor1));
    MockInterceptor mockInterceptor2 = new MockInterceptor();
    interceptorMappings.add(new InterceptorMapping("test2", mockInterceptor2));
    mockInterceptor2.setFoo("test2");
    mockInterceptor2.setExpectedFoo("test2");
    MockInterceptor mockInterceptor3 = new MockInterceptor();
    interceptorMappings.add(new InterceptorMapping("test3", mockInterceptor3));
    mockInterceptor3.setFoo("test3");
    mockInterceptor3.setExpectedFoo("test3");
    DefaultActionInvocation defaultActionInvocation = new DefaultActionInvocationTester(interceptorMappings);
    container.inject(defaultActionInvocation);
    defaultActionInvocation.stack = container.getInstance(ValueStackFactory.class).createValueStack();
    // is possible when result is not executed already
    defaultActionInvocation.setResultCode("");
    defaultActionInvocation.invoke();
    assertTrue(mockInterceptor1.isExecuted());
    assertTrue(mockInterceptor2.isExecuted());
    assertTrue(mockInterceptor3.isExecuted());
    assertTrue(defaultActionInvocation.isExecuted());
    try {
        defaultActionInvocation.setResultCode("");
        fail("should not possible when result already executed");
    } catch (Exception ignored) {
    }
    try {
        defaultActionInvocation.invoke();
        fail("should not possible when result already executed");
    } catch (Exception ignored) {
    }
}
Also used : MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor) ArrayList(java.util.ArrayList) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping)

Example 18 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project struts by apache.

the class XmlConfigurationProviderInterceptorsTest method testInterceptorParamOverriding.

public void testInterceptorParamOverriding() throws Exception {
    Map<String, String> params = new HashMap<>();
    params.put("foo", "expectedFoo");
    params.put("expectedFoo", "expectedFooValue");
    InterceptorStackConfig defaultStack = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).build();
    ArrayList<InterceptorMapping> interceptors = new ArrayList<>();
    interceptors.addAll(defaultStack.getInterceptors());
    ActionConfig intAction = new ActionConfig.Builder("", "TestInterceptorParam", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // TestInterceptorParamOverride action tests that an interceptor with a param override worked
    HashMap<String, String> interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue2");
    interceptorParams.put("foo", "foo123");
    InterceptorStackConfig defaultStack2 = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, interceptorParams))).build();
    interceptors = new ArrayList<>();
    interceptors.addAll(defaultStack2.getInterceptors());
    ActionConfig intOverAction = new ActionConfig.Builder("", "TestInterceptorParamOverride", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    ConfigurationProvider provider = buildConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-params.xml");
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(2, actionConfigs.size());
    assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
    assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));
    ActionConfig ac = (ActionConfig) actionConfigs.get("TestInterceptorParamOverride");
    assertEquals(defaultStack.getInterceptors(), ac.getInterceptors());
    ActionConfig ac2 = (ActionConfig) actionConfigs.get("TestInterceptorParam");
    assertEquals(defaultStack2.getInterceptors(), ac2.getInterceptors());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project aws-cloud-meta by prisma-capacity.

the class ECSMetaDataReaderIntegrationTest method testReadContainerMetaData.

@Test
void testReadContainerMetaData() {
    val interceptor = new MockInterceptor();
    val url = "http://example.com/";
    interceptor.addRule().get(url).respond(CONTAINER_RESPONSE);
    val client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
    val uut = new ECSMetaDataReader(url, client, new ObjectMapper());
    val result = uut.readContainerMetaData();
    assertTrue(result.isPresent());
    assertEquals(result.get().getId(), "43481a6ce4842eec8fe72fc28500c6b52edcc0917f105b83379f88cac1ff3946");
}
Also used : lombok.val(lombok.val) MockInterceptor(okhttp3.mock.MockInterceptor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 20 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project aws-cloud-meta by prisma-capacity.

the class ECSMetaDataReaderIntegrationTest method testReadTaskMetaData.

@Test
void testReadTaskMetaData() {
    val interceptor = new MockInterceptor();
    val url = "http://example.com";
    interceptor.addRule().get(url + "/task").respond(TASK_RESPONSE);
    val client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
    val uut = new ECSMetaDataReader(url, client, new ObjectMapper());
    val result = uut.readTaskMetaData();
    assertTrue(result.isPresent());
    assertEquals(result.get().getCluster(), "default");
}
Also used : lombok.val(lombok.val) MockInterceptor(okhttp3.mock.MockInterceptor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

MockInterceptor (okhttp3.mock.MockInterceptor)13 Before (org.junit.Before)9 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)7 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)5 OkHttpClient (okhttp3.OkHttpClient)5 ActionProxy (com.opensymphony.xwork2.ActionProxy)4 lombok.val (lombok.val)4 Test (org.junit.jupiter.api.Test)4 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)3 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HttpUrl (okhttp3.HttpUrl)3 Builder (okhttp3.HttpUrl.Builder)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)2 Map (java.util.Map)2 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)2 JsonCodecV2 (org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodecV2)2 Mock (com.mockobjects.dynamic.Mock)1