diff --git a/Bots.ipr b/Bots.ipr index cea2e68f..422dfb6d 100644 --- a/Bots.ipr +++ b/Bots.ipr @@ -17,7 +17,7 @@ - + @@ -29,12 +29,12 @@ - + - + @@ -261,7 +261,7 @@ - + @@ -292,12 +292,18 @@ + + + + + + @@ -311,70 +317,70 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -399,72 +405,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -553,17 +493,6 @@ - - - - - - - - - - - @@ -608,103 +537,103 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -718,147 +647,147 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -894,6 +823,17 @@ + + + + + + + + + + + diff --git a/README.md b/README.md index 1967396f..30ee24ec 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ Just import add the library to your project with one of these options: ``` - 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/v2.4.4.1) - 3. Download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.4.4.1) + 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/v2.4.4.3) + 3. Download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.4.4.3) In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`. diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index 0e394c05..4974c335 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -23,4 +23,10 @@ 1. New `max_connections` in `setWebhook` method. 2. New `allowed_updates` in `setWebhook` and `getUpdates` 3. New `deleteWebhook` method -4. Added new configs to DefaultBotOptions to handle `max_connections` and `allowed_updates` \ No newline at end of file +4. Added new configs to DefaultBotOptions to handle `max_connections` and `allowed_updates` + +### 2.4.4.3 ### +1. In `BotSession`, renamed `close` to `stop`. `Close` method is maintained for backward compatibility. +2. Support crating webhook with HTTP servers (HTTPS must be managed via external tools) + +**[[How to update to version 2.4.4.3|How-To-Update#2.4.4.3]]** \ No newline at end of file diff --git a/TelegramBots.wiki/Getting-Started.md b/TelegramBots.wiki/Getting-Started.md index 7b74bf20..47d76e47 100644 --- a/TelegramBots.wiki/Getting-Started.md +++ b/TelegramBots.wiki/Getting-Started.md @@ -11,13 +11,13 @@ First you need ot get the library and add it to your project. There are few poss org.telegram telegrambots - 2.4.4.1 + 2.4.4.3 ``` * With **Gradle**: ```groovy - compile group: 'org.telegram', name: 'telegrambots', version: '2.4.4.1' + compile group: 'org.telegram', name: 'telegrambots', version: '2.4.4.3' ``` 2. Don't like **Maven Central Repository**? It can also be taken from [Jitpack](https://jitpack.io/#rubenlagus/TelegramBots). diff --git a/TelegramBots.wiki/How-To-Update.md b/TelegramBots.wiki/How-To-Update.md index 7b51a2a4..399d7ac5 100644 --- a/TelegramBots.wiki/How-To-Update.md +++ b/TelegramBots.wiki/How-To-Update.md @@ -13,4 +13,7 @@ ### To version 2.4.4 ### 1. Replace `ReplyKeyboardHide` by `ReplyKeyboardRemove` and its field `hideKeyboard` by `removeKeyboard` (remember getter and setters) 2. Replace usage of `edit_message` by `disable_edit_message` (see [this post](https://telegram.me/BotNews/22)) -3. Removed deprecated stuff from version 2.4.3 \ No newline at end of file +3. Removed deprecated stuff from version 2.4.3 + +### To version 2.4.4.3 ### +1. Replace `BotSession.close()` by `BotSession.stop()`. \ No newline at end of file diff --git a/pom.xml b/pom.xml index 83c78ffd..5c4788ee 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots pom - 2.4.4.1 + 2.4.4.3 telegrambots @@ -24,6 +24,6 @@ true - 2.4.4.1 + 2.4.4.3 \ No newline at end of file diff --git a/telegrambots-meta/pom.xml b/telegrambots-meta/pom.xml index ea2149da..02f72e0f 100644 --- a/telegrambots-meta/pom.xml +++ b/telegrambots-meta/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.telegram telegrambots-meta - 2.4.4.1 + 2.4.4.3 jar Telegram Bots Meta diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java index 3978c0a3..8a959eb7 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java @@ -28,7 +28,29 @@ public class TelegramBotsApi { } /** + * Creates an HTTP server to receive webhook request + * @param externalUrl External base url for the webhook + * @param internalUrl Internal base url for the webhook * + * @implSpec This option requires externally handled HTTPS support (i.e. via proxy) + */ + public TelegramBotsApi(String externalUrl, String internalUrl) throws TelegramApiRequestException { + if (externalUrl == null || externalUrl.isEmpty()) { + throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty"); + } + if (internalUrl == null || internalUrl.isEmpty()) { + throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty"); + } + + this.useWebhook = true; + this.extrenalUrl = fixExternalUrl(externalUrl); + webhook = ApiContext.getInstance(Webhook.class); + webhook.setInternalUrl(internalUrl); + webhook.startServer(); + } + + /** + * Creates an HTTPS server to receive webhook request * @param keyStore KeyStore for the server * @param keyStorePassword Key store password for the server * @param externalUrl External base url for the webhook @@ -41,6 +63,12 @@ public class TelegramBotsApi { if (internalUrl == null || internalUrl.isEmpty()) { throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty"); } + if (keyStore == null || keyStore.isEmpty()) { + throw new TelegramApiRequestException("Parameter keyStore can not be null or empty"); + } + if (keyStorePassword == null || keyStorePassword.isEmpty()) { + throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty"); + } this.useWebhook = true; this.extrenalUrl = fixExternalUrl(externalUrl); @@ -51,7 +79,7 @@ public class TelegramBotsApi { } /** - * + * Creates an HTTPS server with self-signed certificate to receive webhook request * @param keyStore KeyStore for the server * @param keyStorePassword Key store password for the server * @param externalUrl External base url for the webhook @@ -65,6 +93,16 @@ public class TelegramBotsApi { if (internalUrl == null || internalUrl.isEmpty()) { throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty"); } + if (keyStore == null || keyStore.isEmpty()) { + throw new TelegramApiRequestException("Parameter keyStore can not be null or empty"); + } + if (keyStorePassword == null || keyStorePassword.isEmpty()) { + throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty"); + } + if (pathToCertificate == null || pathToCertificate.isEmpty()) { + throw new TelegramApiRequestException("Parameter pathToCertificate can not be null or empty"); + } + this.useWebhook = true; this.extrenalUrl = fixExternalUrl(externalUrl); this.pathToCertificate = pathToCertificate; diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/BotSession.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/BotSession.java index c02a3e62..bff5af03 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/BotSession.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/BotSession.java @@ -10,7 +10,27 @@ public interface BotSession { void setOptions(BotOptions options); void setToken(String token); void setCallback(LongPollingBot callback); + + /** + * Starts the bot + */ void start(); - void close(); + + /** + * Stops the bot + */ + void stop(); + + /** + * Check if the bot is running + * @return True if the bot is running, false otherwise + */ boolean isRunning(); + + /** + * @deprecated Use @link{{@link #stop()}} instead + */ + default void close() { + this.stop(); + } } diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/TestTelegramApi.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/TestTelegramApi.java deleted file mode 100644 index 76426b74..00000000 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/TestTelegramApi.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.telegram.telegrambots; - -import org.junit.Assert; -import org.junit.Test; -import org.telegram.telegrambots.base.TestBase; -import org.telegram.telegrambots.exceptions.TelegramApiRequestException; - -/** - * @author Ruben Bermudez - * @version 1.0 - * @brief TODO - * @date 29 of October of 2016 - */ -public class TestTelegramApi extends TestBase { - - @Test - public void TestTelegramApiMustBeInitializableForLongPolling() { - new TelegramBotsApi(); - } - - @Test - public void TestTelegramApiMustBeInitializableForWebhook() { - try { - new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl"); - } catch (TelegramApiRequestException e) { - Assert.fail(); - } - } -} diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/TelegramBotsHelper.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TelegramBotsHelper.java similarity index 99% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/TelegramBotsHelper.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TelegramBotsHelper.java index 07d3e3be..772539a7 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/TelegramBotsHelper.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TelegramBotsHelper.java @@ -1,4 +1,4 @@ -package org.telegram.telegrambots; +package org.telegram.telegrambots.test; /** * @author Ruben Bermudez diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/TestDeserialization.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TestDeserialization.java similarity index 99% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/TestDeserialization.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TestDeserialization.java index 3c21414c..6ad6504a 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/TestDeserialization.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TestDeserialization.java @@ -1,4 +1,4 @@ -package org.telegram.telegrambots; +package org.telegram.telegrambots.test; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TestTelegramApi.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TestTelegramApi.java new file mode 100644 index 00000000..444cb91c --- /dev/null +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/TestTelegramApi.java @@ -0,0 +1,48 @@ +package org.telegram.telegrambots.test; + +import org.junit.Assert; +import org.junit.Test; +import org.telegram.telegrambots.TelegramBotsApi; +import org.telegram.telegrambots.test.base.TestBase; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief TODO + * @date 29 of October of 2016 + */ +public class TestTelegramApi extends TestBase { + + @Test + public void TestTelegramApiMustBeInitializableForLongPolling() { + new TelegramBotsApi(); + } + + @Test + public void TestTelegramApiMustBeInitializableForWebhookWithoutSecureSupport() { + try { + new TelegramBotsApi("externalUrl", "internalUrl"); + } catch (TelegramApiRequestException e) { + Assert.fail(); + } + } + + @Test + public void TestTelegramApiMustBeInitializableForWebhook() { + try { + new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl"); + } catch (TelegramApiRequestException e) { + Assert.fail(); + } + } + + @Test + public void TestTelegramApiMustBeInitializableForWebhookWithSelfSignedCertificate() { + try { + new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl", "selfSignedPath"); + } catch (TelegramApiRequestException e) { + Assert.fail(); + } + } +} diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/apimethods/TestGetUpdates.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestGetUpdates.java similarity index 94% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/apimethods/TestGetUpdates.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestGetUpdates.java index ec88fc93..f782da98 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/apimethods/TestGetUpdates.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestGetUpdates.java @@ -1,11 +1,11 @@ -package org.telegram.telegrambots.apimethods; +package org.telegram.telegrambots.test.apimethods; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.telegram.telegrambots.TelegramBotsHelper; +import org.telegram.telegrambots.test.TelegramBotsHelper; import org.telegram.telegrambots.api.methods.updates.GetUpdates; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.exceptions.TelegramApiRequestException; diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/apimethods/TestSetGameScore.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestSetGameScore.java similarity index 94% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/apimethods/TestSetGameScore.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestSetGameScore.java index 6b7b64ae..ba0d2bf8 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/apimethods/TestSetGameScore.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestSetGameScore.java @@ -1,11 +1,11 @@ -package org.telegram.telegrambots.apimethods; +package org.telegram.telegrambots.test.apimethods; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.telegram.telegrambots.TelegramBotsHelper; +import org.telegram.telegrambots.test.TelegramBotsHelper; import org.telegram.telegrambots.api.methods.games.SetGameScore; import org.telegram.telegrambots.api.objects.Message; diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/asserts/ApiAssert.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/asserts/ApiAssert.java similarity index 76% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/asserts/ApiAssert.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/asserts/ApiAssert.java index e4598f78..ba98633e 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/asserts/ApiAssert.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/asserts/ApiAssert.java @@ -1,4 +1,4 @@ -package org.telegram.telegrambots.asserts; +package org.telegram.telegrambots.test.asserts; /** * @author Ruben Bermudez diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/base/TestBase.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/base/TestBase.java similarity index 75% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/base/TestBase.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/base/TestBase.java index 2795a709..1b68147b 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/base/TestBase.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/base/TestBase.java @@ -1,9 +1,9 @@ -package org.telegram.telegrambots.base; +package org.telegram.telegrambots.test.base; import org.junit.BeforeClass; import org.telegram.telegrambots.ApiContext; -import org.telegram.telegrambots.fakes.FakeBotSession; -import org.telegram.telegrambots.fakes.FakeWebhook; +import org.telegram.telegrambots.test.fakes.FakeBotSession; +import org.telegram.telegrambots.test.fakes.FakeWebhook; import org.telegram.telegrambots.generics.BotSession; import org.telegram.telegrambots.generics.Webhook; diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/fakes/FakeBotSession.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/fakes/FakeBotSession.java similarity index 90% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/fakes/FakeBotSession.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/fakes/FakeBotSession.java index 2965b0f4..ec75847a 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/fakes/FakeBotSession.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/fakes/FakeBotSession.java @@ -1,4 +1,4 @@ -package org.telegram.telegrambots.fakes; +package org.telegram.telegrambots.test.fakes; import org.telegram.telegrambots.generics.BotOptions; import org.telegram.telegrambots.generics.BotSession; @@ -27,7 +27,7 @@ public class FakeBotSession implements BotSession { } @Override - public void close() { + public void stop() { } diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/fakes/FakeWebhook.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/fakes/FakeWebhook.java similarity index 96% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/fakes/FakeWebhook.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/test/fakes/FakeWebhook.java index b54298d4..a1d279ac 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/fakes/FakeWebhook.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/fakes/FakeWebhook.java @@ -1,4 +1,4 @@ -package org.telegram.telegrambots.fakes; +package org.telegram.telegrambots.test.fakes; import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.generics.Webhook; diff --git a/telegrambots/pom.xml b/telegrambots/pom.xml index 868344f1..14af9c8f 100644 --- a/telegrambots/pom.xml +++ b/telegrambots/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.telegram telegrambots - 2.4.4.1 + 2.4.4.3 jar Telegram Bots @@ -59,13 +59,13 @@ UTF-8 UTF-8 - 2.24 + 2.25 1.19.3 4.5.2 20160810 2.8.5 2.5 - 2.4.4.1 + 2.4.4.3 @@ -135,13 +135,19 @@ org.glassfish.jersey.test-framework jersey-test-framework-core - 2.24 + ${glassfish.version} test org.glassfish.jersey.test-framework.providers jersey-test-framework-provider-grizzly2 - 2.24 + ${glassfish.version} + test + + + org.mockito + mockito-all + 2.0.2-beta test diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java index 42e32f49..02d00f04 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java @@ -39,11 +39,10 @@ import java.util.concurrent.TimeUnit; /** * @author Ruben Bermudez * @version 1.0 - * @brief Thread to request updates with active wait - * @date 20 of June of 2015 + * Thread to request updates with active wait */ public class DefaultBotSession implements BotSession { - protected static final String LOGTAG = "BOTSESSION"; + private static final String LOGTAG = "BOTSESSION"; private volatile boolean running = false; @@ -62,34 +61,40 @@ public class DefaultBotSession implements BotSession { } @Override - public void start() { + public synchronized void start() { if (running) { throw new IllegalStateException("Session already running"); } running = true; - if (readerThread == null || readerThread.isInterrupted()) { - readerThread = new ReaderThread(); - readerThread.setName(callback.getBotUsername() + " Telegram Connection"); - } + + lastReceivedUpdate = 0; + + readerThread = new ReaderThread(); + readerThread.setName(callback.getBotUsername() + " Telegram Connection"); readerThread.start(); - if (handlerThread == null || handlerThread.isInterrupted()) { - handlerThread = new HandlerThread(); - handlerThread.setName(callback.getBotUsername() + " Telegram Executor"); - } + handlerThread = new HandlerThread(); + handlerThread.setName(callback.getBotUsername() + " Telegram Executor"); handlerThread.start(); } @Override - public void close() { + public synchronized void stop() { + if (!running) { + throw new IllegalStateException("Session already stopped"); + } + running = false; + if (readerThread != null) { readerThread.interrupt(); } + if (handlerThread != null) { handlerThread.interrupt(); } + if (callback != null) { callback.onClosing(); } @@ -120,7 +125,7 @@ public class DefaultBotSession implements BotSession { } @Override - public boolean isRunning() { + public synchronized boolean isRunning() { return running; } @@ -130,15 +135,14 @@ public class DefaultBotSession implements BotSession { @Override public synchronized void start() { - super.start(); - httpclient = HttpClientBuilder.create() .setSSLHostnameVerifier(new NoopHostnameVerifier()) .setConnectionTimeToLive(70, TimeUnit.SECONDS) .setMaxConnTotal(100) .build(); - requestConfig = options.getRequestConfig(); + + super.start(); } @Override diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java index 0b9a26f4..1e9d5384 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java @@ -49,20 +49,24 @@ public class DefaultWebhook implements Webhook { } public void startServer() throws TelegramApiRequestException { - SSLContextConfigurator sslContext = new SSLContextConfigurator(); - - // set up security context - sslContext.setKeyStoreFile(keystoreServerFile); // contains server keypair - sslContext.setKeyStorePass(keystoreServerPwd); - ResourceConfig rc = new ResourceConfig(); rc.register(restApi); rc.register(JacksonFeature.class); - final HttpServer grizzlyServer = GrizzlyHttpServerFactory.createHttpServer( - getBaseURI(), - rc, - true, - new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false)); + + final HttpServer grizzlyServer; + if (keystoreServerFile != null && keystoreServerPwd != null) { + SSLContextConfigurator sslContext = new SSLContextConfigurator(); + + // set up security context + sslContext.setKeyStoreFile(keystoreServerFile); // contains server keypair + sslContext.setKeyStorePass(keystoreServerPwd); + + grizzlyServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI(), rc, true, + new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false)); + } else { + grizzlyServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI(), rc); + } + try { grizzlyServer.start(); } catch (IOException e) { diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/TestDefaultBotSession.java b/telegrambots/src/test/java/org/telegram/telegrambots/TestDefaultBotSession.java deleted file mode 100644 index fbaf53dc..00000000 --- a/telegrambots/src/test/java/org/telegram/telegrambots/TestDefaultBotSession.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.telegram.telegrambots; - -import org.junit.Assert; -import org.junit.Test; -import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; - -/** - * @author Ruben Bermudez - * @version 1.0 - * @brief TODO - * @date 07 of November of 2016 - */ -public class TestDefaultBotSession { - @Test - public void TestDefaultBotSessionIsNotRunninWhenCreated() throws Exception { - Assert.assertFalse(new DefaultBotSession().isRunning()); - } -} diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/BotApiMethodHelperFactory.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/BotApiMethodHelperFactory.java similarity index 99% rename from telegrambots/src/test/java/org/telegram/telegrambots/BotApiMethodHelperFactory.java rename to telegrambots/src/test/java/org/telegram/telegrambots/test/BotApiMethodHelperFactory.java index 696d4ba8..71121580 100644 --- a/telegrambots/src/test/java/org/telegram/telegrambots/BotApiMethodHelperFactory.java +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/BotApiMethodHelperFactory.java @@ -1,4 +1,4 @@ -package org.telegram.telegrambots; +package org.telegram.telegrambots.test; import org.telegram.telegrambots.api.methods.ActionType; import org.telegram.telegrambots.api.methods.AnswerCallbackQuery; diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeLongPollingBot.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeLongPollingBot.java new file mode 100644 index 00000000..29e55ad0 --- /dev/null +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeLongPollingBot.java @@ -0,0 +1,39 @@ +package org.telegram.telegrambots.test.Fakes; + +import org.telegram.telegrambots.api.objects.Update; +import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.generics.BotOptions; +import org.telegram.telegrambots.generics.LongPollingBot; + +/** + * @author Ruben Bermudez + * @version 1.0 + */ +public class FakeLongPollingBot implements LongPollingBot +{ + @Override + public void onUpdateReceived(Update update) { + + } + + @Override + public String getBotUsername() { + return ""; + } + + @Override + public String getBotToken() { + return ""; + } + + @Override + public BotOptions getOptions() { + return new DefaultBotOptions(); + } + + @Override + public void clearWebhook() throws TelegramApiRequestException { + + } +} diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/Fakes/FakeWebhook.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeWebhook.java similarity index 91% rename from telegrambots/src/test/java/org/telegram/telegrambots/Fakes/FakeWebhook.java rename to telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeWebhook.java index cade79fc..f5a65b82 100644 --- a/telegrambots/src/test/java/org/telegram/telegrambots/Fakes/FakeWebhook.java +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeWebhook.java @@ -1,4 +1,4 @@ -package org.telegram.telegrambots.Fakes; +package org.telegram.telegrambots.test.Fakes; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.Update; @@ -8,8 +8,6 @@ import org.telegram.telegrambots.generics.WebhookBot; /** * @author Ruben Bermudez * @version 1.0 - * @brief TODO - * @date 01 of November of 2016 */ public class FakeWebhook implements WebhookBot { private BotApiMethod returnValue; diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java new file mode 100644 index 00000000..c37fd5a6 --- /dev/null +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java @@ -0,0 +1,96 @@ +package org.telegram.telegrambots.test; + +import org.apache.http.HttpResponse; +import org.apache.http.ProtocolVersion; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.message.BasicStatusLine; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.test.Fakes.FakeLongPollingBot; +import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; + +import java.io.IOException; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief Test for DefaultBotSession + */ +public class TestDefaultBotSession { + DefaultBotSession session; + + @Before + public void setUp() throws Exception { + session = getDefaultBotSession(); + } + + @After + public void tearDown() throws Exception { + if (session != null && session.isRunning()) { + session.stop(); + } + } + + @Test + public void TestDefaultBotSessionIsNotRunningWhenCreated() throws Exception { + Assert.assertFalse(session.isRunning()); + } + + @Test + public void TestDefaultBotSessionCanBeStartedAfterCreation() throws Exception { + session = getDefaultBotSession(); + session.start(); + Assert.assertTrue(session.isRunning()); + } + + @Test(expected = IllegalStateException.class) + public void TestDefaultBotSessionCanNotBeStoppedAfterCreation() throws Exception { + session = getDefaultBotSession(); + session.stop(); + } + + @Test(expected = IllegalStateException.class) + public void TestDefaultBotSessionCanNotBeStartedIfAlreadyStarted() throws Exception { + session = getDefaultBotSession(); + session.start(); + session.start(); + } + + @Test + public void TestDefaultBotSessionCanBeStoppedIfStarted() throws Exception { + session = getDefaultBotSession(); + session.start(); + session.stop(); + Assert.assertFalse(session.isRunning()); + } + + @Test(expected = IllegalStateException.class) + public void TestDefaultBotSessionCanNotBeStoppedIfAlreadyStopped() throws Exception { + session = getDefaultBotSession(); + session.start(); + session.stop(); + session.stop(); + } + + private DefaultBotSession getDefaultBotSession() throws IOException { + HttpResponse response = new BasicHttpResponse(new BasicStatusLine( + new ProtocolVersion("HTTP", 1, 1), 200, "")); + response.setStatusCode(200); + response.setEntity(new StringEntity("{}")); + + HttpClient mockHttpClient = Mockito.mock(HttpClient.class); + Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class))) + .thenReturn(response); + DefaultBotSession session = new DefaultBotSession(); + session.setCallback(new FakeLongPollingBot()); + session.setOptions(new DefaultBotOptions()); + return session; + } +} diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/TestRestApi.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestRestApi.java similarity index 93% rename from telegrambots/src/test/java/org/telegram/telegrambots/TestRestApi.java rename to telegrambots/src/test/java/org/telegram/telegrambots/test/TestRestApi.java index 30d6e81f..5f05b206 100644 --- a/telegrambots/src/test/java/org/telegram/telegrambots/TestRestApi.java +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestRestApi.java @@ -1,48 +1,29 @@ -package org.telegram.telegrambots; +package org.telegram.telegrambots.test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; - import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.Before; import org.junit.Test; -import org.telegram.telegrambots.Fakes.FakeWebhook; -import org.telegram.telegrambots.api.methods.AnswerCallbackQuery; -import org.telegram.telegrambots.api.methods.AnswerInlineQuery; -import org.telegram.telegrambots.api.methods.BotApiMethod; -import org.telegram.telegrambots.api.methods.ForwardMessage; -import org.telegram.telegrambots.api.methods.GetFile; -import org.telegram.telegrambots.api.methods.GetMe; -import org.telegram.telegrambots.api.methods.GetUserProfilePhotos; +import org.telegram.telegrambots.api.methods.*; import org.telegram.telegrambots.api.methods.games.GetGameHighScores; import org.telegram.telegrambots.api.methods.games.SetGameScore; -import org.telegram.telegrambots.api.methods.groupadministration.GetChat; -import org.telegram.telegrambots.api.methods.groupadministration.GetChatAdministrators; -import org.telegram.telegrambots.api.methods.groupadministration.GetChatMember; -import org.telegram.telegrambots.api.methods.groupadministration.GetChatMemberCount; -import org.telegram.telegrambots.api.methods.groupadministration.KickChatMember; -import org.telegram.telegrambots.api.methods.groupadministration.LeaveChat; -import org.telegram.telegrambots.api.methods.groupadministration.UnbanChatMember; -import org.telegram.telegrambots.api.methods.send.SendChatAction; -import org.telegram.telegrambots.api.methods.send.SendContact; -import org.telegram.telegrambots.api.methods.send.SendGame; -import org.telegram.telegrambots.api.methods.send.SendLocation; -import org.telegram.telegrambots.api.methods.send.SendMessage; -import org.telegram.telegrambots.api.methods.send.SendVenue; +import org.telegram.telegrambots.api.methods.groupadministration.*; +import org.telegram.telegrambots.api.methods.send.*; import org.telegram.telegrambots.api.methods.updates.GetWebhookInfo; import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageCaption; import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup; import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageText; import org.telegram.telegrambots.api.objects.Update; +import org.telegram.telegrambots.test.Fakes.FakeWebhook; import org.telegram.telegrambots.updatesreceivers.RestApi; -import java.io.IOException; - import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; +import java.io.IOException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail;