CloseAsyncOperationActorPlugin implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("CloseAsyncOperationActorPlugin"); item .process(() -> { try { IKey checkUserByEmailActorKey = Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); IOC.register(checkUserByEmailActorKey, new CreateNewInstanceStrategy( (args) -> { try { return new CloseAsyncOperationActor((IObject) args[0]); } catch (ArrayIndexOutOfBoundsException e) { throw new RuntimeException( "Can't get args: args must contain one or more elements " + "and first element must be IObject", e); } catch (InvalidArgumentException e) { throw new RuntimeException( "Can't create actor with this args: " + Arrays.toString(args), e); } } ) ); } catch (ResolutionException e) { throw new ActionExecuteException("CloseAsyncOperationActor plugin can't load: can't get CloseAsyncOperationActor key", e); } catch (InvalidArgumentException e) { throw new ActionExecuteException("CloseAsyncOperationActor plugin can't load: can't create strategy", e); } catch (RegistrationException e) { throw new ActionExecuteException("CloseAsyncOperationActor plugin can't load: can't register new strategy", e); } }); bootstrap.add(item); } catch (InvalidArgumentException e) { throw new PluginException("Can't get BootstrapItem from one of reason", e); } } CloseAsyncOperationActorPlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void MustCorrectLoadPlugin() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey checkUserByEmailActorKey = mock(IKey.class); when(Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName())).thenReturn(checkUserByEmailActorKey); actionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); ArgumentCaptor<CreateNewInstanceStrategy> createNewInstanceStrategyArgumentCaptor = ArgumentCaptor.forClass(CreateNewInstanceStrategy.class); verifyStatic(); IOC.register(eq(checkUserByEmailActorKey), createNewInstanceStrategyArgumentCaptor.capture()); CloseAsyncOperationActor actor = mock(CloseAsyncOperationActor.class); IObject actorParams = mock(IObject.class); whenNew(CloseAsyncOperationActor.class).withArguments(actorParams).thenReturn(actor); assertEquals("Objects must return correct object", createNewInstanceStrategyArgumentCaptor.getValue().resolve(actorParams), actor); verifyNew(CloseAsyncOperationActor.class).withArguments(actorParams); }
@Test public void MustInCorrectLoadNewIBootstrapItemThrowException() throws Exception { whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenThrow(new InvalidArgumentException("")); try { plugin.load(); } catch (PluginException e) { verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); return; } assertTrue("Must throw exception, but was not", false); }
@Test public void MustInCorrectExecuteActionWhenKeysThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); when(Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName())).thenThrow(new ResolutionException("")); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectExecuteActionWhenIOCRegisterThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey createAsyncOpKey = mock(IKey.class); when(Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName())).thenReturn(createAsyncOpKey); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); doThrow(new RegistrationException("")).when(IOC.class); IOC.register(eq(createAsyncOpKey), any()); whenNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.capture()) .thenReturn(mock(CreateNewInstanceStrategy.class)); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); verifyNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); verifyStatic(); IOC.register(eq(createAsyncOpKey), any(CreateNewInstanceStrategy.class)); CloseAsyncOperationActor actor = mock(CloseAsyncOperationActor.class); IObject actorParams = mock(IObject.class); whenNew(CloseAsyncOperationActor.class).withArguments(actorParams).thenReturn(actor); assertEquals("Objects must return correct object", targetFuncArgumentCaptor.getValue().apply(new Object[]{actorParams}), actor); verifyNew(CloseAsyncOperationActor.class).withArguments(actorParams); return; } assertTrue("Must throw exception", false); }
@Test public void MustInCorrectExecuteActionWhenNewCreateInstanceThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey createAsyncOpKey = mock(IKey.class); when(Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName())).thenReturn(createAsyncOpKey); ArgumentCaptor<Function<Object[], Object>> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); whenNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.capture()) .thenThrow(new RegistrationException("")); try { actionArgumentCaptor.getValue().execute(); } catch (ActionExecuteException e) { verifyStatic(); Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); verifyNew(CreateNewInstanceStrategy.class).withArguments(targetFuncArgumentCaptor.getValue()); CloseAsyncOperationActor actor = mock(CloseAsyncOperationActor.class); IObject actorParams = mock(IObject.class); whenNew(CloseAsyncOperationActor.class).withArguments(actorParams).thenReturn(actor); assertTrue("Objects must return correct object", targetFuncArgumentCaptor.getValue().apply(new Object[]{actorParams}) == actor); verifyNew(CloseAsyncOperationActor.class).withArguments(actorParams); return; } assertTrue("Must throw exception", false); }
@Test(expected = RuntimeException.class) public void MustInCorrectResolveWhenKeysGetOrAddActorParamKey() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey checkUserByEmailActorKey = mock(IKey.class); when(Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName())).thenReturn(checkUserByEmailActorKey); ArgumentCaptor<Function> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); CreateNewInstanceStrategy createNewInstanceStrategy = mock(CreateNewInstanceStrategy.class); whenNew(CreateNewInstanceStrategy.class) .withArguments(targetFuncArgumentCaptor.capture()) .thenReturn(createNewInstanceStrategy); actionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); verifyStatic(); IOC.register(checkUserByEmailActorKey, createNewInstanceStrategy); IObject arg = mock(IObject.class); when(Keys.getOrAdd(ActorParams.class.toString())).thenThrow(new ResolutionException("")); targetFuncArgumentCaptor.getValue().apply(new Object[]{arg}); }
@Test(expected = RuntimeException.class) public void MustInCorrectResolveWhenIOCResolveActorParamsThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey checkUserByEmailActorKey = mock(IKey.class); when(Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName())).thenReturn(checkUserByEmailActorKey); ArgumentCaptor<Function> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); CreateNewInstanceStrategy createNewInstanceStrategy = mock(CreateNewInstanceStrategy.class); whenNew(CreateNewInstanceStrategy.class) .withArguments(targetFuncArgumentCaptor.capture()) .thenReturn(createNewInstanceStrategy); actionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); verifyStatic(); IOC.register(checkUserByEmailActorKey, createNewInstanceStrategy); IObject arg = mock(IObject.class); IKey actorParamsIKey = mock(IKey.class); when(Keys.getOrAdd(ActorParams.class.toString())).thenReturn(actorParamsIKey); when(IOC.resolve(actorParamsIKey, arg)).thenThrow(new ResolutionException("")); targetFuncArgumentCaptor.getValue().apply(new Object[]{arg}); }
@Test public void MustInCorrectResolveWhenNewCloseAsyncOperationActorThrowException() throws Exception { BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); when(bootstrapItem.before(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("CloseAsyncOperationActorPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); IKey checkUserByEmailActorKey = mock(IKey.class); when(Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName())).thenReturn(checkUserByEmailActorKey); ArgumentCaptor<Function> targetFuncArgumentCaptor = ArgumentCaptor.forClass((Class) Function.class); CreateNewInstanceStrategy createNewInstanceStrategy = mock(CreateNewInstanceStrategy.class); whenNew(CreateNewInstanceStrategy.class) .withArguments(targetFuncArgumentCaptor.capture()) .thenReturn(createNewInstanceStrategy); actionArgumentCaptor.getValue().execute(); verifyStatic(); Keys.getOrAdd(CloseAsyncOperationActor.class.getCanonicalName()); verifyStatic(); IOC.register(checkUserByEmailActorKey, createNewInstanceStrategy); IObject actorParams = mock(IObject.class); whenNew(CloseAsyncOperationActor.class).withArguments(actorParams).thenThrow(new InvalidArgumentException("")); try { targetFuncArgumentCaptor.getValue().apply(new Object[]{actorParams}); } catch (RuntimeException e) { verifyNew(CloseAsyncOperationActor.class).withArguments(actorParams); return; } assertTrue("Must throw exception", false); }