|
| 1 | +package uk.gov.communities.prsdb.webapp.journeys.example |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 4 | +import org.junit.jupiter.api.Assertions.assertNull |
| 5 | +import org.junit.jupiter.api.Assertions.assertSame |
| 6 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 7 | +import org.junit.jupiter.api.Test |
| 8 | +import org.mockito.kotlin.mock |
| 9 | +import org.mockito.kotlin.whenever |
| 10 | +import uk.gov.communities.prsdb.webapp.journeys.JourneyStep |
| 11 | + |
| 12 | +class DestinationTests { |
| 13 | + @Test |
| 14 | + fun `Step Destination with just a step returns a redirect for the current step and journey`() { |
| 15 | + // Arrange |
| 16 | + val mockStep = mock<JourneyStep<*, *, *>>() |
| 17 | + val journeyId = "test-journey-id" |
| 18 | + val routeSegment = "test-segment" |
| 19 | + |
| 20 | + whenever(mockStep.currentJourneyId).thenReturn(journeyId) |
| 21 | + whenever(mockStep.routeSegment).thenReturn(routeSegment) |
| 22 | + |
| 23 | + // Act |
| 24 | + val destination = Destination(mockStep) |
| 25 | + val modelAndView = destination.toModelAndView() |
| 26 | + |
| 27 | + // Assert |
| 28 | + assertEquals("redirect:$routeSegment", modelAndView.viewName) |
| 29 | + assertEquals(journeyId, modelAndView.model["journeyId"]) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `Step Destination with explicit journeyId returns a redirect for the specified step and journey`() { |
| 34 | + // Arrange |
| 35 | + val mockStep = mock<JourneyStep<*, *, *>>() |
| 36 | + val journeyId = "explicit-journey-id" |
| 37 | + val routeSegment = "explicit-segment" |
| 38 | + |
| 39 | + whenever(mockStep.routeSegment).thenReturn(routeSegment) |
| 40 | + |
| 41 | + // Act |
| 42 | + val destination = Destination.Step(mockStep, journeyId) |
| 43 | + val modelAndView = destination.toModelAndView() |
| 44 | + |
| 45 | + // Assert |
| 46 | + assertEquals("redirect:$routeSegment", modelAndView.viewName) |
| 47 | + assertEquals(journeyId, modelAndView.model["journeyId"]) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `ExternalUrl Destination returns a redirect to the specified URL with parameters`() { |
| 52 | + // Arrange |
| 53 | + val externalUrl = "http://example.com" |
| 54 | + val params = mapOf("param1" to "value1", "param2" to "value2") |
| 55 | + |
| 56 | + // Act |
| 57 | + val destination = Destination.ExternalUrl(externalUrl, params) |
| 58 | + val modelAndView = destination.toModelAndView() |
| 59 | + |
| 60 | + // Assert |
| 61 | + assertEquals("redirect:$externalUrl", modelAndView.viewName) |
| 62 | + assertEquals("value1", modelAndView.model["param1"]) |
| 63 | + assertEquals("value2", modelAndView.model["param2"]) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `ExternalUrl Destination without parameters returns a redirect to the specified URL`() { |
| 68 | + // Arrange |
| 69 | + val externalUrl = "http://example.com" |
| 70 | + |
| 71 | + // Act |
| 72 | + val destination = Destination.ExternalUrl(externalUrl) |
| 73 | + val modelAndView = destination.toModelAndView() |
| 74 | + |
| 75 | + // Assert |
| 76 | + assertEquals("redirect:$externalUrl", modelAndView.viewName) |
| 77 | + assertTrue(modelAndView.model.isEmpty()) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `Template Destination returns the correct template name and content`() { |
| 82 | + // Arrange |
| 83 | + val templateName = "test-template" |
| 84 | + val content = mapOf("key1" to "value1", "key2" to "value2") |
| 85 | + |
| 86 | + // Act |
| 87 | + val destination = Destination.Template(templateName, content) |
| 88 | + val modelAndView = destination.toModelAndView() |
| 89 | + |
| 90 | + // Assert |
| 91 | + assertEquals(templateName, modelAndView.viewName) |
| 92 | + assertEquals("value1", modelAndView.model["key1"]) |
| 93 | + assertEquals("value2", modelAndView.model["key2"]) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun `Template Destination with empty content returns the correct template name`() { |
| 98 | + // Arrange |
| 99 | + val templateName = "test-template" |
| 100 | + |
| 101 | + // Act |
| 102 | + val destination = Destination.Template(templateName) |
| 103 | + val modelAndView = destination.toModelAndView() |
| 104 | + |
| 105 | + // Assert |
| 106 | + assertEquals(templateName, modelAndView.viewName) |
| 107 | + assertTrue(modelAndView.model.isEmpty()) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `Template Destination withContent creates a new Destination with that content`() { |
| 112 | + // Arrange |
| 113 | + val templateName = "test-template" |
| 114 | + val initialContent = mapOf("key1" to "value1") |
| 115 | + val additionalContent = mapOf("key2" to "value2") |
| 116 | + |
| 117 | + // Act |
| 118 | + val destination = Destination.Template(templateName, initialContent) |
| 119 | + val updatedDestination = destination.withContent(additionalContent) |
| 120 | + val updatedModelAndView = updatedDestination.toModelAndView() |
| 121 | + val oldModelAndView = destination.toModelAndView() |
| 122 | + |
| 123 | + // Assert |
| 124 | + assertEquals(templateName, updatedModelAndView.viewName) |
| 125 | + assertEquals("value1", updatedModelAndView.model["key1"]) |
| 126 | + assertEquals("value2", updatedModelAndView.model["key2"]) |
| 127 | + |
| 128 | + assertEquals(templateName, oldModelAndView.viewName) |
| 129 | + assertEquals("value1", oldModelAndView.model["key1"]) |
| 130 | + assertNull(oldModelAndView.model["key2"]) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun `withContent on non-Template Destination returns the same Destination`() { |
| 135 | + // Arrange |
| 136 | + val mockStep = mock<JourneyStep<*, *, *>>() |
| 137 | + val journeyId = "test-journey-id" |
| 138 | + val routeSegment = "test-segment" |
| 139 | + |
| 140 | + whenever(mockStep.currentJourneyId).thenReturn(journeyId) |
| 141 | + whenever(mockStep.routeSegment).thenReturn(routeSegment) |
| 142 | + |
| 143 | + val stepDestination = Destination(mockStep) |
| 144 | + val externalUrlDestination = Destination.ExternalUrl("http://example.com") |
| 145 | + val contentToAdd = mapOf("key" to "value") |
| 146 | + |
| 147 | + // Act |
| 148 | + val updatedStepDestination = stepDestination.withContent(contentToAdd) |
| 149 | + val updatedExternalUrlDestination = externalUrlDestination.withContent(contentToAdd) |
| 150 | + |
| 151 | + // Assert |
| 152 | + assertSame(stepDestination, updatedStepDestination) |
| 153 | + assertSame(externalUrlDestination, updatedExternalUrlDestination) |
| 154 | + } |
| 155 | +} |
0 commit comments