diff --git a/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/EventApiClient.kt b/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/EventApiClient.kt index 8ecf2e6..fb4f047 100644 --- a/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/EventApiClient.kt +++ b/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/EventApiClient.kt @@ -11,6 +11,20 @@ class EventApiClient(engine: HttpClientEngine) : IEventApiClient { private val apiHttpClient = ApiHttpClient(engine) private val httpClient = apiHttpClient.httpClient + + override suspend fun getEventsByPolicy(userId: String, policyId: String): Result> { + return try { + val response = httpClient.get("${apiHttpClient.url}/events/${userId}?policy=${policyId}") + + if (response.status == HttpStatusCode.OK) { + Result.success(response.body()) + } else { + Result.failure(Exception(response.status.description)) + } + } catch (e: Exception) { + Result.failure(e) + } + } override suspend fun getAllEventsForUser(userId: String): Result> { return try { diff --git a/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/interfaces/IEventApiClient.kt b/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/interfaces/IEventApiClient.kt index 796aec3..ac79701 100644 --- a/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/interfaces/IEventApiClient.kt +++ b/shared/src/commonMain/kotlin/com/mindera/minderapeople/apiclient/interfaces/IEventApiClient.kt @@ -3,6 +3,7 @@ package com.mindera.minderapeople.apiclient.interfaces import com.mindera.minderapeople.dto.EventDTO interface IEventApiClient { + suspend fun getEventsByPolicy(userId: String, policyId: String): Result> suspend fun getAllEventsForUser(userId: String): Result> suspend fun editExistingEvent(userId: String, event: EventDTO): Result suspend fun removeEventById(userId: String, eventId: String): Result diff --git a/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/EventRepository.kt b/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/EventRepository.kt index e02c81d..6e7b4cd 100644 --- a/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/EventRepository.kt +++ b/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/EventRepository.kt @@ -2,10 +2,10 @@ package com.mindera.minderapeople.repository import com.mindera.minderapeople.apiclient.interfaces.IEventApiClient import com.mindera.minderapeople.dto.EventDTO +import com.mindera.minderapeople.repository.interfaces.IEventRepository import com.mindera.minderapeople.dto.PartOfDayDTO import com.mindera.minderapeople.dto.PolicyDTO import com.mindera.minderapeople.dto.ProjectDTO -import com.mindera.minderapeople.repository.interfaces.IEventRepository class EventRepository(private val apiClient: IEventApiClient) : IEventRepository { override suspend fun getAllEventsForUser(userId: String): Result> { return apiClient.getAllEventsForUser(userId) @@ -32,4 +32,7 @@ class EventRepository(private val apiClient: IEventApiClient) : IEventRepository override suspend fun getEventById(userId: String, eventId: String): Result { return apiClient.getEventById(userId, eventId) } + override suspend fun getEventsByPolicy(userId: String, policyId: String): Result> { + return apiClient.getEventsByPolicy(userId, policyId) + } } diff --git a/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/interfaces/IEventRepository.kt b/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/interfaces/IEventRepository.kt index 7876d72..962b7a7 100644 --- a/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/interfaces/IEventRepository.kt +++ b/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/interfaces/IEventRepository.kt @@ -21,4 +21,5 @@ interface IEventRepository { ): Result suspend fun removeEventById(userId: String, event: EventDTO): Result suspend fun getEventById(userId: String, eventId: String): Result + suspend fun getEventsByPolicy(userId: String, policyId: String): Result> } diff --git a/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/interfaces/IProjectRepository.kt b/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/interfaces/IProjectRepository.kt deleted file mode 100644 index 8640fc2..0000000 --- a/shared/src/commonMain/kotlin/com/mindera/minderapeople/repository/interfaces/IProjectRepository.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.mindera.minderapeople.repository.interfaces - -import com.mindera.minderapeople.dto.ProjectDTO - -interface IProjectRepository { - suspend fun getAllProjects(): Result> -} \ No newline at end of file diff --git a/shared/src/commonTest/kotlin/com/mindera/minderapeople/integration/event/EventIntegrationTests.kt b/shared/src/commonTest/kotlin/com/mindera/minderapeople/integration/event/EventIntegrationTests.kt index 8480be6..b5d38db 100644 --- a/shared/src/commonTest/kotlin/com/mindera/minderapeople/integration/event/EventIntegrationTests.kt +++ b/shared/src/commonTest/kotlin/com/mindera/minderapeople/integration/event/EventIntegrationTests.kt @@ -314,4 +314,110 @@ class EventIntegrationTests { } } + @Test + fun `test getEventsByPolicy returns success and a list of events if successful`() { + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(Json.encodeToString(listOf(DefaultTestData.CORRECT_EVENT))), + status = HttpStatusCode.OK, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + val client = EventApiClient(mockEngine) + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, DefaultTestData.POLICY_ID_CORRECT) + + assertTrue(result.isSuccess) + assertNotEquals(null, result.getOrNull()) + assertEquals(listOf(DefaultTestData.CORRECT_EVENT), result.getOrNull()) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if userId is incorrect`(){ + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(""), + status = HttpStatusCode.NotFound, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + val client = EventApiClient(mockEngine) + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy("0001", DefaultTestData.POLICY_ID_CORRECT) + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if policyId is incorrect`(){ + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(""), + status = HttpStatusCode.NotFound, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + val client = EventApiClient(mockEngine) + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, "0001") + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if policyId and userId are incorrect`(){ + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(""), + status = HttpStatusCode.NotFound, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + val client = EventApiClient(mockEngine) + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy("0001", "0001") + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure when Exception is thrown`(){ + val error = "Error occurred" + val mockEngine = MockEngine { + throw Exception(error) + } + + val client = EventApiClient(mockEngine) + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, DefaultTestData.POLICY_ID_CORRECT) + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(error, result.exceptionOrNull()?.message) + } + } + } \ No newline at end of file diff --git a/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/DefaultTestData.kt b/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/DefaultTestData.kt index 7be0c7b..4b88a34 100644 --- a/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/DefaultTestData.kt +++ b/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/DefaultTestData.kt @@ -21,6 +21,7 @@ object DefaultTestData { //Events const val USER_ID_CORRECT = "3b1276b3-d2f6-4e29-af8f-a0cb00208dda" const val EVENT_ID_CORRECT = "f4b20503-0a59-4e76-8796-aee78726139f" + const val POLICY_ID_CORRECT = "001" val CORRECT_EVENT = EventDTO( EVENT_ID_CORRECT, PolicyDTO( diff --git a/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/EventApiClientMock.kt b/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/EventApiClientMock.kt index 2384e1e..90edf76 100644 --- a/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/EventApiClientMock.kt +++ b/shared/src/commonTest/kotlin/com/mindera/minderapeople/mocks/EventApiClientMock.kt @@ -5,6 +5,15 @@ import com.mindera.minderapeople.dto.EventDTO import io.ktor.http.* class EventApiClientMock : IEventApiClient { + override suspend fun getEventsByPolicy( + userId: String, + policyId: String + ): Result> { + if (userId == DefaultTestData.USER_ID_CORRECT && policyId == DefaultTestData.POLICY_ID_CORRECT) { + return Result.success(listOf(DefaultTestData.CORRECT_EVENT)) + } + return Result.failure(Exception(HttpStatusCode.NotFound.description)) + } override suspend fun getAllEventsForUser(userId: String): Result> { if (userId == DefaultTestData.USER_ID_CORRECT) { diff --git a/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/apiclient/EventApiClientTest.kt b/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/apiclient/EventApiClientTest.kt index 417d049..c1d4435 100644 --- a/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/apiclient/EventApiClientTest.kt +++ b/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/apiclient/EventApiClientTest.kt @@ -285,4 +285,101 @@ class EventApiClientTest { assertEquals(error, result.exceptionOrNull()?.message) } } + + @Test + fun `test getEventsByPolicy returns success and a list of events if successful`() { + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(Json.encodeToString(listOf(DefaultTestData.CORRECT_EVENT))), + status = HttpStatusCode.OK, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + runBlocking { + val client = EventApiClient(mockEngine) + val result = client.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, DefaultTestData.POLICY_ID_CORRECT) + + assertTrue(result.isSuccess) + assertNotEquals(null, result.getOrNull()) + assertEquals(listOf(DefaultTestData.CORRECT_EVENT), result.getOrNull()) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if userId is incorrect`(){ + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(""), + status = HttpStatusCode.NotFound, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + runBlocking { + val client = EventApiClient(mockEngine) + val result = client.getEventsByPolicy("0001", DefaultTestData.POLICY_ID_CORRECT) + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if policyId is incorrect`(){ + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(""), + status = HttpStatusCode.NotFound, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + runBlocking { + val client = EventApiClient(mockEngine) + val result = client.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, "0001") + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if policyId and userId are incorrect`(){ + val mockEngine = MockEngine { + respond( + content = ByteReadChannel(""), + status = HttpStatusCode.NotFound, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + runBlocking { + val client = EventApiClient(mockEngine) + val result = client.getEventsByPolicy("0001", "0001") + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure when Exception is thrown`(){ + val error = "Error occurred" + val mockEngine = MockEngine { + throw Exception(error) + } + + runBlocking { + val client = EventApiClient(mockEngine) + val result = client.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, DefaultTestData.EVENT_ID_CORRECT) + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(error, result.exceptionOrNull()?.message) + } + } } \ No newline at end of file diff --git a/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/repository/EventRepositoryTest.kt b/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/repository/EventRepositoryTest.kt index 8616275..aeacb27 100644 --- a/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/repository/EventRepositoryTest.kt +++ b/shared/src/commonTest/kotlin/com/mindera/minderapeople/unit/repository/EventRepositoryTest.kt @@ -184,4 +184,60 @@ class EventRepositoryTest { assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) } } + + @Test + fun `test getEventsByPolicy returns success and a list of events if successful`() { + val client = EventApiClientMock() + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, DefaultTestData.POLICY_ID_CORRECT) + + assertTrue(result.isSuccess) + assertNotEquals(null, result.getOrNull()) + assertEquals(listOf(DefaultTestData.CORRECT_EVENT), result.getOrNull()) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if userId is incorrect`(){ + val client = EventApiClientMock() + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy("0001", DefaultTestData.POLICY_ID_CORRECT) + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if policyId is incorrect`(){ + val client = EventApiClientMock() + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy(DefaultTestData.USER_ID_CORRECT, "abc") + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } + + @Test + fun `test getEventsByPolicy returns failure and NotFound status if policyId and userId are incorrect`(){ + val client = EventApiClientMock() + val eventRepo = EventRepository(client) + + runBlocking { + val result = eventRepo.getEventsByPolicy("0001", "abc") + + assertTrue(result.isFailure) + assertEquals(null, result.getOrNull()) + assertEquals(HttpStatusCode.NotFound.description, result.exceptionOrNull()?.message) + } + } }