|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.uber.cadence.samples.bookingsaga; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | +import static org.mockito.Matchers.eq; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import com.uber.cadence.client.WorkflowClient; |
| 28 | +import com.uber.cadence.client.WorkflowException; |
| 29 | +import com.uber.cadence.testing.TestWorkflowEnvironment; |
| 30 | +import com.uber.cadence.worker.Worker; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +public class TripBookingWorkflowTest { |
| 36 | + |
| 37 | + private TestWorkflowEnvironment testEnv; |
| 38 | + private Worker worker; |
| 39 | + private WorkflowClient workflowClient; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + testEnv = TestWorkflowEnvironment.newInstance(); |
| 44 | + worker = testEnv.newWorker(TripBookingSaga.TASK_LIST); |
| 45 | + worker.registerWorkflowImplementationTypes(TripBookingWorkflowImpl.class); |
| 46 | + |
| 47 | + workflowClient = testEnv.newWorkflowClient(); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void tearDown() { |
| 52 | + testEnv.close(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Not very useful test that validates that the default activities cause workflow to fail. See |
| 57 | + * other tests on using mocked activities to test SAGA logic. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void testTripBookingFails() { |
| 61 | + worker.registerActivitiesImplementations(new TripBookingActivitiesImpl()); |
| 62 | + testEnv.start(); |
| 63 | + |
| 64 | + TripBookingWorkflow workflow = workflowClient.newWorkflowStub(TripBookingWorkflow.class); |
| 65 | + try { |
| 66 | + workflow.bookTrip("trip1"); |
| 67 | + fail("unreachable"); |
| 68 | + } catch (WorkflowException e) { |
| 69 | + assertEquals("Flight booking did not work", e.getCause().getCause().getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** Unit test workflow logic using mocked activities. */ |
| 74 | + @Test |
| 75 | + public void testSAGA() { |
| 76 | + TripBookingActivities activities = mock(TripBookingActivities.class); |
| 77 | + when(activities.bookHotel("trip1")).thenReturn("HotelBookingID1"); |
| 78 | + when(activities.reserveCar("trip1")).thenReturn("CarBookingID1"); |
| 79 | + when(activities.bookFlight("trip1")) |
| 80 | + .thenThrow(new RuntimeException("Flight booking did not work")); |
| 81 | + worker.registerActivitiesImplementations(activities); |
| 82 | + |
| 83 | + testEnv.start(); |
| 84 | + |
| 85 | + TripBookingWorkflow workflow = workflowClient.newWorkflowStub(TripBookingWorkflow.class); |
| 86 | + try { |
| 87 | + workflow.bookTrip("trip1"); |
| 88 | + fail("unreachable"); |
| 89 | + } catch (WorkflowException e) { |
| 90 | + assertEquals("Flight booking did not work", e.getCause().getCause().getMessage()); |
| 91 | + } |
| 92 | + |
| 93 | + verify(activities).cancelHotel(eq("HotelBookingID1"), eq("trip1")); |
| 94 | + verify(activities).cancelCar(eq("CarBookingID1"), eq("trip1")); |
| 95 | + } |
| 96 | +} |
0 commit comments