Skip to content

Commit

Permalink
Change for bean providers functionality: If bean is not found provide…
Browse files Browse the repository at this point in the history
…r throws RuntimeException
  • Loading branch information
Nurkambay committed Jul 10, 2020
1 parent 688cc7d commit 19ba8e5
Show file tree
Hide file tree
Showing 21 changed files with 498 additions and 23 deletions.
22 changes: 22 additions & 0 deletions cluster-boost-ignite-audit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,27 @@
<version>${core.version}</version>
</dependency>

<!-- Testing dependencies -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit4-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public <R, T extends ClusterJob<R>> IgniteCallable<R> getJob(Class<T> bean) {
*/
@Override
public <A, R, T extends ClusterTask<A, R>> IgniteCallable<R> getTask(Class<T> bean, A arg) {
BeanProviderTask<A, R, T> provider = new BeanProviderTask<>(bean, arg);
BeanProviderAuditTask<A, R, T> provider = new BeanProviderAuditTask<>(bean, arg);

String trackingId;
if (arg instanceof Trackable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.ignite.lang.IgniteCallable;

import java.util.Optional;

/**
* Job provider holds a cluster job bean class
* This bean will be injected and executed on every node from a defined node set
Expand All @@ -31,6 +29,10 @@ protected Class<T> getBeanClass() {

@Override
protected R runBean() {
return Optional.ofNullable(getBean(beanClass)).map(ClusterJob::run).orElse(null);
T bean = getBean(beanClass);
if (bean == null) {
throw new RuntimeException("Job execution failed. " + beanClass + " not found");
}
return bean.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ protected Class<T> getBeanClass() {

@Override
protected Void runBean() {
getBean(beanClass).run();
T bean = getBean(beanClass);
if (bean == null) {
throw new RuntimeException("Job execution failed. " + beanClass + " not found");
}
bean.run();
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
@Slf4j
@RequiredArgsConstructor
class BeanProviderTask<A, R, T extends ClusterTask<A, R>> extends BeanProviderAudit<R, T> implements IgniteCallable<R> {
class BeanProviderAuditTask<A, R, T extends ClusterTask<A, R>> extends BeanProviderAudit<R, T> implements IgniteCallable<R> {
private final Class<T> beanClass;
private final A arg;

Expand All @@ -30,6 +30,10 @@ protected Class<T> getBeanClass() {

@Override
protected R runBean() {
return getBean(beanClass).run(arg);
T bean = getBean(beanClass);
if (bean == null) {
throw new RuntimeException("Job execution failed. " + beanClass + " not found");
}
return bean.run(arg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.technologicgroup.boost.audit.providers;

import com.technologicgroup.boost.audit.AuditItem;
import com.technologicgroup.boost.audit.AuditItemAccessor;
import com.technologicgroup.boost.common.ContextHolder;
import com.technologicgroup.boost.mock.*;
import org.apache.ignite.lang.IgniteCallable;
import org.apache.ignite.lang.IgniteRunnable;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;

import java.util.UUID;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class BeanProviderAuditFactoryTest {
private BeanProviderAuditFactory factory;

@SuppressWarnings("unused")
@InjectMocks
private ContextHolder contextHolder;

@Mock
private ApplicationContext context;

@Mock
private AuditItemAccessor auditItemAccessor;

@Before
public void setUp() {
when(context.getBean(AuditItemAccessor.class)).thenReturn(auditItemAccessor);
factory = new BeanProviderAuditFactory();
}

@Test
public void testGetRunnable() {
IgniteRunnable runnable = factory.getRunnable(TestRunnableBean.class);

Assert.assertNotNull(runnable);
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

@Test
public void testGetJob() {
IgniteCallable<Integer> callable = factory.getJob(TestClusterJob.class);

Assert.assertNotNull(callable);
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

@Test
public void testGetTask() {
IgniteCallable<Integer> callable = factory.getTask(TestClusterTask.class, UUID.randomUUID().toString());

Assert.assertNotNull(callable);
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

@Test
public void testGetTask_trackable_OK() {
String trackingId = UUID.randomUUID().toString();
IgniteCallable<Integer> callable = factory.getTask(TestTrackableClusterTask.class, new TestTrackableArg(10, trackingId));

Assert.assertNotNull(callable);
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.technologicgroup.boost.audit.providers;

import com.technologicgroup.boost.audit.AuditItem;
import com.technologicgroup.boost.audit.AuditItemAccessor;
import com.technologicgroup.boost.common.ContextHolder;
import com.technologicgroup.boost.mock.TestClusterJob;
import com.technologicgroup.boost.mock.TestClusterTask;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class BeanProviderAuditJobTest {

@SuppressWarnings("unused")
@InjectMocks
private ContextHolder contextHolder;

@Mock
private ApplicationContext context;

@Mock
private AuditItemAccessor auditItemAccessor;

private BeanProviderAuditJob<Integer, ?> provider;

@Before
public void setUp() {
provider = new BeanProviderAuditJob<>(TestClusterJob.class);
}

@Test
public void testCall_OK() {
when(context.getBean(TestClusterJob.class)).thenReturn(new TestClusterJob(10));
when(context.getBean(AuditItemAccessor.class)).thenReturn(auditItemAccessor);

Integer result = provider.call();

verify(context, times(1)).getBean(TestClusterJob.class);
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
Assert.assertEquals(10, result.longValue());
}

@Test(expected = RuntimeException.class)
public void testCall_bean_notFound() {
when(context.getBean(TestClusterJob.class)).thenReturn(null);
provider.call();
}

@Test
public void testCall_exception_tracked() {
when(context.getBean(TestClusterJob.class)).thenReturn(new TestClusterJob(TestClusterJob.EXCEPTION_CODE));
when(context.getBean(AuditItemAccessor.class)).thenReturn(auditItemAccessor);

try {
provider.call();
Assert.fail();
} catch (Exception e) {
Assert.assertEquals(TestClusterTask.MESSAGE, e.getMessage());
}

verify(context, times(1)).getBean(TestClusterJob.class);
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.technologicgroup.boost.audit.providers;

import com.technologicgroup.boost.audit.AuditItem;
import com.technologicgroup.boost.audit.AuditItemAccessor;
import com.technologicgroup.boost.common.ContextHolder;
import com.technologicgroup.boost.mock.TestFailRunnableBean;
import com.technologicgroup.boost.mock.TestRunnableBean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class BeanProviderAuditRunnableTest {

@SuppressWarnings("unused")
@InjectMocks
private ContextHolder contextHolder;

@Mock
private ApplicationContext context;

@Mock
private AuditItemAccessor auditItemAccessor;

private BeanProviderAuditRunnable<?> provider;

@Before
public void setUp() {
provider = new BeanProviderAuditRunnable<>(TestRunnableBean.class);
}

@Test
public void testCall_run_OK() {
when(context.getBean(TestRunnableBean.class)).thenReturn(new TestRunnableBean());
when(context.getBean(AuditItemAccessor.class)).thenReturn(auditItemAccessor);

provider.run();

verify(context, times(1)).getBean(TestRunnableBean.class);
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

@Test(expected = RuntimeException.class)
public void testRun_bean_notFound() {
when(context.getBean(TestRunnableBean.class)).thenReturn(null);
provider.run();
}

@Test
public void testRun_exception_tracked() {
provider = new BeanProviderAuditRunnable<>(TestFailRunnableBean.class);
when(context.getBean(TestFailRunnableBean.class)).thenReturn(new TestFailRunnableBean());
when(context.getBean(AuditItemAccessor.class)).thenReturn(auditItemAccessor);

try {
provider.run();
Assert.fail();
} catch (Exception e) {
Assert.assertEquals(TestFailRunnableBean.MESSAGE, e.getMessage());
}
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.technologicgroup.boost.audit.providers;

import com.technologicgroup.boost.audit.AuditItem;
import com.technologicgroup.boost.audit.AuditItemAccessor;
import com.technologicgroup.boost.common.ContextHolder;
import com.technologicgroup.boost.mock.TestClusterTask;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class BeanProviderAuditTaskTest {

@SuppressWarnings("unused")
@InjectMocks
private ContextHolder contextHolder;

@Mock
private ApplicationContext context;

@Mock
private AuditItemAccessor auditItemAccessor;

private BeanProviderAuditTask<String, Integer, TestClusterTask> provider;

@Test
public void testRun_OK() {
provider = new BeanProviderAuditTask<>(TestClusterTask.class, "10");
when(context.getBean(TestClusterTask.class)).thenReturn(new TestClusterTask());
Integer result = provider.call();

verify(context, times(1)).getBean(TestClusterTask.class);
Assert.assertEquals(10, result.longValue());
}

@Test(expected = RuntimeException.class)
public void testCall_run_bean_notFound() {
provider = new BeanProviderAuditTask<>(TestClusterTask.class, "10");
when(context.getBean(TestClusterTask.class)).thenReturn(null);
provider.call();
}

@Test(expected = RuntimeException.class)
public void testCall_run_exception() {
provider = new BeanProviderAuditTask<>(TestClusterTask.class, null);
when(context.getBean(TestClusterTask.class)).thenReturn(new TestClusterTask());
provider.call();
}

@Test
public void testCall_run_exception_tracked() {
provider = new BeanProviderAuditTask<>(TestClusterTask.class, null);
when(context.getBean(TestClusterTask.class)).thenReturn(new TestClusterTask());
when(context.getBean(AuditItemAccessor.class)).thenReturn(auditItemAccessor);

try {
provider.call();
Assert.fail();
} catch (Exception e) {
Assert.assertEquals(TestClusterTask.MESSAGE, e.getMessage());
}
verify(auditItemAccessor, times(1)).put(any(String.class), any(AuditItem.class));
}

}
Loading

0 comments on commit 19ba8e5

Please sign in to comment.