-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from kaaproject/feature/KAA-661
KAA-661: improve Java unit tests
- Loading branch information
Showing
50 changed files
with
2,315 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...top/src/test/java/org/kaaproject/kaa/client/connectivity/PingConnectivityCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2014-2015 CyberVision, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.kaaproject.kaa.client.connectivity; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class PingConnectivityCheckerTest { | ||
|
||
@Test | ||
public void checkConnectivityToWrongHostTest() { | ||
PingConnectivityChecker pingConnectivityChecker = new PingConnectivityChecker("some-wrong-host=with-dashes-in-name"); | ||
Assert.assertFalse(pingConnectivityChecker.checkConnectivity()); | ||
} | ||
|
||
@Test | ||
public void checkConnectivityForLocalHostTest() { | ||
PingConnectivityChecker pingConnectivityChecker = new PingConnectivityChecker("localhost"); | ||
Assert.assertTrue(pingConnectivityChecker.checkConnectivity()); | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
...java-desktop/src/test/java/org/kaaproject/kaa/client/transport/DesktopHttpClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright 2014-2015 CyberVision, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.kaaproject.kaa.client.transport; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.util.LinkedHashMap; | ||
|
||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class DesktopHttpClientTest { | ||
private static final String URL = "http://some-url"; | ||
private static final String HTTP_CLIENT_FIELD_NAME = "httpClient"; | ||
private static final String HTTP_METHOD_FIELD_NAME = "method"; | ||
private static final int OK = 200; | ||
private static final int FAILURE = 400; | ||
private static PrivateKey privateKey; | ||
private static PublicKey publicKey; | ||
private static PublicKey remotePublicKey; | ||
private static LinkedHashMap<String, byte[]> entities = new LinkedHashMap<>(); | ||
private CloseableHttpResponse httpResponse; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
entities.put("entity1", new byte[]{1, 2, 3}); | ||
entities.put("entity2", new byte[]{53, 1}); | ||
privateKey = mock(PrivateKey.class); | ||
publicKey = mock(PublicKey.class); | ||
remotePublicKey = mock(PublicKey.class); | ||
when(privateKey.getEncoded()).thenReturn(new byte[]{1, 2, 3, 4, 5}); | ||
when(publicKey.getEncoded()).thenReturn(new byte[]{1, 2, 3, 4, 5, 10}); | ||
when(remotePublicKey.getEncoded()).thenReturn(new byte[]{5, 3, 2, 5, 6 , 3, 127}); | ||
} | ||
|
||
@Test(expected = TransportException.class) | ||
public void executeInvalidHttpRequestTest() throws Exception { | ||
DesktopHttpClient client = new DesktopHttpClient(URL, privateKey, publicKey, remotePublicKey); | ||
CloseableHttpClient httpClientMock = mockForHttpClient(FAILURE, true, null); | ||
ReflectionTestUtils.setField(client, HTTP_CLIENT_FIELD_NAME, httpClientMock); | ||
client.executeHttpRequest(URL, entities, false); | ||
verify(httpResponse).close(); | ||
} | ||
|
||
@Test | ||
public void executeValidHttpRequest() throws Exception { | ||
byte[] inputData = new byte[]{100, 101, 102}; | ||
DesktopHttpClient client = new DesktopHttpClient(URL, privateKey, publicKey, remotePublicKey); | ||
CloseableHttpClient httpClientMock = mockForHttpClient(OK, true, inputData); | ||
ReflectionTestUtils.setField(client, HTTP_CLIENT_FIELD_NAME, httpClientMock); | ||
byte[] body = client.executeHttpRequest(URL, entities, false); | ||
Assert.assertArrayEquals(inputData, body); | ||
verify(httpResponse).close(); | ||
} | ||
|
||
@Test | ||
public void canAbortTest() throws Throwable { | ||
DesktopHttpClient client = new DesktopHttpClient(URL, privateKey, publicKey, remotePublicKey); | ||
ReflectionTestUtils.setField(client, HTTP_METHOD_FIELD_NAME, null); | ||
Assert.assertFalse(client.canAbort()); | ||
HttpPost method = new HttpPost(); | ||
method.abort(); | ||
ReflectionTestUtils.setField(client, HTTP_METHOD_FIELD_NAME, method); | ||
Assert.assertFalse(client.canAbort()); | ||
method = new HttpPost(); | ||
ReflectionTestUtils.setField(client, HTTP_METHOD_FIELD_NAME, method); | ||
Assert.assertTrue(client.canAbort()); | ||
} | ||
|
||
@Test(expected = IOException.class) | ||
public void executeValidHttpRequestWithNoResponseEntityTest() throws Exception { | ||
DesktopHttpClient client = new DesktopHttpClient(URL, privateKey, publicKey, remotePublicKey); | ||
CloseableHttpClient httpClientMock = mockForHttpClient(OK, false, null); | ||
ReflectionTestUtils.setField(client, HTTP_CLIENT_FIELD_NAME, httpClientMock); | ||
client.executeHttpRequest(URL, entities, false); | ||
verify(httpResponse).close(); | ||
} | ||
|
||
@Test | ||
public void closeTest() throws IOException { | ||
DesktopHttpClient client = new DesktopHttpClient(URL, privateKey, publicKey, remotePublicKey); | ||
CloseableHttpClient httpClientMock = mockForHttpClient(OK, false, null); | ||
ReflectionTestUtils.setField(client, HTTP_CLIENT_FIELD_NAME, httpClientMock); | ||
client.close(); | ||
verify(httpClientMock).close(); | ||
} | ||
|
||
@Test | ||
public void abortTest() throws IOException { | ||
DesktopHttpClient client = new DesktopHttpClient(URL, privateKey, publicKey, remotePublicKey); | ||
HttpPost method = mock(HttpPost.class); | ||
when(method.isAborted()).thenReturn(false); | ||
ReflectionTestUtils.setField(client, HTTP_METHOD_FIELD_NAME, method); | ||
client.abort(); | ||
verify(method).abort(); | ||
} | ||
|
||
private CloseableHttpClient mockForHttpClient(int status, boolean hasEntity, byte[] body) throws IOException { | ||
CloseableHttpClient httpClientMock = mock(CloseableHttpClient.class); | ||
httpResponse = mock(CloseableHttpResponse.class, RETURNS_DEEP_STUBS); | ||
doReturn(httpResponse).when(httpClientMock).execute(any(HttpPost.class)); | ||
if (hasEntity) { | ||
HttpEntity entity = mock(HttpEntity.class); | ||
doReturn(entity).when(httpResponse).getEntity(); | ||
if (body != null) { | ||
doReturn(new ByteArrayInputStream(body)).when(entity).getContent(); | ||
} | ||
} else { | ||
doReturn(null).when(httpResponse).getEntity(); | ||
} | ||
when(httpResponse.getStatusLine().getStatusCode()).thenReturn(status); | ||
return httpClientMock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.