Skip to content

Commit

Permalink
add mockito inline, rewrite test to junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailNavitski committed Apr 20, 2024
1 parent 51acf20 commit 575d6fd
Show file tree
Hide file tree
Showing 26 changed files with 645 additions and 594 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)

## Unreleased ([details][unreleased changes details])

## Added

- 3327 - add Mockito inline dependency

6.6.0 - 2024-04-15

## Added
Expand Down
5 changes: 5 additions & 0 deletions bundle-cloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ Fragment-Host: com.adobe.acs.acs-aem-commons-bundle
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.osgi-mock.junit4</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit-addons</groupId>
<artifactId>junit-addons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public static BufferedImage readRGBImageFromInvertedYCCK(InputStream in, ICC_Pro
* @param cmykProfile An ICC_Profile for conversion from the CMYK color space
* to the RGB color space. If this parameter is null, a default profile is used.
* @return a BufferedImage in the RGB color space.
* @throws NullPointerException.
* @throws NullPointerException
*/
public static BufferedImage createRGBImageFromYCCK(Raster ycckRaster, ICC_Profile cmykProfile) {
BufferedImage image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package com.adobe.acs.commons.adobeio.service.impl;

import com.adobe.acs.commons.adobeio.service.IntegrationService;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.ProtocolVersion;
Expand All @@ -33,29 +36,27 @@
import org.apache.http.util.EntityUtils;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.skyscreamer.jsonassert.JSONAssert;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
public class EndpointServiceImplTest {
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class EndpointServiceImplTest {

private final AemContext context = new AemContext();

@Mock
private IntegrationService integrationService;
Expand All @@ -64,7 +65,7 @@ public class EndpointServiceImplTest {
private AdobeioHelper helper;

@InjectMocks
private EndpointServiceImpl endpointService = new EndpointServiceImpl();
private EndpointServiceImpl endpointService;

@Mock
private CloseableHttpClient httpClient;
Expand All @@ -75,24 +76,18 @@ public class EndpointServiceImplTest {
@Mock
private CloseableHttpResponse response;

@Before
@BeforeEach
public void setup() throws Exception {
when(config.id()).thenReturn("test");
when(config.endpoint()).thenReturn("https://test.com");
when(integrationService.getTimeoutinMilliSeconds()).thenReturn(60000);
when(integrationService.getAccessToken()).thenReturn("ACCESS_TOKEN");
when(integrationService.getApiKey()).thenReturn("API_KEY");
when(helper.getHttpClient(60000)).thenReturn(httpClient);
when(httpClient.execute(any())).thenReturn(response);
when(response.getEntity()).thenReturn(new StringEntity("{'result':'ok'}"));
when(response.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "OK"));
context.registerService(AdobeioHelper.class, helper);
context.registerService(CloseableHttpClient.class, httpClient);
context.registerService(IntegrationService.class, integrationService);
endpointService = context.registerInjectActivateService(new EndpointServiceImpl(), ImmutableMap.of("id", "test",
"endpoint", "https://test.com", "method", "GET"));
}

@Test
public void testGet() throws Exception {
when(config.method()).thenReturn("GET");
endpointService.activate(config);

void testGet() throws Exception {
prepareMock();
JsonObject result = endpointService.performIO_Action();
JSONAssert.assertEquals("{'result':'ok'}", result.toString(), false);

Expand All @@ -112,12 +107,11 @@ public void testGet() throws Exception {
}

@Test
public void testGetWithCustomContentType() throws Exception {
when(config.method()).thenReturn("GET");
endpointService.activate(config);
void testGetWithCustomContentType() throws Exception {
prepareMock();
String[] customHeaders = new String[1];
customHeaders[0] = "content-type:application/vnd.adobe.target.v1+json";
JsonObject result = endpointService.performIO_Action("https://test.com", "GET", customHeaders, null);
JsonObject result = endpointService.performIO_Action("https://test.com", "GET", customHeaders, null);
JSONAssert.assertEquals("{'result':'ok'}", result.toString(), false);

ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);
Expand All @@ -136,10 +130,8 @@ public void testGetWithCustomContentType() throws Exception {
}

@Test
public void testGetWithQueryParams() throws Exception {
when(config.method()).thenReturn("GET");
endpointService.activate(config);

void testGetWithQueryParams() throws Exception {
prepareMock();
Map<String, String> params = new HashMap<>();
params.put("foo", "bar");
params.put("some with", "spaces to check");
Expand All @@ -163,18 +155,18 @@ public void testGetWithQueryParams() throws Exception {
}

@Test
public void testGetWithServiceSpecificHeaders() throws Exception {
when(config.method()).thenReturn("GET");
when(config.specificServiceHeaders()).thenReturn(new String[] {
"",
":",
"foo:",
":bar",
"custom:header",
"custom:header1",
"custom2:header2"
});
endpointService.activate(config);
void testGetWithServiceSpecificHeaders() throws Exception {
prepareMock();
endpointService = context.registerInjectActivateService(new EndpointServiceImpl(), ImmutableMap.of("id", "test",
"endpoint", "https://test.com", "method", "GET", "specificServiceHeaders", new String[]{
"",
":",
"foo:",
":bar",
"custom:header",
"custom:header1",
"custom2:header2"
}));

JsonObject result = endpointService.performIO_Action();
JSONAssert.assertEquals("{'result':'ok'}", result.toString(), false);
Expand All @@ -197,12 +189,19 @@ public void testGetWithServiceSpecificHeaders() throws Exception {
}

@Test
public void testPostWithServiceSpecificHeaders() throws Exception {
when(config.method()).thenReturn("POST");
when(config.specificServiceHeaders()).thenReturn(new String[] {
"content-type:application/json;charset=UTF-8",
});
endpointService.activate(config);
void testPostWithServiceSpecificHeaders() throws Exception {
when(integrationService.getTimeoutinMilliSeconds()).thenReturn(60000);
when(integrationService.getAccessToken()).thenReturn("ACCESS_TOKEN");
when(integrationService.getApiKey()).thenReturn("API_KEY");
when(helper.getHttpClient(60000)).thenReturn(httpClient);
when(httpClient.execute(any())).thenReturn(response);
when(response.getEntity()).thenReturn(new StringEntity("{'result':'ok'}"));

endpointService = context.registerInjectActivateService(new EndpointServiceImpl(), ImmutableMap.of("id", "test",
"endpoint", "https://test.com", "method", "POST", "specificServiceHeaders", new String[]{
"content-type:application/json;charset=UTF-8",
}));

JsonObject payload = new JsonObject();
payload.addProperty("result", "My ‘Payload’");
JsonObject result = endpointService.performIO_Action(payload);
Expand All @@ -226,30 +225,30 @@ public void testPostWithServiceSpecificHeaders() throws Exception {
}

@Test
public void testConvertServiceSpecificHeadersWithNull() {
List<Map.Entry<String, String>> headers = endpointService.convertServiceSpecificHeaders(null);
assertEquals(Collections.EMPTY_LIST, headers);
void testConvertServiceSpecificHeadersWithNull() {
List<Map.Entry<String, String>> headers = endpointService.convertServiceSpecificHeaders(null);

assertEquals(Collections.EMPTY_LIST, headers);
}

@Test
public void testConvertServiceSpecificHeadersWitNothNull() {
String[] arr = {"name1:value","name2:value"};
List<Map.Entry<String, String>> headers = endpointService.convertServiceSpecificHeaders(arr);
void testConvertServiceSpecificHeadersWitNothNull() {
String[] arr = {"name1:value", "name2:value"};
List<Map.Entry<String, String>> headers = endpointService.convertServiceSpecificHeaders(arr);

assertEquals(2, headers.size());
assertEquals(2, headers.size());
}

@Test
public void testCharsetFrom() {
void testCharsetFrom() {
Charset iso88591 = endpointService.charsetFrom(new BasicHeader("Content-Type", "application/json;charset=iso-8859-1"));
assertEquals("ISO-8859-1", iso88591.name());

Charset utf8 = endpointService.charsetFrom(new BasicHeader("Content-Type", "application/json;charset=UTF-8"));
assertEquals("UTF-8", utf8.name());

assertEquals(null, endpointService.charsetFrom(null));
assertEquals(null, endpointService.charsetFrom(new BasicHeader("Content-Type", "application/json;charset=invalid")));
assertNull(endpointService.charsetFrom(null));
assertNull(endpointService.charsetFrom(new BasicHeader("Content-Type", "application/json;charset=invalid")));
}

public static TypeSafeMatcher<HttpUriRequest> hasUri(final String uri) {
Expand Down Expand Up @@ -332,4 +331,15 @@ public void describeMismatchSafely(HttpPost request, Description description) {
}
};
}

private void prepareMock() throws IOException {
when(integrationService.getTimeoutinMilliSeconds()).thenReturn(60000);
when(integrationService.getAccessToken()).thenReturn("ACCESS_TOKEN");
when(integrationService.getApiKey()).thenReturn("API_KEY");
when(helper.getHttpClient(60000)).thenReturn(httpClient);
when(httpClient.execute(any())).thenReturn(response);
when(response.getEntity()).thenReturn(new StringEntity("{'result':'ok'}"));
when(response.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "OK"));

}
}
Loading

0 comments on commit 575d6fd

Please sign in to comment.