对于不需要角色的端点,Jersey测试失败(未使用@RolesAllowed注释)

时间:2016-04-28 13:54:01

标签: java jersey jersey-test-framework

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

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注释)。但我有一些端点,不需要角色。这个测试是失败的,因为他们正在检查先前测试的作用(例如,先前的测试是调用需要MANAGE角色的端点,下一个测试是调用不需要任何角色的端点,为此(第二次测试)是调用securityContext.isUserInRole(“MANAGE”))。 是bug还是它应该是这样的? 我们如何解决它?

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

@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);
    }
}

0 个答案:

没有答案