CreateAsyncOperationActor { public void create(final CreateAsyncOperationMessage message) throws CreateAsyncOperationActorException { try { String token = IOC.resolve(Keys.getOrAdd("db.collection.nextid")); Integer amountOfHoursToExpireFromNow = message.getExpiredTime(); String expiredTime = LocalDateTime.now().plusHours(amountOfHoursToExpireFromNow).format(formatter); message.setSessionIdInData(message.getSessionId()); IObject authOperationData = message.getOperationData(); collection.createAsyncOperation(authOperationData, token, expiredTime); message.setAsyncOperationToken(token); List<String> availableTokens = message.getOperationTokens(); if (availableTokens == null) { message.setOperationTokens(Arrays.asList(token)); } else { availableTokens.add(token); message.setOperationTokens(availableTokens); } } catch (ResolutionException | ReadValueException | ChangeValueException | CreateAsyncOperationException e) { throw new CreateAsyncOperationActorException("Can't create async operation.", e); } } CreateAsyncOperationActor(final IObject params); void create(final CreateAsyncOperationMessage message);  }
@Test public void ShouldCreateOperationAndSetToken() throws ReadValueException, CreateAsyncOperationActorException, ChangeValueException, ResolutionException, CreateAsyncOperationException { String sessionId = "sessionId"; when(IOC.resolve(Keys.getOrAdd("db.collection.nextid"))).thenReturn(""); when(message.getSessionId()).thenReturn(sessionId); when(message.getExpiredTime()).thenReturn(4); IObject asyncDataObj = mock(IObject.class); IKey dataKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(dataKey); when(message.getOperationData()).thenReturn(asyncDataObj); actor.create(message); verify(collection).createAsyncOperation(eq(asyncDataObj), anyString(), anyString()); verify(message).setAsyncOperationToken(anyString()); }
@Test(expected = CreateAsyncOperationActorException.class) public void ShouldThrowException_When_InternalErrorIsOccured() throws ReadValueException, CreateAsyncOperationActorException, ChangeValueException, ResolutionException, CreateAsyncOperationException { String sessionId = "sessionId"; when(IOC.resolve(Keys.getOrAdd("db.collection.nextid"))).thenReturn(""); when(message.getSessionId()).thenReturn(sessionId); when(message.getExpiredTime()).thenReturn(4); IObject asyncDataObj = mock(IObject.class); IKey dataKey = mock(IKey.class); when(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")).thenReturn(dataKey); when(message.getOperationData()).thenReturn(asyncDataObj); doThrow(new CreateAsyncOperationException("exception")).when(collection).createAsyncOperation(any(), any(), any()); actor.create(message); fail(); }