@Test public void testInvalidEntity() throws Exception { // tests that class rewrite fails if no parameters are found with a Name specified in the annotation entities field testInvalidEntityHelper(DummyAuthEnforce.AbsentEntityName.class); // tests that class rewrite fails if invalid parameters are found with a Name specified in the annotation entities // field testInvalidEntityHelper(DummyAuthEnforce.InvalidEntityName.class); // test that the class rewrite fails if two parameters are found with the same Name annotation testInvalidEntityHelper(DummyAuthEnforce.DuplicateEntityName.class); // test that the class rewrite fails if less entity parts are provided than needed by enforceOn testInvalidEntityHelper(DummyAuthEnforce.LessMultipleParts.class); // test that the class rewrite fails if more entity parts are provided than needed by enforceOn testInvalidEntityHelper(DummyAuthEnforce.MoreMultipleParts.class); // test that the class rewrite fails if multiple entityId are provided and not strings testInvalidEntityHelper(DummyAuthEnforce.MultipleEntityIds.class); // test that the class rewrite fails if invalid enforceOn entity type is provided testInvalidEntityHelper(DummyAuthEnforce.InvalidAuthEnforceEntityType.class); // test that the class rewrite fails if invalid Annotation is used to annotate a parameter testInvalidEntityHelper(DummyAuthEnforce.InvalidParameterAnnotationType.class); // test that class rewrite fails if two parameter have same annotated name testInvalidEntityHelper(DummyAuthEnforce.DuplicateAnnotationName.class); // test that class rewrite fails if parameter have QueryParam and PathParam with same name testInvalidEntityHelper(DummyAuthEnforce.SameQueryAndPathParam.class); // test that class rewrite fails if multiple part is specified of some other type than String testInvalidEntityHelper(DummyAuthEnforce.EntityWithString.class); // test that class rewrite fails if a blank string is provided in entity parts testInvalidEntityHelper(DummyAuthEnforce.BlankEntityName.class); }
private void testRewrite(Method method, Object rewrittenObject, Class<? extends Exception> expectedException, Object... args) throws NoSuchMethodException { testRewrite(method, rewrittenObject, null, expectedException, args); }
@Test public void test() throws Exception { ByteCodeClassLoader classLoader = new ByteCodeClassLoader(getClass().getClassLoader()); classLoader.addClass(rewrite(DummyAuthEnforce.ValidAuthEnforceAnnotations.class)); classLoader.addClass(rewrite(DummyAuthEnforce.AnotherValidAuthEnforceAnnotations.class)); classLoader.addClass(rewrite(DummyAuthEnforce.ClassImplementingInterfaceWithAuthAnnotation.class)); classLoader.addClass(rewrite(DummyAuthEnforce.ClassWithoutAuthEnforce.class)); classLoader.addClass(rewrite(DummyAuthEnforce.ValidAuthEnforceWithFields.class)); Object rewrittenObject = loadRewritten(classLoader, DummyAuthEnforce.class.getName(), cls.getName()); invokeSetters(cls, rewrittenObject); testRewrite(getMethod(cls, "testSingleAction", NamespaceId.class), rewrittenObject, ExceptionAuthorizationEnforcer.ExpectedException.class, NamespaceId.DEFAULT); testRewrite(getMethod(cls, "testMultipleAction", NamespaceId.class), rewrittenObject, ExceptionAuthorizationEnforcer.ExpectedException.class, NamespaceId.DEFAULT); testRewrite(getMethod(cls, "testNoAuthEnforceAnnotation", NamespaceId.class), rewrittenObject, DummyAuthEnforce.EnforceNotCalledException.class, NamespaceId.DEFAULT); testRewrite(getMethod(cls, "testMethodWithoutException", NamespaceId.class), rewrittenObject, ExceptionAuthorizationEnforcer.ExpectedException.class, NamespaceId.DEFAULT); testRewrite(getMethod(cls, "testNameAnnotationPref", NamespaceId.class, String.class), rewrittenObject, NamespaceId.DEFAULT, ExceptionAuthorizationEnforcer.ExpectedException.class, NamespaceId.DEFAULT, "dataset"); testRewrite(getMethod(cls, "testMultipleParts", String.class, String.class), rewrittenObject, new DatasetId("ns", "dataset"),
private void testInvalidEntityHelper(Class cls) throws Exception { try { rewrite(cls); Assert.fail("An IllegalArgumentException should have been thrown earlier."); } catch (IllegalArgumentException e) { // expected } }
private void testRewrite(Method method, Object rewrittenObject, EntityId entityId, Class<? extends Exception> expectedException, Object... args) throws NoSuchMethodException { try { method.invoke(rewrittenObject, args); } catch (Exception e) { // Since the above method is invoked through reflection any exception thrown will be wrapped in // InvocationTargetException so verify that the root cause is the expected exception confirming that enforce // was called successfully. if (!(e instanceof InvocationTargetException && expectedException.isAssignableFrom(e.getCause().getClass()))) { Assert.fail(String.format("Got exception %s while expecting %s%s%s", e.getCause(), ExceptionAuthorizationEnforcer.ExpectedException.class.getName(), System.lineSeparator(), getFormattedStackTrace(e.getStackTrace()))); } if (entityId != null) { if (!ExceptionAuthorizationEnforcer.ExpectedException.class.isAssignableFrom(e.getCause().getClass())) { Assert.fail(String.format("Exception %s is not assignable from %s to match entity %s", e.getCause(), ExceptionAuthorizationEnforcer.ExpectedException.class.getName(), entityId)); } ExceptionAuthorizationEnforcer.ExpectedException exception = (ExceptionAuthorizationEnforcer.ExpectedException) e.getCause(); if (!exception.getEntityId().equals(entityId)) { Assert.fail(String.format("Expected %s with entity %s but found %s", ExceptionAuthorizationEnforcer.ExpectedException.class.getSimpleName(), entityId, exception.getEntityId())); } } } }