-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#242): Support proxied and subclassed scenarios
- Loading branch information
Thorsten Schlathoelter
committed
Feb 15, 2024
1 parent
cadb91b
commit 5aaad5d
Showing
5 changed files
with
279 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...lator-spring-boot/src/main/java/org/citrusframework/simulator/scenario/ScenarioUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* 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.citrusframework.simulator.scenario; | ||
|
||
import static lombok.AccessLevel.PRIVATE; | ||
import static org.springframework.aop.framework.AopProxyUtils.ultimateTargetClass; | ||
|
||
import jakarta.annotation.Nonnull; | ||
import jakarta.annotation.Nullable; | ||
import java.lang.annotation.Annotation; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = PRIVATE) | ||
public class ScenarioUtils { | ||
|
||
/** | ||
* Retrieves the specified annotation from the class hierarchy of the given scenario object. | ||
* If the scenario object is a proxy, this method unwraps the proxy to obtain the actual target class. | ||
* | ||
* @param scenario The scenario object to search for the annotation. Must not be null. | ||
* @param annotationType The type of annotation to retrieve. Must not be null. | ||
* @param <T> The type of the annotation. | ||
* @return The annotation if found, otherwise {@code null}. | ||
*/ | ||
@Nullable | ||
public static <T extends Annotation> T getAnnotationFromClassHierarchy( | ||
@Nonnull SimulatorScenario scenario, Class<T> annotationType) { | ||
return getAnnotationFromClassHierarchy(ultimateTargetClass(scenario), annotationType); | ||
} | ||
|
||
/** | ||
* Retrieves the specified annotation from the class hierarchy of the given scenario class. | ||
* | ||
* @param scenarioClass The class to search for the annotation. | ||
* @param annotationType The type of annotation to retrieve. | ||
* @param <T> The type of the annotation. | ||
* @return The annotation if found, otherwise {@code null}. | ||
*/ | ||
@Nullable | ||
public static <T extends Annotation> T getAnnotationFromClassHierarchy( | ||
@Nonnull Class<?> scenarioClass, Class<T> annotationType) { | ||
|
||
T annotation = scenarioClass.getAnnotation(annotationType); | ||
if (annotation != null) { | ||
return annotation; | ||
} else if (scenarioClass.getSuperclass() != null) { | ||
return getAnnotationFromClassHierarchy(scenarioClass.getSuperclass(), annotationType); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...c/test/java/org/citrusframework/simulator/http/HttpRequestAnnotationScenarioMapperIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* 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.citrusframework.simulator.http; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.springframework.http.HttpMethod.PUT; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.citrusframework.http.message.HttpMessage; | ||
import org.citrusframework.simulator.config.SimulatorConfigurationProperties; | ||
import org.citrusframework.simulator.http.HttpRequestAnnotationScenarioMapperIT.AspectTestConfiguration; | ||
import org.citrusframework.simulator.scenario.AbstractSimulatorScenario; | ||
import org.citrusframework.simulator.scenario.Scenario; | ||
import org.citrusframework.simulator.scenario.SimulatorScenario; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.aop.support.AopUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.EnableAspectJAutoProxy; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
/** | ||
* @author Thorsten Schlathoelter | ||
*/ | ||
@ExtendWith(SpringExtension.class) | ||
@Import(AspectTestConfiguration.class) | ||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | ||
class HttpRequestAnnotationScenarioMapperIT { | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Autowired | ||
private HttpRequestAnnotationScenarioMapper fixture; | ||
|
||
@Test | ||
void testGetMappingKeyFromProxiedScenario() { | ||
Collection<SimulatorScenario> scenarios = applicationContext.getBeansOfType( | ||
SimulatorScenario.class).values(); | ||
|
||
assertThat(scenarios).hasSize(1); | ||
|
||
assertThat(AopUtils.isAopProxy(scenarios.iterator().next())).isTrue(); | ||
fixture.setScenarioList(new ArrayList<>(scenarios)); | ||
assertEquals("PutFooScenario", fixture.getMappingKey(new HttpMessage().path("/issues/foo").method(PUT))); | ||
|
||
} | ||
|
||
@Scenario("PutFooScenario") | ||
@RequestMapping(value = "/issues/foo", method = RequestMethod.PUT) | ||
public static class PutFooScenario extends AbstractSimulatorScenario { | ||
} | ||
|
||
@TestConfiguration | ||
@EnableAspectJAutoProxy | ||
public static class AspectTestConfiguration { | ||
|
||
@Bean | ||
public SimulatorScenario putFooScenario() { | ||
return new PutFooScenario(); | ||
} | ||
|
||
@Bean | ||
public ScenarioWrappingAspect myAspect() { | ||
return new ScenarioWrappingAspect(); | ||
} | ||
|
||
@Bean | ||
public HttpRequestAnnotationScenarioMapper httpRequestAnnotationScenarioMapper() { | ||
return new HttpRequestAnnotationScenarioMapper(); | ||
} | ||
|
||
@Bean | ||
public SimulatorConfigurationProperties simulatorConfigurationProperties() { | ||
return new SimulatorConfigurationProperties(); | ||
} | ||
} | ||
|
||
@Aspect | ||
public static class ScenarioWrappingAspect { | ||
|
||
private static final String RUN_SCENARIO_POINTCUT = | ||
"within(org.citrusframework.simulator.scenario.SimulatorScenario+) && execution(* run(org.citrusframework.simulator.scenario.ScenarioRunner))"; | ||
|
||
@Around(RUN_SCENARIO_POINTCUT) | ||
public Object interceptScenarioExecution(ProceedingJoinPoint joinPoint) throws Throwable { | ||
return joinPoint.proceed(); | ||
} | ||
} | ||
} |
Oops, something went wrong.