Search in sources :

Example 6 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class CacheInvocationContextFactoryTest method testCreateCacheResultInvocationContext_givenMethodAnnotatedWithCacheResult_shouldCreateCorrectCacheKeyInvocationContext.

@Test
public void testCreateCacheResultInvocationContext_givenMethodAnnotatedWithCacheResult_shouldCreateCorrectCacheKeyInvocationContext() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String param1 = "val_1";
    String param2 = "val_2";
    Integer param3 = 3;
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethod", String.class, String.class, Integer.class)).args(new Object[] { param1, param2, param3 }).obj(testCacheClass).build();
    // when
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    // then
    assertNotNull(context.getKeyParameters());
    assertEquals(2, context.getKeyParameters().size());
    assertEquals(String.class, context.getKeyParameters().get(0).getRawType());
    assertEquals(0, context.getKeyParameters().get(0).getPosition());
    assertEquals(param1, context.getKeyParameters().get(0).getValue());
    assertTrue(isAnnotationPresent(context.getKeyParameters().get(0), CacheKey.class));
    assertEquals(Integer.class, context.getKeyParameters().get(1).getRawType());
    assertEquals(2, context.getKeyParameters().get(1).getPosition());
    assertEquals(param3, context.getKeyParameters().get(1).getValue());
    assertTrue(isAnnotationPresent(context.getKeyParameters().get(1), CacheKey.class));
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) CacheKey(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey) Test(org.junit.Test)

Example 7 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class CacheInvocationContextFactoryTest method testCacheResultMethodWithWrongCacheKeyMethodSignature_givenWrongCacheKeyMethod_shouldThrowException.

@Test(expected = HystrixCachingException.class)
public void testCacheResultMethodWithWrongCacheKeyMethodSignature_givenWrongCacheKeyMethod_shouldThrowException() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String param1 = "val_1";
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethodWithWrongCacheKeyMethodSignature", String.class)).args(new Object[] { param1 }).obj(testCacheClass).build();
    // when
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
// then expected HystrixCachingException
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Example 8 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class HystrixCacheKeyGeneratorTest method testGenerateCacheKey_givenCacheKeyMethodWithNoArguments_shouldReturnEmptyCacheKey.

@Test
public void testGenerateCacheKey_givenCacheKeyMethodWithNoArguments_shouldReturnEmptyCacheKey() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethod")).args(new Object[] {}).obj(testCacheClass).build();
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    HystrixCacheKeyGenerator keyGenerator = HystrixCacheKeyGenerator.getInstance();
    // when
    HystrixGeneratedCacheKey actual = keyGenerator.generateCacheKey(context);
    // then
    assertEquals(DefaultHystrixGeneratedCacheKey.EMPTY, actual);
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Example 9 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class HystrixCacheKeyGeneratorTest method testGenerateCacheKey_givenUser_shouldReturnCorrectCacheKey.

@Test
public void testGenerateCacheKey_givenUser_shouldReturnCorrectCacheKey() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String id = "1";
    User user = new User();
    user.setId(id);
    Profile profile = new Profile("user name");
    user.setProfile(profile);
    String expectedKey = id + user.getProfile().getName();
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethod", String.class, User.class)).args(new Object[] { id, user }).obj(testCacheClass).build();
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    HystrixCacheKeyGenerator keyGenerator = HystrixCacheKeyGenerator.getInstance();
    // when
    String actual = keyGenerator.generateCacheKey(context).getCacheKey();
    // then
    assertEquals(expectedKey, actual);
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Example 10 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class HystrixCacheAspect method methodsAnnotatedWithCacheRemove.

@Around("cacheRemoveAnnotationPointcut()")
public Object methodsAnnotatedWithCacheRemove(final ProceedingJoinPoint joinPoint) throws Throwable {
    Method method = getMethodFromTarget(joinPoint);
    Object obj = joinPoint.getTarget();
    Object[] args = joinPoint.getArgs();
    Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint);
    MetaHolder metaHolder = MetaHolder.builder().args(args).method(method).obj(obj).executionType(ExecutionType.SYNCHRONOUS).ajcMethod(isCompileWeaving() ? getAjcMethodAroundAdvice(obj.getClass(), method) : null).build();
    CacheInvocationContext<CacheRemove> context = CacheInvocationContextFactory.createCacheRemoveInvocationContext(metaHolder);
    HystrixRequestCacheManager.getInstance().clearCache(context);
    return joinPoint.proceed();
}
Also used : CacheRemove(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove) MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Aggregations

MetaHolder (com.netflix.hystrix.contrib.javanica.command.MetaHolder)10 Test (org.junit.Test)7 CacheResult (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult)6 Method (java.lang.reflect.Method)3 CacheRemove (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove)2 AopUtils.getDeclaredMethod (com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod)2 Around (org.aspectj.lang.annotation.Around)2 HystrixInvokable (com.netflix.hystrix.HystrixInvokable)1 HystrixCollapser (com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)1 HystrixCommand (com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)1 CacheKey (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey)1 ExecutionType (com.netflix.hystrix.contrib.javanica.command.ExecutionType)1 MethodExecutionAction (com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction)1 HystrixCachingException (com.netflix.hystrix.contrib.javanica.exception.HystrixCachingException)1 FallbackMethod (com.netflix.hystrix.contrib.javanica.utils.FallbackMethod)1 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1