Skip to content

Commit

Permalink
Merge branch 'main' into fix-127
Browse files Browse the repository at this point in the history
  • Loading branch information
Bindambc authored Jan 26, 2024
2 parents fc2c73c + 8211b24 commit 1e00dbb
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 32 deletions.
24 changes: 18 additions & 6 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
tests:
- src/test/**/*
- changed-files:
- any-glob-to-any-file: 'src/test/**/*'

java:
- '**/*.java'
- changed-files:
- any-glob-to-any-file: '**/*.java'

documentation:
- '**/*.md'
- changed-files:
- any-glob-to-any-file: '**/*.md'

markdown:
- '**/*.md'
- changed-files:
- any-glob-to-any-file: '**/*.md'

github-actions:
- .github/**/*
- changed-files:
- any-glob-to-any-file: '.github/**/*'

dependencies:
- pom.xml
- changed-files:
- any-glob-to-any-file: 'pom.xml'


8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:

- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
Expand All @@ -53,7 +53,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -64,7 +64,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -78,6 +78,6 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
2 changes: 1 addition & 1 deletion .github/workflows/javadoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
git config --local user.name "github-actions[bot]"
git merge origin/main
- name: Set up JDK 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
pull-requests: write

steps:
- uses: actions/labeler@v4
- uses: actions/labeler@v5
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
4 changes: 2 additions & 2 deletions .github/workflows/maven-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
Expand All @@ -26,4 +26,4 @@ jobs:
run: mvn -B package --file pom.xml
#coverage
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v3
2 changes: 1 addition & 1 deletion .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
- uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-javadoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<com.squareup.retrofit2.version>2.9.0</com.squareup.retrofit2.version>
<org.mockito.version>5.2.0</org.mockito.version>
<org.junit.jupiter.version>5.10.0</org.junit.jupiter.version>
<org.junit.jupiter.version>5.10.1</org.junit.jupiter.version>
<com.squareup.okhttp3.version>5.0.0-alpha.11</com.squareup.okhttp3.version>
</properties>

Expand Down Expand Up @@ -116,7 +116,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.1</version>
<configuration>
<source>17</source>
<target>17</target>
Expand All @@ -125,12 +125,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>3.6.3</version>
<configuration>
<source>17</source>
<reportOutputDirectory>${project.basedir}</reportOutputDirectory>
Expand All @@ -141,7 +141,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<version>0.8.11</version>
<executions>
<execution>
<goals>
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ private WhatsappApiServiceGenerator() {
}

static {
sharedClient = createDefaultHttpClient();
}

