From b3961fa4362b51aa50b8b23df9bc79c95520a65c Mon Sep 17 00:00:00 2001 From: Maciej Lew Date: Mon, 10 Jun 2024 12:51:58 +0200 Subject: [PATCH 1/3] Adds HLR action factory --- CHANGELOG.md | 1 + .../pl/smsapi/api/action/hlr/HlrFactory.java | 22 ++++++++++++ .../smsapi/api/action/hlr/HlrFactoryTest.java | 34 +++++++++++++++++++ src/test/java/pl/smsapi/test/run/HlrTest.java | 21 ------------ 4 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java create mode 100644 src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java delete mode 100644 src/test/java/pl/smsapi/test/run/HlrTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index c66e1ef..454c903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - `pl.smsapi.exception.SmsapiErrorException` to handle API error responses - `date_sent` to SMS/MMS send action responses - `time_restriction` parameter for SMS send action +- HLR action factory, `HlrFactory` ### Changed - `pl.smsapi.api.UserFactory.actionAdd` marked as deprecated, use `pl.smsapi.api.action.subusers.SubusersFactory.actionAdd` instead diff --git a/src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java b/src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java new file mode 100644 index 0000000..d4f6565 --- /dev/null +++ b/src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java @@ -0,0 +1,22 @@ +package pl.smsapi.api.action.hlr; + +import pl.smsapi.Client; +import pl.smsapi.api.ActionFactory; +import pl.smsapi.proxy.Proxy; + +public class HlrFactory extends ActionFactory { + + public HlrFactory(Client client, Proxy proxy) { + super(client, proxy); + } + + public HLRCheckNumber actionCheckNumber(String phoneNumber) { + HLRCheckNumber action = new HLRCheckNumber(); + action.client(client); + action.proxy(proxy); + + action.setNumber(phoneNumber); + + return action; + } +} diff --git a/src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java b/src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java new file mode 100644 index 0000000..b73bb1b --- /dev/null +++ b/src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java @@ -0,0 +1,34 @@ +package pl.smsapi.api.action.hlr; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import pl.smsapi.api.response.CheckNumberResponse; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.TestSmsapi; + +import static org.junit.Assert.*; + +@Ignore +public class HlrFactoryTest extends TestSmsapi { + + HlrFactory apiFactory; + + @Before + public void setUp() { + super.setUp(); + apiFactory = new HlrFactory(client, proxy); + } + + @Test + public void checkNumberTest() throws SmsapiException { + + HLRCheckNumber actionCheckNumber = apiFactory.actionCheckNumber("500600700"); + + CheckNumberResponse responseCheckNumber = actionCheckNumber.execute(); + + assertNotNull(responseCheckNumber); + assertEquals(1, responseCheckNumber.count); + assertTrue(responseCheckNumber.list.stream().anyMatch(messageResponse -> messageResponse.getNumber().equals("500600700"))); + } +} diff --git a/src/test/java/pl/smsapi/test/run/HlrTest.java b/src/test/java/pl/smsapi/test/run/HlrTest.java deleted file mode 100644 index e3578da..0000000 --- a/src/test/java/pl/smsapi/test/run/HlrTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package pl.smsapi.test.run; - -import org.junit.Ignore; -import org.junit.Test; -import pl.smsapi.api.action.hlr.HLRCheckNumber; -import pl.smsapi.exception.SmsapiException; -import pl.smsapi.test.TestSmsapi; - -@Ignore -public class HlrTest extends TestSmsapi { - @Test - public void checkNumberTest() throws SmsapiException { - HLRCheckNumber action = new HLRCheckNumber(); - action.client(client); - action.proxy(proxy); - - action.setNumber("500600700"); - - action.execute(); - } -} From 463ae3b63a5962baac2395f56ce2d6f12ccb4349 Mon Sep 17 00:00:00 2001 From: Maciej Lew Date: Mon, 10 Jun 2024 13:56:53 +0200 Subject: [PATCH 2/3] Deprecates HLR check number without parameters action --- CHANGELOG.md | 1 + .../smsapi/api/action/hlr/HLRCheckNumber.java | 17 +++++++--- .../pl/smsapi/api/action/hlr/HlrFactory.java | 4 +-- .../api/action/hlr/CheckNumberMother.java | 25 +++++++++++++++ .../action/hlr}/CheckNumberResponseTest.java | 31 ++++--------------- .../api/action/hlr/HLRCheckNumberTest.java | 31 +++++++++++++++++++ 6 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 src/test/java/pl/smsapi/api/action/hlr/CheckNumberMother.java rename src/test/java/pl/smsapi/{test/unit/response => api/action/hlr}/CheckNumberResponseTest.java (54%) create mode 100644 src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 454c903..b91f2a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - `pl.smsapi.api.action.sms.SMSGet` without parameters marked as deprecated - `pl.smsapi.api.SmsFactory.actionDelete` without parameters marked as deprecated - `pl.smsapi.api.action.sms.SMSDelete` without parameters marked as deprecated +- `pl.smsapi.api.action.hlr.HLRCheckNumber` without parameters marked as deprecated ### Removed - legacy `phonebook.do` contacts API support diff --git a/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java b/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java index 352bad9..871c104 100644 --- a/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java +++ b/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java @@ -6,21 +6,28 @@ public class HLRCheckNumber extends AbstractAction { + /** + * @deprecated use {@link HLRCheckNumber(String)} instead + */ + @Deprecated public HLRCheckNumber() { setJson(true); } + public HLRCheckNumber(String phoneNumber) { + setJson(true); + params.put("number", phoneNumber); + } + /** + * @deprecated use {@link HLRCheckNumber(String)} instead + */ + @Deprecated public HLRCheckNumber setNumber(String number) { params.put("number", number); return this; } - /*public HLRCheckNumber setNumber(String[] number) { - params.put("number", StringUtils.join(number, ',')); - return this; - }*/ - protected CheckNumberResponse createResponse(String data) { JSONObject jsonObject = new JSONObject(data); return new CheckNumberResponse(jsonObject.getInt("count"), jsonObject.getJSONArray("list")); diff --git a/src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java b/src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java index d4f6565..deb38b4 100644 --- a/src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java +++ b/src/main/java/pl/smsapi/api/action/hlr/HlrFactory.java @@ -11,12 +11,10 @@ public HlrFactory(Client client, Proxy proxy) { } public HLRCheckNumber actionCheckNumber(String phoneNumber) { - HLRCheckNumber action = new HLRCheckNumber(); + HLRCheckNumber action = new HLRCheckNumber(phoneNumber); action.client(client); action.proxy(proxy); - action.setNumber(phoneNumber); - return action; } } diff --git a/src/test/java/pl/smsapi/api/action/hlr/CheckNumberMother.java b/src/test/java/pl/smsapi/api/action/hlr/CheckNumberMother.java new file mode 100644 index 0000000..4c3bc1d --- /dev/null +++ b/src/test/java/pl/smsapi/api/action/hlr/CheckNumberMother.java @@ -0,0 +1,25 @@ +package pl.smsapi.api.action.hlr; + +public class CheckNumberMother { + + public static String create() { + return + "{" + + " \"count\": 1," + + " \"list\": [" + + " {" + + " \"date\": 1712565008," + + " \"id\": \"1\"," + + " \"info\": \"Resource description\"," + + " \"mcc\": 260," + + " \"mnc\": 3," + + " \"number\": \"500600700\"," + + " \"ported\": 0," + + " \"ported_from\": 3," + + " \"price\": 0.04," + + " \"status\": \"OK\"" + + " }" + + " ]" + + "}"; + } +} diff --git a/src/test/java/pl/smsapi/test/unit/response/CheckNumberResponseTest.java b/src/test/java/pl/smsapi/api/action/hlr/CheckNumberResponseTest.java similarity index 54% rename from src/test/java/pl/smsapi/test/unit/response/CheckNumberResponseTest.java rename to src/test/java/pl/smsapi/api/action/hlr/CheckNumberResponseTest.java index fa1d64f..3e98886 100644 --- a/src/test/java/pl/smsapi/test/unit/response/CheckNumberResponseTest.java +++ b/src/test/java/pl/smsapi/api/action/hlr/CheckNumberResponseTest.java @@ -1,7 +1,6 @@ -package pl.smsapi.test.unit.response; +package pl.smsapi.api.action.hlr; import org.junit.Test; -import pl.smsapi.api.action.hlr.HLRCheckNumber; import pl.smsapi.api.response.CheckNumberResponse; import pl.smsapi.api.response.NumberResponse; import pl.smsapi.exception.SmsapiException; @@ -17,34 +16,16 @@ public class CheckNumberResponseTest { @Test public void deserialize_non_empty_response() throws SmsapiException { - HLRCheckNumber action = new HLRCheckNumber(); + HLRCheckNumber action = new HLRCheckNumber("500600700"); action.client(new ClientStub()); - action.proxy(new ProxyResponseStub( - "{" + - " \"count\": 1," + - " \"list\": [" + - " {" + - " \"date\": 1712565008," + - " \"id\": \"1\"," + - " \"info\": \"Resource description\"," + - " \"mcc\": 260," + - " \"mnc\": 3," + - " \"number\": \"500600700\"," + - " \"ported\": 0," + - " \"ported_from\": 3," + - " \"price\": 0.04," + - " \"status\": \"OK\"" + - " }" + - " ]" + - "}" - )); + action.proxy(new ProxyResponseStub(CheckNumberMother.create())); CheckNumberResponse response = action.execute(); - assertFalse(response.getList().isEmpty()); - assertEquals(1, response.getCount()); + assertFalse(response.list.isEmpty()); + assertEquals(1, response.count); - Optional hlr1 = response.getList().stream().filter( + Optional hlr1 = response.list.stream().filter( hlrResponse -> hlrResponse.getId().equals("1") ).findFirst(); assertTrue(hlr1.isPresent()); diff --git a/src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java b/src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java new file mode 100644 index 0000000..48e5e3c --- /dev/null +++ b/src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java @@ -0,0 +1,31 @@ +package pl.smsapi.api.action.hlr; + +import org.junit.Test; +import pl.smsapi.api.action.sms.StatusJsonMother; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.doubles.ClientStub; +import pl.smsapi.test.doubles.ProxyRequestSpy; + +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +public class HLRCheckNumberTest { + + @Test + public void executeCheckNumberRequest() throws SmsapiException { + ProxyRequestSpy requestStub = new ProxyRequestSpy(StatusJsonMother.create()); + HLRCheckNumber action = new HLRCheckNumber("500600700"); + action.client(new ClientStub()); + action.proxy(requestStub); + + action.execute(); + + assertEquals("POST", requestStub.requestMethod); + assertEquals("hlrsync.do", requestStub.requestEndpoint); + HashMap expectedRequestPayload = new HashMap<>(); + expectedRequestPayload.put("number", "500600700"); + expectedRequestPayload.put("format", "json"); + assertEquals(expectedRequestPayload, requestStub.requestPayload); + } +} From 0452abb626f161e208813f4b2f71b1e4ba9a55fa Mon Sep 17 00:00:00 2001 From: Maciej Lew Date: Mon, 10 Jun 2024 14:14:52 +0200 Subject: [PATCH 3/3] Adds HLR check number with IDX support --- CHANGELOG.md | 1 + .../smsapi/api/action/hlr/HLRCheckNumber.java | 8 ++++++++ .../api/action/hlr/HLRCheckNumberTest.java | 19 +++++++++++++++++++ .../smsapi/api/action/hlr/HlrFactoryTest.java | 5 +++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b91f2a5..99777ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - `date_sent` to SMS/MMS send action responses - `time_restriction` parameter for SMS send action - HLR action factory, `HlrFactory` +- HLR with IDX support ### Changed - `pl.smsapi.api.UserFactory.actionAdd` marked as deprecated, use `pl.smsapi.api.action.subusers.SubusersFactory.actionAdd` instead diff --git a/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java b/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java index 871c104..6f6f5d2 100644 --- a/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java +++ b/src/main/java/pl/smsapi/api/action/hlr/HLRCheckNumber.java @@ -28,6 +28,14 @@ public HLRCheckNumber setNumber(String number) { return this; } + /** + * Set optional custom value sent with HLR and sent back in CALLBACK. + */ + public HLRCheckNumber setIDx(String idx) { + params.put("idx", idx); + return this; + } + protected CheckNumberResponse createResponse(String data) { JSONObject jsonObject = new JSONObject(data); return new CheckNumberResponse(jsonObject.getInt("count"), jsonObject.getJSONArray("list")); diff --git a/src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java b/src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java index 48e5e3c..93a4698 100644 --- a/src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java +++ b/src/test/java/pl/smsapi/api/action/hlr/HLRCheckNumberTest.java @@ -28,4 +28,23 @@ public void executeCheckNumberRequest() throws SmsapiException { expectedRequestPayload.put("format", "json"); assertEquals(expectedRequestPayload, requestStub.requestPayload); } + + @Test + public void executeCheckNumberWithOptionalFieldsRequest() throws SmsapiException { + ProxyRequestSpy requestStub = new ProxyRequestSpy(StatusJsonMother.create()); + HLRCheckNumber action = new HLRCheckNumber("500600700"); + action.client(new ClientStub()); + action.proxy(requestStub); + action.setIDx("example-user-provided-id-123"); + + action.execute(); + + assertEquals("POST", requestStub.requestMethod); + assertEquals("hlrsync.do", requestStub.requestEndpoint); + HashMap expectedRequestPayload = new HashMap<>(); + expectedRequestPayload.put("number", "500600700"); + expectedRequestPayload.put("format", "json"); + expectedRequestPayload.put("idx", "example-user-provided-id-123"); + assertEquals(expectedRequestPayload, requestStub.requestPayload); + } } diff --git a/src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java b/src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java index b73bb1b..eb92e0a 100644 --- a/src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java +++ b/src/test/java/pl/smsapi/api/action/hlr/HlrFactoryTest.java @@ -22,8 +22,9 @@ public void setUp() { @Test public void checkNumberTest() throws SmsapiException { - - HLRCheckNumber actionCheckNumber = apiFactory.actionCheckNumber("500600700"); + HLRCheckNumber actionCheckNumber = apiFactory + .actionCheckNumber("500600700") + .setIDx("example-user-provided-id-123"); CheckNumberResponse responseCheckNumber = actionCheckNumber.execute();