Skip to content

Commit

Permalink
feat (SMS/Inbounds): inbounds API
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Dec 19, 2024
1 parent 1a777d2 commit 265e3b8
Show file tree
Hide file tree
Showing 17 changed files with 820 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sinch.sdk.domains.sms.api.v1;

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.domains.sms.models.v1.inbounds.InboundMessage;
import com.sinch.sdk.domains.sms.models.v1.inbounds.request.ListInboundMessagesQueryParameters;
import com.sinch.sdk.domains.sms.models.v1.inbounds.response.ListInboundsResponse;

public interface InboundsService {

ListInboundsResponse list(ListInboundMessagesQueryParameters parameters) throws ApiException;

InboundMessage get(String inboundId) throws ApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public interface SMSService {

BatchesService batches();

InboundsService inbounds();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.sinch.sdk.domains.sms.api.v1.adapters;

import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.AuthManager;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.domains.sms.api.v1.internal.InboundsApi;
import com.sinch.sdk.domains.sms.models.v1.batches.internal.SMSCursorPageNavigator;
import com.sinch.sdk.domains.sms.models.v1.inbounds.InboundMessage;
import com.sinch.sdk.domains.sms.models.v1.inbounds.request.ListInboundMessagesQueryParameters;
import com.sinch.sdk.domains.sms.models.v1.inbounds.response.ListInboundsResponse;
import com.sinch.sdk.domains.sms.models.v1.inbounds.response.internal.ApiInboundList;
import com.sinch.sdk.models.SmsContext;
import java.util.Map;

public class InboundsService implements com.sinch.sdk.domains.sms.api.v1.InboundsService {

private final InboundsApi api;

public InboundsService(
String uriUUID,
SmsContext context,
HttpClient httpClient,
Map<String, AuthManager> authManagers) {
this.api =
new InboundsApi(
httpClient, context.getSmsServer(), authManagers, new HttpMapper(), uriUUID);
}

protected InboundsApi getApi() {
return this.api;
}

public ListInboundsResponse list(ListInboundMessagesQueryParameters parameters)
throws ApiException {

ListInboundMessagesQueryParameters guardParameters =
null != parameters ? parameters : ListInboundMessagesQueryParameters.builder().build();

ApiInboundList response = getApi().list(parameters);

SMSCursorPageNavigator navigator =
new SMSCursorPageNavigator(response.getPage(), response.getPageSize());

return new ListInboundsResponse(
this, new Page<>(guardParameters, response.getInbounds(), navigator));
}

public InboundMessage get(String inboundId) throws ApiException {
return getApi().get(inboundId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public class SMSService implements com.sinch.sdk.domains.sms.api.v1.SMSService {
private final String uriUUID;
private final SmsContext context;
private final HttpClient httpClient;
private BatchesService batches;
private final Map<String, AuthManager> authManagers;

private BatchesService batches;
private InboundsService inbounds;

public SMSService(
UnifiedCredentials credentials,
SmsContext context,
Expand Down Expand Up @@ -85,4 +87,12 @@ public BatchesService batches() {
}
return this.batches;
}

@Override
public InboundsService inbounds() {
if (null == this.inbounds) {
this.inbounds = new InboundsService(uriUUID, context, httpClient, authManagers);
}
return this.inbounds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sinch.sdk.domains.sms.models.v1.inbounds;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.sinch.sdk.domains.sms.models.v1.inbounds.response.internal.InboundInternalImpl;

@JsonDeserialize(using = InboundInternalImpl.Deserializer.class)
public interface InboundMessage {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.sinch.sdk.domains.sms.models.v1.inbounds.response;

import com.sinch.sdk.core.models.pagination.ListResponse;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.domains.sms.api.v1.InboundsService;
import com.sinch.sdk.domains.sms.models.v1.inbounds.InboundMessage;
import com.sinch.sdk.domains.sms.models.v1.inbounds.request.ListInboundMessagesQueryParameters;
import java.util.Collection;
import java.util.NoSuchElementException;

public class ListInboundsResponse extends ListResponse<InboundMessage> {

private final Page<ListInboundMessagesQueryParameters, InboundMessage, Integer> page;
private final InboundsService service;
private ListInboundsResponse nextPage;

public ListInboundsResponse(
InboundsService service,
Page<ListInboundMessagesQueryParameters, InboundMessage, Integer> page) {
this.service = service;
this.page = page;
}

public boolean hasNextPage() {

if (null == nextPage) {
ListInboundMessagesQueryParameters.Builder newParameters =
ListInboundMessagesQueryParameters.builder(page.getParameters());
newParameters.setPage(page.getNextPageToken());
nextPage = service.list(newParameters.build());
}
return (null != nextPage.getContent() && !nextPage.getContent().isEmpty());
}

public ListInboundsResponse nextPage() {

if (!hasNextPage()) {
throw new NoSuchElementException("Reached the last page of the API response");
}

ListInboundsResponse response = nextPage;
nextPage = null;
return response;
}

public Collection<InboundMessage> getContent() {
return page.getEntities();
}

@Override
public String toString() {
return "ListInboundsResponse{" + "page=" + page + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.sinch.sdk.domains.sms.api.v1.adapters;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import com.adelean.inject.resources.junit.jupiter.GivenJsonResource;
import com.adelean.inject.resources.junit.jupiter.TestWithResources;
import com.sinch.sdk.BaseTest;
import com.sinch.sdk.core.TestHelpers;
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.AuthManager;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.domains.sms.api.v1.internal.InboundsApi;
import com.sinch.sdk.domains.sms.models.v1.inbounds.BinaryMessage;
import com.sinch.sdk.domains.sms.models.v1.inbounds.InboundMessage;
import com.sinch.sdk.domains.sms.models.v1.inbounds.TextMessage;
import com.sinch.sdk.domains.sms.models.v1.inbounds.request.ListInboundMessagesQueryParameters;
import com.sinch.sdk.domains.sms.models.v1.inbounds.response.ListInboundsResponse;
import com.sinch.sdk.domains.sms.models.v1.inbounds.response.internal.ApiInboundList;
import com.sinch.sdk.models.SmsContext;
import java.time.Instant;
import java.util.Iterator;
import java.util.Map;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

@TestWithResources
class InboundsServiceTest extends BaseTest {

@Mock SmsContext context;
@Mock HttpClient httpClient;
@Mock Map<String, AuthManager> authManagers;
@Mock InboundsApi api;
InboundsService service;

String uriPartID = "foovalue";

@GivenJsonResource("/domains/sms/v1/inbounds/MOBinaryDto.json")
InboundMessage binary;

@GivenJsonResource("/domains/sms/v1/inbounds/MOTextDto.json")
InboundMessage text;

@GivenJsonResource("/domains/sms/v1/inbounds/MOMediaDto.json")
InboundMessage media;

@GivenJsonResource("/domains/sms/v1/inbounds/response/internal/InboundsListResponseDtoPage0.json")
ApiInboundList inboundsLisResponseDtoPage0;

@GivenJsonResource("/domains/sms/v1/inbounds/response/internal/InboundsListResponseDtoPage1.json")
ApiInboundList inboundsLisResponseDtoPage1;

@GivenJsonResource("/domains/sms/v1/inbounds/response/internal/InboundsListResponseDtoPage2.json")
ApiInboundList inboundsLisResponseDtoPage2;

@BeforeEach
public void initMocks() {
service = spy(new InboundsService(uriPartID, context, httpClient, authManagers));
doReturn(api).when(service).getApi();
}

@Test
void getBinary() throws ApiException {

when(api.get(eq("foo inbound ID"))).thenReturn(binary);

InboundMessage response = service.get("foo inbound ID");

TestHelpers.recursiveEquals(response, binary);
}

@Test
void getText() throws ApiException {

when(api.get(eq("foo inbound ID"))).thenReturn(text);

InboundMessage response = service.get("foo inbound ID");

TestHelpers.recursiveEquals(response, text);
}

@Test
void getMedia() throws ApiException {

when(api.get(eq("foo inbound ID"))).thenReturn(media);

InboundMessage response = service.get("foo inbound ID");

TestHelpers.recursiveEquals(response, media);
}

@Test
void list() throws ApiException {

ListInboundMessagesQueryParameters initialRequest =
ListInboundMessagesQueryParameters.builder().build();
ListInboundMessagesQueryParameters page1 =
ListInboundMessagesQueryParameters.builder().setPage(1).build();
ListInboundMessagesQueryParameters page2 =
ListInboundMessagesQueryParameters.builder().setPage(2).build();

when(api.list(initialRequest)).thenReturn(inboundsLisResponseDtoPage0);
when(api.list(page1)).thenReturn(inboundsLisResponseDtoPage1);
when(api.list(page2)).thenReturn(inboundsLisResponseDtoPage2);

ListInboundsResponse response = service.list(initialRequest);

Iterator<InboundMessage> iterator = response.iterator();
InboundMessage item = iterator.next();
Assertions.assertThat(item)
.usingRecursiveComparison()
.isEqualTo(
BinaryMessage.builder()
.setBody("a body")
.setClientReference("a client reference")
.setFrom("+11203494390")
.setId("01FC66621XXXXX119Z8PMV1QPA")
.setOperatorId("35000")
.setReceivedAt(Instant.parse("2019-08-24T14:17:22Z"))
.setSentAt(Instant.parse("2019-08-24T14:15:22Z"))
.setTo("11203453453")
.setUdh("foo udh")
.build());

item = iterator.next();
Assertions.assertThat(iterator.hasNext()).isEqualTo(true);
Assertions.assertThat(item)
.usingRecursiveComparison()
.isEqualTo(
TextMessage.builder()
.setBody("a body")
.setClientReference("a client reference")
.setFrom("+11203494390")
.setId("01FC66621XXXXX119Z8PMV1QPA")
.setOperatorId("35000")
.setReceivedAt(Instant.parse("2019-08-24T14:17:22Z"))
.setSentAt(Instant.parse("2019-08-24T14:15:22Z"))
.setTo("11203453453")
.build());

item = iterator.next();
Assertions.assertThat(iterator.hasNext()).isEqualTo(false);
Assertions.assertThat(item)
.usingRecursiveComparison()
.isEqualTo(
BinaryMessage.builder()
.setBody("a body")
.setClientReference("a client reference")
.setFrom("+11203494390")
.setId("01FC66621XXXXX119Z8PMV1QPA")
.setOperatorId("35000")
.setReceivedAt(Instant.parse("2019-08-24T14:17:22Z"))
.setSentAt(Instant.parse("2019-08-24T14:15:22Z"))
.setTo("11203453453")
.setUdh("foo udh")
.build());
}
}
Loading

0 comments on commit 265e3b8

Please sign in to comment.