sharedClient = new OkHttpClient.Builder()//
public static OkHttpClient createDefaultHttpClient(){
return new OkHttpClient.Builder()//
.callTimeout(20, TimeUnit.SECONDS)//
.pingInterval(20, TimeUnit.SECONDS)//
.build();
Expand All @@ -58,28 +61,29 @@ private WhatsappApiServiceGenerator() {
* </ul>
* <p>
* @param host the host (Not null)
* @param port the port (Not null)
* @param port the port
* @param username the username
* @param pwd the pwd
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-selector/">Proxy Selector</a>
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-authenticator/">Proxy Authenticator</a>
*/
public static void setHttpProxy(String host, int port, String username, String pwd) {
Objects.requireNonNull(host, "Host cannot be null");
Objects.requireNonNull(port, "Http Port cannot be null");
CustomHttpProxySelector proxySelector = new CustomHttpProxySelector(host, port);

sharedClient = sharedClient.newBuilder()
.proxySelector(proxySelector)
.build();

if (username == null || pwd == null) {
sharedClient = sharedClient.newBuilder()
.proxySelector(proxySelector)
.build();
//Without authentication
return;
}

CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator(username, pwd);

sharedClient = sharedClient.newBuilder()
.authenticator(proxyAuthenticator)
.proxyAuthenticator(proxyAuthenticator)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public record Message(

@JsonProperty("id") String id,
@JsonProperty("message_status") String messageStatus){
@JsonProperty("message_status") String messageStatus)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public List<Proxy> select(URI uri) {
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
// Do something here
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@

public class CustomProxyAuthenticator implements Authenticator {

private final String credential;
private final String CREDENTIALS;

public CustomProxyAuthenticator(final String username, final String password) {
credential = Credentials.basic(username, password);
CREDENTIALS = Credentials.basic(username, password);
}

@Override
public Request authenticate(final Route route, final Response response) throws IOException {
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.header("Proxy-Authorization", CREDENTIALS)
.build();
}
}
119 changes: 119 additions & 0 deletions src/test/java/com/whatsapp/api/WhatsappApiServiceGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.whatsapp.api;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;
import java.net.ProxySelector;
import java.net.URISyntaxException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.whatsapp.api.domain.errors.WhatsappApiError;
import com.whatsapp.api.exception.WhatsappApiException;
import com.whatsapp.api.utils.proxy.CustomHttpProxySelector;
import com.whatsapp.api.utils.proxy.CustomProxyAuthenticator;

import okhttp3.Authenticator;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class WhatsappApiServiceGeneratorTest extends TestUtils {

@BeforeEach
void resetProxy() {
WhatsappApiServiceGenerator.sharedClient = WhatsappApiServiceGenerator.createDefaultHttpClient();
}

/**
* Method under test:
* {@link WhatsappApiServiceGenerator#getSharedClient}
*/
@Test
void testGetSharedClient() {

assertNotNull(WhatsappApiServiceGenerator.getSharedClient(), "Shared client should not be null");
assertEquals(WhatsappApiServiceGenerator.getSharedClient().getClass(), OkHttpClient.class, "Shared client should be OkHttpClient");

}

/**
* Method under test:
* {@link WhatsappApiServiceGenerator#getWhatsappApiError}
*
* @throws IOException
* @throws URISyntaxException
* @throws WhatsappApiException
*/
@Test
void testGetWhatsappApiError() throws IOException, URISyntaxException {

String verifyCodeErrorBody = fromResource("/phone/verifyCodeError.json");

Response<?> response = Response.error(400, ResponseBody.create(verifyCodeErrorBody, MediaType.parse("application/json")));
WhatsappApiError apiError = WhatsappApiServiceGenerator.getWhatsappApiError(response);

assertEquals(136025, apiError.error().code(), "Error code should be 136025");
assertEquals(2388093, apiError.error().errorSubcode(), "Error code should be 136025");
assertEquals(false, apiError.error().isTransient(), "Error code should be 136025");
assertEquals("O c\u00F3digo inserido est\u00E1 incorreto.", apiError.error().errorUserMsg(), "Error code should be 136025");
assertEquals("N\u00E3o foi poss\u00EDvel verificar o c\u00F3digo", apiError.error().errorUserSubtitle(), "Error code should be 136025");

}

/**
* Method under test:
* {@link WhatsappApiServiceGenerator#setHttpProxy(String, int, String, String)}
*/
@Test
void testSetHttpProxy_WithoutAuthentication() {

// Pre-condition Proxy
assertNull(WhatsappApiServiceGenerator.getSharedClient().proxy(), "Proxy should be null");
assertEquals(ProxySelector.getDefault(), WhatsappApiServiceGenerator.getSharedClient().proxySelector(),
"Proxy selector should be null");

// Set proxy in shared client
WhatsappApiServiceGenerator.setHttpProxy("localhost", 8080, null, null);

// Check if proxy is set
assertNotNull(WhatsappApiServiceGenerator.getSharedClient().proxySelector(), "Proxy selector should not be null");
assertEquals(WhatsappApiServiceGenerator.getSharedClient().proxySelector().getClass(), CustomHttpProxySelector.class, "Proxy selector should be CustomHttpProxySelector");

// Check if authenticator is NONE
assertEquals(Authenticator.NONE, WhatsappApiServiceGenerator.getSharedClient().authenticator(), "Authenticator should be NONE");
}

/**
* Method under test:
* {@link WhatsappApiServiceGenerator#setHttpProxy(String, int, String, String)}
*/
@Test
void testSetHttpProxy_WithAuthentication() {

// Pre-condition Proxy
assertNull(WhatsappApiServiceGenerator.getSharedClient().proxy(), "Proxy should be null");
assertEquals(ProxySelector.getDefault(), WhatsappApiServiceGenerator.getSharedClient().proxySelector(),
"Proxy selector should be ProxySelector.getDefault()");

// Pre-condition Authenticator
assertEquals(Authenticator.NONE, WhatsappApiServiceGenerator.getSharedClient().authenticator(), "Authenticator should be NONE");
assertEquals(Authenticator.NONE, WhatsappApiServiceGenerator.getSharedClient().proxyAuthenticator(), "Authenticator should be NONE");

// Set proxy with Authentication in shared client
WhatsappApiServiceGenerator.setHttpProxy("localhost", 8080, "Proxy-User", "Proxy-Pwd");

// Check if proxy is set
assertNotNull(WhatsappApiServiceGenerator.getSharedClient().proxySelector(), "Proxy selector should not be null");
assertEquals(WhatsappApiServiceGenerator.getSharedClient().proxySelector().getClass(), CustomHttpProxySelector.class, "Proxy selector should be CustomHttpProxySelector");

// Check if authenticator is CustomProxyAuthenticator
assertNotNull(WhatsappApiServiceGenerator.getSharedClient().proxyAuthenticator(), "Proxy Authenticator should not be null");
assertEquals(CustomProxyAuthenticator.class, WhatsappApiServiceGenerator.getSharedClient().proxyAuthenticator().getClass(), "Authenticator should be CustomProxyAuthenticator");

}
}

0 comments on commit 1e00dbb

Please sign in to comment.