Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPA-77_get-events-by-policy #8

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
819c9f9
MPA-73: Getting projects from the API
DaniCamelo Mar 30, 2023
83c265e
using HttpClientEngine for the HttpClient
DaniCamelo Apr 3, 2023
590e2ca
unit tests for ProjectRepository and ProjectApiClient
DaniCamelo Apr 3, 2023
9fa17eb
integration Tests for projectRepository and ProjectApiClient
DaniCamelo Apr 3, 2023
4a6315c
request specific event from API
DaniCamelo Apr 3, 2023
26a9abb
change name of method getEventBy to getEventById
DaniCamelo Apr 4, 2023
8844180
implemented get events by policy
DaniCamelo Apr 4, 2023
9074eeb
Update EventRepositoryImpl.kt
DaniCamelo Apr 4, 2023
cd3b02d
Merge branch 'MPA-71_get-specific-event' into MPA-77_get-events-by-po…
DaniCamelo Apr 4, 2023
81e2f6c
Merge branch 'MPA-71_get-specific-event' into MPA-77_get-events-by-po…
DaniCamelo Apr 4, 2023
95fa820
Merge branch 'MPA-71_get-specific-event' into MPA-77_get-events-by-po…
DaniCamelo Apr 6, 2023
7e8f93d
Merge branch 'MPA-71_get-specific-event' into MPA-77_get-events-by-po…
DaniCamelo Apr 10, 2023
7d7ae58
unit tests for getEventsByPolicy
DaniCamelo Apr 10, 2023
1bee040
integration tests for getEventsByPolicy
DaniCamelo Apr 10, 2023
2844972
Merge branch 'MPA-71_get-specific-event' into MPA-77_get-events-by-po…
DaniCamelo Apr 11, 2023
b957c92
Merge branch 'MPA-71_get-specific-event' into MPA-77_get-events-by-po…
DaniCamelo Apr 13, 2023
5cc0e26
using manually implemented mock instead of Mockative for getEventsByP…
DaniCamelo Apr 13, 2023
563f3da
added newLine
DaniCamelo May 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<EventDTO>> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline? :)

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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Há algum tipo de documentação ou swagger que diga o tipo de excepções que podem ocorrer?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não, é por isso que decidimos criar uma exception com a mensagem que vem da API

}
} catch (e: Exception) {
Result.failure(e)
}
}

override suspend fun getAllEventsForUser(userId: String): Result<List<EventDTO>> {
return try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<EventDTO>>
suspend fun getAllEventsForUser(userId: String): Result<List<EventDTO>>
suspend fun editExistingEvent(userId: String, event: EventDTO): Result<EventDTO>
suspend fun removeEventById(userId: String, eventId: String): Result<Nothing?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<EventDTO>> {
return apiClient.getAllEventsForUser(userId)
Expand All @@ -32,4 +32,7 @@ class EventRepository(private val apiClient: IEventApiClient) : IEventRepository
override suspend fun getEventById(userId: String, eventId: String): Result<EventDTO> {
return apiClient.getEventById(userId, eventId)
}
override suspend fun getEventsByPolicy(userId: String, policyId: String): Result<List<EventDTO>> {
return apiClient.getEventsByPolicy(userId, policyId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ interface IEventRepository {
): Result<EventDTO>
suspend fun removeEventById(userId: String, event: EventDTO): Result<Nothing?>
suspend fun getEventById(userId: String, eventId: String): Result<EventDTO>
suspend fun getEventsByPolicy(userId: String, policyId: String): Result<List<EventDTO>>
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<EventDTO>> {
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<List<EventDTO>> {
if (userId == DefaultTestData.USER_ID_CORRECT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}