0

我有以下基本资源(端点)测试类。

public abstract class AbstractResourceTest extends JerseyTest {

    private static final String PORT = "9991";

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    protected DeploymentContext configureDeployment() {
        forceSet(TestProperties.CONTAINER_PORT, PORT);
        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);

        final ResourceConfig config = new ResourceConfig().packages(
                "com.intouch.api.rest",
                "com.intouch.api.security")
                .register(createMoxyJsonResolver())
                .register(SecurityEntityFilteringFeature.class)
                .property("jersey.config.server.tracing.type", "ALL")
                .property("jersey.config.server.tracing.threshold", "TRACE")
                .property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);

        return ServletDeploymentContext
                .forServlet(new ServletContainer(config))
                .addListener(ContextLoaderListener.class)
                .contextParam("contextConfigLocation", "classpath:applicationContext-api-test.xml")
                .build();
    }

    @Override
    protected void configureClient(ClientConfig config) {
        super.configureClient(config);
        config.register(GsonJsonObjectProvider.class)
                .register(GsonJsonArrayProvider.class);
    }
}

它非常适合需要角色的测试(用@RolesAllowed 注释)。但是我有一些不需要角色的端点。这个测试失败是因为他们检查了前一个测试的角色(例如前一个测试正在调用一个需要管理角色的端点,下一个测试正在调用一个不需要任何角色的端点,对于这个(第二个测试)是调用 securityContext.isUserInRole("MANAGE"))。它是错误还是应该是这样的?我们如何解决它?

这是资源(端点)的示例:

@Path("/users")
@Component
public class UserResource {

    @Autowired
    private UserServiceFacade userServiceFacade;

    @POST
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Consumes(MediaType.APPLICATION_JSON)
    public UserAccountBean createUser(UserAccountBean userAccountBean) {
           return userServiceFacade.createUser(userAccountBean);
    }

    @PUT
    @RolesAllowed({Permission.Constants.MANAGE_USERS_VALUE})
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Consumes(MediaType.APPLICATION_JSON)
    public UserAccountBean updateUser(UserAccountBean userAccountBean) {
           return userServiceFacade.updateUser(userAccountBean);
    }
}
4

0 回答 0