|
| 1 | +/* |
| 2 | + * Copyright the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.citrusframework.simulator.web.rest; |
| 18 | + |
| 19 | +import org.citrusframework.simulator.model.Message; |
| 20 | +import org.citrusframework.simulator.model.ScenarioAction; |
| 21 | +import org.citrusframework.simulator.model.ScenarioExecution; |
| 22 | +import org.citrusframework.simulator.model.ScenarioParameter; |
| 23 | +import org.citrusframework.simulator.service.ScenarioExecutionQueryService; |
| 24 | +import org.citrusframework.simulator.service.ScenarioExecutionService; |
| 25 | +import org.citrusframework.simulator.service.criteria.ScenarioExecutionCriteria; |
| 26 | +import org.citrusframework.simulator.web.rest.dto.ScenarioExecutionDTO; |
| 27 | +import org.citrusframework.simulator.web.rest.dto.mapper.ScenarioExecutionMapper; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Nested; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 34 | +import org.springframework.data.domain.PageImpl; |
| 35 | +import org.springframework.data.domain.Pageable; |
| 36 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 37 | +import org.springframework.web.context.request.RequestContextHolder; |
| 38 | +import org.springframework.web.context.request.ServletRequestAttributes; |
| 39 | + |
| 40 | +import static java.lang.Boolean.FALSE; |
| 41 | +import static java.lang.Boolean.TRUE; |
| 42 | +import static java.util.Collections.singletonList; |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.mockito.Mockito.doReturn; |
| 45 | +import static org.mockito.Mockito.mock; |
| 46 | +import static org.springframework.http.HttpStatus.OK; |
| 47 | + |
| 48 | +@ExtendWith({MockitoExtension.class}) |
| 49 | +class ScenarioExecutionResourceTest { |
| 50 | + |
| 51 | + @Mock |
| 52 | + private ScenarioExecutionService scenarioExecutionServiceMock; |
| 53 | + |
| 54 | + @Mock |
| 55 | + private ScenarioExecutionQueryService scenarioExecutionQueryServiceMock; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private ScenarioExecutionMapper scenarioExecutionMapperMock; |
| 59 | + |
| 60 | + private ScenarioExecutionResource fixture; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + void beforeEachSetup() { |
| 64 | + fixture = new ScenarioExecutionResource(scenarioExecutionServiceMock, scenarioExecutionQueryServiceMock, scenarioExecutionMapperMock); |
| 65 | + } |
| 66 | + |
| 67 | + @Nested |
| 68 | + class GetAllScenarioExecutions { |
| 69 | + |
| 70 | + @Mock |
| 71 | + private ScenarioExecutionCriteria criteriaMock; |
| 72 | + |
| 73 | + @Mock |
| 74 | + private Pageable pageableMock; |
| 75 | + |
| 76 | + @Mock |
| 77 | + private ScenarioExecutionDTO scenarioExecutionDTOMock; |
| 78 | + |
| 79 | + private ScenarioExecution scenarioExecution; |
| 80 | + |
| 81 | + @BeforeEach |
| 82 | + void beforeEachSetup() { |
| 83 | + var request = new MockHttpServletRequest(); |
| 84 | + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); |
| 85 | + |
| 86 | + scenarioExecution = new ScenarioExecution(); |
| 87 | + scenarioExecution.getScenarioActions().add(mock(ScenarioAction.class)); |
| 88 | + scenarioExecution.getScenarioMessages().add(mock(Message.class)); |
| 89 | + scenarioExecution.getScenarioParameters().add(mock(ScenarioParameter.class)); |
| 90 | + |
| 91 | + var scenarioExecutions = new PageImpl<>(singletonList(scenarioExecution)); |
| 92 | + doReturn(scenarioExecutions).when(scenarioExecutionQueryServiceMock).findByCriteria(criteriaMock, pageableMock); |
| 93 | + |
| 94 | + doReturn(scenarioExecutionDTOMock).when(scenarioExecutionMapperMock).toDto(scenarioExecution); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void stripsIncludedActions() { |
| 99 | + var response = fixture.getAllScenarioExecutions(criteriaMock, FALSE, TRUE, TRUE, pageableMock); |
| 100 | + |
| 101 | + assertThat(response) |
| 102 | + .satisfies( |
| 103 | + r -> assertThat(r.getStatusCode()).isEqualTo(OK), |
| 104 | + r -> assertThat(r.getBody()) |
| 105 | + .singleElement() |
| 106 | + .isEqualTo(scenarioExecutionDTOMock) |
| 107 | + ); |
| 108 | + |
| 109 | + assertThat(scenarioExecution.getScenarioActions()).isEmpty(); |
| 110 | + assertThat(scenarioExecution.getScenarioMessages()).isNotEmpty(); |
| 111 | + assertThat(scenarioExecution.getScenarioParameters()).isNotEmpty(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void stripsIncludedMessages() { |
| 116 | + var response = fixture.getAllScenarioExecutions(criteriaMock, TRUE, FALSE, TRUE, pageableMock); |
| 117 | + |
| 118 | + assertThat(response) |
| 119 | + .satisfies( |
| 120 | + r -> assertThat(r.getStatusCode()).isEqualTo(OK), |
| 121 | + r -> assertThat(r.getBody()) |
| 122 | + .singleElement() |
| 123 | + .isEqualTo(scenarioExecutionDTOMock) |
| 124 | + ); |
| 125 | + |
| 126 | + assertThat(scenarioExecution.getScenarioActions()).isNotEmpty(); |
| 127 | + assertThat(scenarioExecution.getScenarioMessages()).isEmpty(); |
| 128 | + assertThat(scenarioExecution.getScenarioParameters()).isNotEmpty(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void stripsIncludedParameters() { |
| 133 | + var response = fixture.getAllScenarioExecutions(criteriaMock, TRUE, TRUE, FALSE, pageableMock); |
| 134 | + |
| 135 | + assertThat(response) |
| 136 | + .satisfies( |
| 137 | + r -> assertThat(r.getStatusCode()).isEqualTo(OK), |
| 138 | + r -> assertThat(r.getBody()) |
| 139 | + .singleElement() |
| 140 | + .isEqualTo(scenarioExecutionDTOMock) |
| 141 | + ); |
| 142 | + |
| 143 | + assertThat(scenarioExecution.getScenarioActions()).isNotEmpty(); |
| 144 | + assertThat(scenarioExecution.getScenarioMessages()).isNotEmpty(); |
| 145 | + assertThat(scenarioExecution.getScenarioParameters()).isEmpty(); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments