|
| 1 | +package org.broadinstitute.consent.http.resources; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.mockito.ArgumentMatchers.anyString; |
| 6 | +import static org.mockito.Mockito.doNothing; |
| 7 | +import static org.mockito.Mockito.doThrow; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import jakarta.ws.rs.NotFoundException; |
| 13 | +import jakarta.ws.rs.core.Response; |
| 14 | +import jakarta.ws.rs.core.UriBuilder; |
| 15 | +import jakarta.ws.rs.core.UriInfo; |
| 16 | +import java.net.URI; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import org.broadinstitute.consent.http.AbstractTestHelper; |
| 20 | +import org.broadinstitute.consent.http.models.AuthUser; |
| 21 | +import org.broadinstitute.consent.http.models.FeatureFlag; |
| 22 | +import org.broadinstitute.consent.http.service.FeatureFlagService; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 28 | + |
| 29 | +@ExtendWith(MockitoExtension.class) |
| 30 | +class FeatureFlagResourceTest extends AbstractTestHelper { |
| 31 | + |
| 32 | + @Mock private FeatureFlagService featureFlagService; |
| 33 | + |
| 34 | + private FeatureFlagResource resource; |
| 35 | + private final AuthUser authUser = new AuthUser( "[email protected]"); |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + void setUp() { |
| 39 | + resource = new FeatureFlagResource(featureFlagService); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testGetAllFeatureFlags() { |
| 44 | + FeatureFlag flag1 = new FeatureFlag("feature1", "value1"); |
| 45 | + FeatureFlag flag2 = new FeatureFlag("feature2", "value2"); |
| 46 | + when(featureFlagService.getAllFeatureFlags()).thenReturn(List.of(flag1, flag2)); |
| 47 | + |
| 48 | + Response response = resource.getAllFeatureFlags(); |
| 49 | + assertEquals(200, response.getStatus()); |
| 50 | + assertNotNull(response.getEntity()); |
| 51 | + verify(featureFlagService).getAllFeatureFlags(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testGetAllFeatureFlags_Empty() { |
| 56 | + when(featureFlagService.getAllFeatureFlags()).thenReturn(List.of()); |
| 57 | + |
| 58 | + Response response = resource.getAllFeatureFlags(); |
| 59 | + assertEquals(200, response.getStatus()); |
| 60 | + verify(featureFlagService).getAllFeatureFlags(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testGetFeatureFlagById_Found() { |
| 65 | + FeatureFlag flag = new FeatureFlag("test-feature", "test-value"); |
| 66 | + when(featureFlagService.getFeatureFlagById("test-feature")).thenReturn(flag); |
| 67 | + |
| 68 | + Response response = resource.getFeatureFlagById("test-feature"); |
| 69 | + assertEquals(200, response.getStatus()); |
| 70 | + assertNotNull(response.getEntity()); |
| 71 | + verify(featureFlagService).getFeatureFlagById("test-feature"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testGetFeatureFlagById_NotFound() { |
| 76 | + when(featureFlagService.getFeatureFlagById("non-existent")) |
| 77 | + .thenThrow(new NotFoundException("Feature flag with id 'non-existent' not found")); |
| 78 | + |
| 79 | + Response response = resource.getFeatureFlagById("non-existent"); |
| 80 | + assertEquals(404, response.getStatus()); |
| 81 | + verify(featureFlagService).getFeatureFlagById("non-existent"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testCreateOrUpdateFeatureFlag_Create() { |
| 86 | + UriInfo uriInfo = mock(UriInfo.class); |
| 87 | + UriBuilder uriBuilder = mock(UriBuilder.class); |
| 88 | + when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder); |
| 89 | + when(uriBuilder.path(anyString())).thenReturn(uriBuilder); |
| 90 | + when(uriBuilder.build()).thenReturn(URI.create("http://localhost/api/feature/new-feature")); |
| 91 | + |
| 92 | + FeatureFlag createdFlag = new FeatureFlag("new-feature", "new-value"); |
| 93 | + when(featureFlagService.exists("new-feature")).thenReturn(false); |
| 94 | + when(featureFlagService.createOrUpdateFeatureFlag("new-feature", "new-value")) |
| 95 | + .thenReturn(createdFlag); |
| 96 | + |
| 97 | + Map<String, String> body = Map.of("value", "new-value"); |
| 98 | + Response response = resource.createOrUpdateFeatureFlag(uriInfo, authUser, "new-feature", body); |
| 99 | + |
| 100 | + assertEquals(201, response.getStatus()); |
| 101 | + assertNotNull(response.getEntity()); |
| 102 | + verify(featureFlagService).exists("new-feature"); |
| 103 | + verify(featureFlagService).createOrUpdateFeatureFlag("new-feature", "new-value"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testCreateOrUpdateFeatureFlag_Update() { |
| 108 | + UriInfo uriInfo = mock(UriInfo.class); |
| 109 | + FeatureFlag updatedFlag = new FeatureFlag("existing-feature", "updated-value"); |
| 110 | + when(featureFlagService.exists("existing-feature")).thenReturn(true); |
| 111 | + when(featureFlagService.createOrUpdateFeatureFlag("existing-feature", "updated-value")) |
| 112 | + .thenReturn(updatedFlag); |
| 113 | + |
| 114 | + Map<String, String> body = Map.of("value", "updated-value"); |
| 115 | + Response response = |
| 116 | + resource.createOrUpdateFeatureFlag(uriInfo, authUser, "existing-feature", body); |
| 117 | + |
| 118 | + assertEquals(200, response.getStatus()); |
| 119 | + assertNotNull(response.getEntity()); |
| 120 | + verify(featureFlagService).exists("existing-feature"); |
| 121 | + verify(featureFlagService).createOrUpdateFeatureFlag("existing-feature", "updated-value"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testCreateOrUpdateFeatureFlag_MissingValue() { |
| 126 | + UriInfo uriInfo = mock(UriInfo.class); |
| 127 | + Map<String, String> body = Map.of(); |
| 128 | + |
| 129 | + Response response = resource.createOrUpdateFeatureFlag(uriInfo, authUser, "test-id", body); |
| 130 | + |
| 131 | + assertEquals(400, response.getStatus()); |
| 132 | + assertNotNull(response.getEntity()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void testCreateOrUpdateFeatureFlag_ServiceError() { |
| 137 | + UriInfo uriInfo = mock(UriInfo.class); |
| 138 | + when(featureFlagService.exists("test-id")).thenReturn(false); |
| 139 | + when(featureFlagService.createOrUpdateFeatureFlag("test-id", "value")) |
| 140 | + .thenThrow(new RuntimeException("Database error")); |
| 141 | + |
| 142 | + Map<String, String> body = Map.of("value", "value"); |
| 143 | + Response response = resource.createOrUpdateFeatureFlag(uriInfo, authUser, "test-id", body); |
| 144 | + |
| 145 | + assertEquals(500, response.getStatus()); |
| 146 | + verify(featureFlagService).exists("test-id"); |
| 147 | + verify(featureFlagService).createOrUpdateFeatureFlag("test-id", "value"); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testDeleteFeatureFlag_Success() { |
| 152 | + doNothing().when(featureFlagService).deleteFeatureFlag("test-id"); |
| 153 | + |
| 154 | + Response response = resource.deleteFeatureFlag(authUser, "test-id"); |
| 155 | + |
| 156 | + assertEquals(204, response.getStatus()); |
| 157 | + verify(featureFlagService).deleteFeatureFlag("test-id"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + void testDeleteFeatureFlag_NotFound() { |
| 162 | + doThrow(new NotFoundException("Feature flag with id 'non-existent' not found")) |
| 163 | + .when(featureFlagService) |
| 164 | + .deleteFeatureFlag("non-existent"); |
| 165 | + |
| 166 | + Response response = resource.deleteFeatureFlag(authUser, "non-existent"); |
| 167 | + |
| 168 | + assertEquals(404, response.getStatus()); |
| 169 | + verify(featureFlagService).deleteFeatureFlag("non-existent"); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + void testDeleteFeatureFlag_ServiceError() { |
| 174 | + doThrow(new RuntimeException("Database error")) |
| 175 | + .when(featureFlagService) |
| 176 | + .deleteFeatureFlag("test-id"); |
| 177 | + |
| 178 | + Response response = resource.deleteFeatureFlag(authUser, "test-id"); |
| 179 | + |
| 180 | + assertEquals(500, response.getStatus()); |
| 181 | + verify(featureFlagService).deleteFeatureFlag("test-id"); |
| 182 | + } |
| 183 | +} |
0 commit comments