List<VdcOperation<?, ?>> waitForResultList = getWaitForResultList(actionEntry.getValue()); if (!waitForResultList.isEmpty()) { runMultipleActions(actionEntry.getKey(), waitForResultList, parameters, allActionOperations, true, runOnlyIfAllValidationPass); List<VdcOperation<?, ?>> immediateReturnList = actionEntry.getValue(); runMultipleActions(actionEntry.getKey(), immediateReturnList, parameters, allActionOperations, false, runOnlyIfAllValidationPass); transmitOperation(actionEntry.getValue().get(0));
private void handleRunMultipleActionFailure(final List<VdcOperation<?, ?>> operations, final Throwable exception) { //Clear out the token, and let the retry mechanism try again. xsrfRequestBuilder.setXsrfToken(null); Map<VdcOperationCallback<?, ?>, List<VdcOperation<?, ?>>> callbackMap = getCallbackMap(operations); for (Map.Entry<VdcOperationCallback<?, ?>, List<VdcOperation<?, ?>>> callbackEntry: callbackMap.entrySet()) { if (callbackEntry.getKey() instanceof VdcOperationCallbackList) { ((VdcOperationCallbackList) callbackEntry.getKey()).onFailure(callbackEntry.getValue(), exception); } else { ((VdcOperationCallback) callbackEntry.getKey()).onFailure(callbackEntry.getValue().get(0), exception); } } }
/** * Run a query that does not require the user to be logged in. * @param operation The operation to run. */ private void runPublicQuery(final VdcOperation<?, ?> operation) { getService(new ServiceCallback() { @Override public void serviceFound(GenericApiGWTServiceAsync service) { service.runPublicQuery((QueryType) operation.getOperation(), (QueryParametersBase) operation.getParameter(), new AsyncCallback<QueryReturnValue>() { @Override public void onFailure(final Throwable exception) { operation.getCallback().onFailure(operation, exception); } @Override public void onSuccess(final QueryReturnValue result) { operation.getCallback().onSuccess(operation, result); } }); } @Override public void onFailure(Throwable exception) { operation.getCallback().onFailure(operation, exception); } }); }
/** * Transmit a single operation, with the expectation of a single result object. * @param operation The operation to execute. */ void transmitOperation(final VdcOperation<?, ?> operation) { // Figure out if this is an action or a query. if (operation.isAction()) { // Action runAction(operation); } else { // Query if (operation.isPublic()) { runPublicQuery(operation); } else { runQuery(operation); } } }
if (operation.isPublic()) { queriesList.remove(operation); runPublicQuery(operation); } else { queryTypes.add((QueryType) operation.getOperation()); getService(new ServiceCallback() { @Override public void serviceFound(GenericApiGWTServiceAsync service) { transmitOperation(queriesList.get(0));
@SuppressWarnings("unchecked") @Test public void testMissingXsrfToken() { //Remove token so there should be a request for it. mockXsrfRpcRequestBuilder.setXsrfToken(null); QueryParametersBase testParameters = new QueryParametersBase(); final List<VdcOperation<QueryType, QueryParametersBase>> operationList = new ArrayList<>(); final VdcOperation<QueryType, QueryParametersBase> testOperation = new VdcOperation<>(QueryType.Search, testParameters, null); operationList.add(testOperation); testProvider.transmitOperation(testOperation); verify(mockXsrfService).getNewXsrfToken((AsyncCallback<XsrfToken>) any()); }
@BeforeEach public void setUp() { mockXsrfRpcRequestBuilder = new XsrfRpcRequestBuilder(); testProvider = new GWTRPCCommunicationProvider(mockService, mockXsrfService, mockXsrfRpcRequestBuilder); mockXsrfRpcRequestBuilder.setXsrfToken(new XsrfToken("Something")); //$NON-NLS-1$ }
@Test public void testGetOperationResult_Empty() { List<VdcOperation<?, ?>> testOperationList = new ArrayList<>(); List<VdcOperation<?, ?>> allOperationList = new ArrayList<>(); List<?> allResults = new ArrayList<>(); List<?> result = testProvider.getOperationResult(testOperationList, allOperationList, allResults); assertEquals(0, result.size(), "Result should have no results"); //$NON-NLS-1$ }
@Test public void testTransmitOperationAction_failure() { ActionParametersBase testParameters = new ActionParametersBase(); final List<VdcOperation<ActionType, ActionParametersBase>> operationList = new ArrayList<>(); final Exception testException = new Exception("This is an exception"); //$NON-NLS-1$ final VdcOperation<ActionType, ActionParametersBase> testOperation = new VdcOperation<>(ActionType.AddDisk, testParameters, new VdcOperationCallback<VdcOperation<ActionType, ActionParametersBase>, ActionReturnValue>() { @Override public void onSuccess(VdcOperation<ActionType, ActionParametersBase> operation, ActionReturnValue result) { fail("Should not get here"); //$NON-NLS-1$ } @Override public void onFailure(VdcOperation<ActionType, ActionParametersBase> operation, Throwable exception) { assertEquals(operationList.get(0), operation, "Operations should match"); //$NON-NLS-1$ assertEquals(testException, exception, "Exceptions should match"); //$NON-NLS-1$ } }); operationList.add(testOperation); testProvider.transmitOperation(testOperation); verify(mockService).runAction(eq(ActionType.AddDisk), eq(testParameters), actionCallback.capture()); actionCallback.getValue().onFailure(testException); }
@BeforeEach public void setUp() { mockService = mock(GenericApiGWTServiceAsync.class, withSettings().extraInterfaces(ServiceDefTarget.class)); fakeScheduler = new FakeGWTScheduler(); CommunicationProvider communicationsProvider = new GWTRPCCommunicationProvider(mockService, mockXsrfService, mockXsrfRpcRequestBuilder); when(mockXsrfRpcRequestBuilder.getXsrfToken()).thenReturn(new XsrfToken("Something")); //$NON-NLS-1$ OperationProcessor operationProcessor = new OperationProcessor(communicationsProvider); operationProcessor.setScheduler(fakeScheduler); VdcOperationManager operationsManager = new VdcOperationManager(mockEventBus, operationProcessor); frontend = new Frontend(operationsManager, mockValidateErrorsTranslator, mockVdsmErrorsTranslator, mockEventBus); frontend.frontendFailureEvent = mockFrontendFailureEvent; frontend.frontendNotLoggedInEvent = mockFrontendNotLoggedInEvent; frontend.setEventsHandler(mockEventsHandler); frontend.setConstants(mockConstants); when(mockAsyncQuery.getModel()).thenReturn(ASYNC_OPERATION_TARGET); when(mockAsyncQuery.getAsyncCallback()).thenReturn(mockAsyncCallback); }
@Test public void testGetOperationResult_One() { VdcOperation<ActionType, ActionParametersBase> testOperation1 = new VdcOperation<>(ActionType.ActivateVds, new ActionParametersBase(), null); List<VdcOperation<?, ?>> testOperationList = new ArrayList<>(); testOperationList.add(testOperation1); List<VdcOperation<?, ?>> allOperationList = new ArrayList<>(); allOperationList.add(testOperation1); ActionReturnValue testResult1 = new ActionReturnValue(); List<ActionReturnValue> allResults = new ArrayList<>(); allResults.add(testResult1); List<?> result = testProvider.getOperationResult(testOperationList, allOperationList, allResults); assertEquals(1, result.size(), "Result should have one results"); //$NON-NLS-1$ }
@Test public void testTransmitOperationQuery_failure() { QueryParametersBase testParameters = new QueryParametersBase(); final Exception testException = new Exception("This is an exception"); //$NON-NLS-1$ final List<VdcOperation<QueryType, QueryParametersBase>> operationList = new ArrayList<>(); final VdcOperation<QueryType, QueryParametersBase> testOperation = new VdcOperation<>(QueryType.Search, testParameters, new VdcOperationCallback<VdcOperation<QueryType, QueryParametersBase>, QueryReturnValue>() { @Override public void onSuccess(VdcOperation<QueryType, QueryParametersBase> operation, QueryReturnValue result) { fail("Should not get here"); //$NON-NLS-1$ } @Override public void onFailure(VdcOperation<QueryType, QueryParametersBase> operation, Throwable exception) { assertEquals(operationList.get(0), operation, "Operations should match"); //$NON-NLS-1$ assertEquals(testException, exception, "Exceptions should match"); //$NON-NLS-1$ } }); operationList.add(testOperation); testProvider.transmitOperation(testOperation); verify(mockService).runQuery(eq(QueryType.Search), eq(testParameters), queryCallback.capture()); queryCallback.getValue().onFailure(testException); }
/** * Run a query that requires the user to be logged in. * @param operation The operation to run. */ private void runQuery(final VdcOperation<?, ?> operation) { getService(new ServiceCallback() { @Override public void serviceFound(GenericApiGWTServiceAsync service) { service.runQuery((QueryType) operation.getOperation(), (QueryParametersBase) operation.getParameter(), new AsyncCallback<QueryReturnValue>() { @Override public void onFailure(final Throwable exception) { //Clear out the token, and let the retry mechanism try again. xsrfRequestBuilder.setXsrfToken(null); operation.getCallback().onFailure(operation, exception); } @Override public void onSuccess(final QueryReturnValue result) { operation.getCallback().onSuccess(operation, result); } }); } @Override public void onFailure(Throwable exception) { operation.getCallback().onFailure(operation, exception); } }); }
@BeforeEach public void setUp() { mockService = mock(GenericApiGWTServiceAsync.class, withSettings().extraInterfaces(ServiceDefTarget.class)); fakeScheduler = new FakeGWTScheduler(); CommunicationProvider communicationsProvider = new GWTRPCCommunicationProvider(mockService, mockXsrfService, mockXsrfRpcRequestBuilder); when(mockXsrfRpcRequestBuilder.getXsrfToken()).thenReturn(new XsrfToken("Something")); //$NON-NLS-1$ OperationProcessor operationProcessor = new OperationProcessor(communicationsProvider); operationProcessor.setScheduler(fakeScheduler); VdcOperationManager operationsManager = new VdcOperationManager(mockEventBus, operationProcessor); frontend = new Frontend(operationsManager, mockValidateErrorsTranslator, mockVdsmErrorsTranslator, mockEventBus); frontend.setEventsHandler(mockEventsHandler); frontend.setConstants(mockConstants); frontend.frontendFailureEvent = mockFrontendFailureEvent; when(mockConstants.noValidateMessage()).thenReturn(NO_MESSAGE); }
@Test public void testGetOperationResult_One_of_Two() { VdcOperation<ActionType, ActionParametersBase> testOperation1 = new VdcOperation<>(ActionType.ActivateVds, new ActionParametersBase(), null); VdcOperation<ActionType, ActionParametersBase> testOperation2 = new VdcOperation<>(ActionType.AddBookmark, new ActionParametersBase(), null); List<VdcOperation<?, ?>> testOperationList = new ArrayList<>(); testOperationList.add(testOperation2); List<VdcOperation<?, ?>> allOperationList = new ArrayList<>(); allOperationList.add(testOperation1); allOperationList.add(testOperation2); ActionReturnValue testResult1 = new ActionReturnValue(); ActionReturnValue testResult2 = new ActionReturnValue(); List<ActionReturnValue> allResults = new ArrayList<>(); allResults.add(testResult1); allResults.add(testResult2); List<?> result = testProvider.getOperationResult(testOperationList, allOperationList, allResults); assertEquals(1, result.size(), "Result should have one results"); //$NON-NLS-1$ assertEquals(testResult2, result.get(0), "Result should match"); //$NON-NLS-1$ }
/** * Multiple queries failure handler. * @param queriesList The queries list. * @param exception The exception causing the failure. */ private void handleMultipleQueriesFailure(final List<VdcOperation<?, ?>> queriesList, final Throwable exception) { //Clear out the token, and let the retry mechanism try again. xsrfRequestBuilder.setXsrfToken(null); Map<VdcOperationCallback<?, ?>, List<VdcOperation<?, ?>>> callbackMap = getCallbackMap(queriesList); for (Map.Entry<VdcOperationCallback<?, ?>, List<VdcOperation<?, ?>>> callbackEntry: callbackMap.entrySet()) { if (callbackEntry.getKey() instanceof VdcOperationCallbackList) { ((VdcOperationCallbackList) callbackEntry.getKey()).onFailure(callbackEntry.getValue(), exception); } else { ((VdcOperationCallback) callbackEntry.getKey()).onFailure(callbackEntry.getValue().get(0), exception); } } }
@Test public void testTransmitOperationAction_success() { ActionParametersBase testParameters = new ActionParametersBase(); final ActionReturnValue testResult = new ActionReturnValue(); final List<VdcOperation<ActionType, ActionParametersBase>> operationList = new ArrayList<>(); final VdcOperation<ActionType, ActionParametersBase> testOperation = new VdcOperation<>(ActionType.AddDisk, testParameters, new VdcOperationCallback<VdcOperation<ActionType, ActionParametersBase>, ActionReturnValue>() { @Override public void onSuccess(VdcOperation<ActionType, ActionParametersBase> operation, ActionReturnValue result) { assertEquals(testResult, result, "Test results should match"); //$NON-NLS-1$ assertEquals(operationList.get(0), operation, "Operations should match"); //$NON-NLS-1$ } @Override public void onFailure(VdcOperation<ActionType, ActionParametersBase> operation, Throwable caught) { fail("Should not get here"); //$NON-NLS-1$ } }); operationList.add(testOperation); testProvider.transmitOperation(testOperation); verify(mockService).runAction(eq(ActionType.AddDisk), eq(testParameters), actionCallback.capture()); actionCallback.getValue().onSuccess(testResult); }
/** * Run an action on the {@code GenericApiGWTServiceAsync} service. * @param operation The operation to run. */ private void runAction(final VdcOperation<?, ?> operation) { getService(new ServiceCallback() { @Override public void serviceFound(GenericApiGWTServiceAsync service) { service.runAction((ActionType) operation.getOperation(), (ActionParametersBase) operation.getParameter(), new AsyncCallback<ActionReturnValue>() { @Override public void onFailure(final Throwable exception) { //Clear out the token, and let the retry mechanism try again. xsrfRequestBuilder.setXsrfToken(null); operation.getCallback().onFailure(operation, exception); } @Override public void onSuccess(final ActionReturnValue result) { operation.getCallback().onSuccess(operation, result); } }); } @Override public void onFailure(Throwable exception) { operation.getCallback().onFailure(operation, exception); } }); }
@Test public void testTransmitOperationQuery_success() { QueryParametersBase testParameters = new QueryParametersBase(); final QueryReturnValue testResult = new QueryReturnValue(); final List<VdcOperation<QueryType, QueryParametersBase>> operationList = new ArrayList<>(); final VdcOperation<QueryType, QueryParametersBase> testOperation = new VdcOperation<>(QueryType.Search, testParameters, new VdcOperationCallback<VdcOperation<QueryType, QueryParametersBase>, QueryReturnValue>() { @Override public void onSuccess(VdcOperation<QueryType, QueryParametersBase> operation, QueryReturnValue result) { assertEquals(testResult, result, "Test results should match"); //$NON-NLS-1$ assertEquals(operationList.get(0), operation, "Operations should match"); //$NON-NLS-1$ } @Override public void onFailure(VdcOperation<QueryType, QueryParametersBase> operation, Throwable caught) { fail("Should not get here"); //$NON-NLS-1$ } }); operationList.add(testOperation); testProvider.transmitOperation(testOperation); verify(mockService).runQuery(eq(QueryType.Search), eq(testParameters), queryCallback.capture()); queryCallback.getValue().onSuccess(testResult); }
private void runMultipleActions(final ActionType actionType, final List<VdcOperation<?, ?>> operations, final List<ActionParametersBase> parameters, final List<VdcOperation<?, ?>> allActionOperations, final boolean waitForResults, final boolean runOnlyIfAllValidationPass) { getService(new ServiceCallback() { @Override public void serviceFound(GenericApiGWTServiceAsync service) {