Skip to content

Commit

Permalink
Add unit tests for resource operations
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliouzbett committed Mar 8, 2024
1 parent 15130b0 commit 2debfdc
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ozonehis.camel.frappe.sdk.api.security.FrappeAuthentication;
import com.ozonehis.camel.frappe.sdk.api.transformer.Transformer;
import com.ozonehis.camel.frappe.sdk.internal.security.FrappeBasicAuthentication;
import com.ozonehis.camel.frappe.sdk.internal.security.DefaultFrappeAuthentication;
import com.ozonehis.camel.frappe.sdk.internal.transformer.JacksonTransformer;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -37,7 +37,7 @@ public static FrappeClientBuilder newClient(String baseApiUrl, FrappeAuthenticat
}

public static FrappeClientBuilder newClient(String baseApiUrl, String username, String password) {
return new FrappeClientBuilder(baseApiUrl, new FrappeBasicAuthentication(username, password));
return new FrappeClientBuilder(baseApiUrl, new DefaultFrappeAuthentication(username, password));
}

public FrappeClientBuilder maxIdleConnections(int maxIdleConnections) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public interface FrappeResponse extends Closeable {
*/
int code();

/**
* This method returns the response message.
* @return The response message.
*/
String message();

/**
* This method returns the response body.
* @return The response body.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public int code() {
return response.code();
}

@Override
public String message() {
return response.message();
}

@Override
public ResponseBody responseBody() {
return response.body();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public ParameterizedOperation<T> withParameters(Map<String, String> parameters)

@Override
public ParameterizedOperation<T> withParameter(String name, String value) {
List<String> values = queryParams.computeIfAbsent(name, k -> new ArrayList<>());
values.add(value);
this.queryParams.put(name, values);
this.queryParams.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.ozonehis.camel.frappe.sdk.api.operation.DeleteOperation;
import com.ozonehis.camel.frappe.sdk.api.transformer.Transformer;
import com.ozonehis.camel.frappe.sdk.internal.DefaultFrappeResponse;
import lombok.Getter;
import okhttp3.OkHttpClient;
import okhttp3.Request;

@Getter
public class DefaultDeleteOperation extends AbstractResourceOperation implements DeleteOperation {

private String nameOfResourceToBeDeleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.ozonehis.camel.frappe.sdk.api.operation.PutOperation;
import com.ozonehis.camel.frappe.sdk.api.transformer.Transformer;
import com.ozonehis.camel.frappe.sdk.internal.DefaultFrappeResponse;
import lombok.Getter;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

@Getter
public class DefaultPutOperation extends AbstractResourceOperation implements PutOperation {

private String nameOfResourceToBeUpdated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import org.jetbrains.annotations.NotNull;

@Slf4j
public class FrappeBasicAuthentication implements FrappeAuthentication {
public class DefaultFrappeAuthentication implements FrappeAuthentication {

private final String username;

private final String password;

public FrappeBasicAuthentication(String username, String password) {
public DefaultFrappeAuthentication(String username, String password) {
this.username = username;
this.password = password;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ private Response executeRequest(Request request) throws IOException {

private List<String> extractCookies(Response response) {
if (response.isSuccessful()) {
log.info("Logged in successfully");
log.info("Authentication successful setting session cookie.");
return response.headers("set-cookie");
} else {
log.error("Failed to login, {}", response.body());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public List<Cookie> loadForRequest(@NotNull HttpUrl httpUrl) {

@Override
public void saveFromResponse(@NotNull HttpUrl httpUrl, @NotNull List<Cookie> cookies) {
this.cookiesCache.removeAll(cookies.stream().map(WrappedCookie::new).toList());
this.cookiesCache.addAll(cookies.stream().map(WrappedCookie::new).toList());
List<WrappedCookie> wrappedCookies =
cookies.stream().map(WrappedCookie::new).toList();
this.cookiesCache.removeAll(wrappedCookies);
this.cookiesCache.addAll(wrappedCookies);
}

@Synchronized
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.ozonehis.camel.frappe.sdk.internal.operation;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import com.ozonehis.camel.frappe.sdk.api.FrappeResponse;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

class DefaultDeleteOperationTest {

@Mock
private okhttp3.OkHttpClient httpClient;

@Mock
private okhttp3.Call call;

private DefaultDeleteOperation defaultDeleteOperation;

private static AutoCloseable mocksCloser;

@BeforeEach
void setUp() {
mocksCloser = org.mockito.MockitoAnnotations.openMocks(this);
defaultDeleteOperation = new DefaultDeleteOperation("http://localhost", "doctype", httpClient, null);
}

@AfterAll
static void closeMocks() throws Exception {
mocksCloser.close();
}

@Test
@DisplayName("doResourceExecute should return FrappeResponse when request is successful")
void doResourceExecuteShouldReturnFrappeResponseWhenRequestIsSuccessful() throws Exception {
byte[] resourceAsBytes = "test".getBytes();
Request.Builder requestBuilder = new Request.Builder().url("http://localhost");

when(httpClient.newCall(Mockito.any(Request.class))).thenReturn(call);
Response.Builder responseBuilder = new Response.Builder();
responseBuilder.code(200);
responseBuilder.message("Deleted");
responseBuilder.request(new Request.Builder().url("http://localhost").build());
responseBuilder.body(okhttp3.ResponseBody.create("{}", okhttp3.MediaType.parse("application/json")));
responseBuilder.protocol(okhttp3.Protocol.HTTP_1_1);
when(call.execute()).thenReturn(responseBuilder.build());

FrappeResponse response = defaultDeleteOperation.doResourceExecute(resourceAsBytes, requestBuilder);

assertEquals(200, response.code());
assertEquals("Deleted", response.message());
}

@Test
@DisplayName("withName should set the name of the resource to be deleted")
void withNameShouldSetTheNameOfTheResourceToBeUpdated() {
String resourceName = "testResource";
defaultDeleteOperation.withName(resourceName);
assertEquals(resourceName, defaultDeleteOperation.getNameOfResourceToBeDeleted());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.ozonehis.camel.frappe.sdk.internal.operation;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.ozonehis.camel.frappe.sdk.api.FrappeClientException;
import com.ozonehis.camel.frappe.sdk.api.FrappeResponse;
import java.io.IOException;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class DefaultPostOperationTest {

@Mock
private okhttp3.OkHttpClient httpClient;

@Mock
private okhttp3.Call call;

private DefaultPostOperation defaultPostOperation;

private static AutoCloseable mocksCloser;

@BeforeEach
void setUp() {
mocksCloser = org.mockito.MockitoAnnotations.openMocks(this);
defaultPostOperation = new DefaultPostOperation("http://localhost", "doctype", httpClient, null);
}

@AfterAll
static void closeMocks() throws Exception {
mocksCloser.close();
}

@Test
@DisplayName("doResourceExecute should return FrappeResponse when request is successful")
void doResourceExecuteShouldReturnFrappeResponseWhenRequestIsSuccessful() throws IOException {
byte[] resourceAsBytes = "test".getBytes();
Request.Builder requestBuilder = new Request.Builder().url("http://localhost");

when(httpClient.newCall(any(Request.class))).thenReturn(call);
Response.Builder responseBuilder = new Response.Builder();
responseBuilder.code(200);
responseBuilder.message("OK");
responseBuilder.request(new Request.Builder().url("http://localhost").build());
responseBuilder.body(okhttp3.ResponseBody.create("{}", okhttp3.MediaType.parse("application/json")));
responseBuilder.protocol(okhttp3.Protocol.HTTP_1_1);
when(call.execute()).thenReturn(responseBuilder.build());

FrappeResponse response = defaultPostOperation.doResourceExecute(resourceAsBytes, requestBuilder);

assertNotNull(response);
assertEquals(200, response.code());
assertEquals("OK", response.message());
}

@Test
@DisplayName("doResourceExecute should throw IOException when request fails")
void doResourceExecuteShouldThrowIOExceptionWhenRequestFails() throws IOException {
byte[] resourceAsBytes = "test".getBytes();
Request.Builder requestBuilder = new Request.Builder().url("http://localhost");

when(httpClient.newCall(any(Request.class))).thenReturn(call);
when(call.execute()).thenThrow(IOException.class);

assertThrows(
FrappeClientException.class,
() -> defaultPostOperation.doResourceExecute(resourceAsBytes, requestBuilder));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.ozonehis.camel.frappe.sdk.internal.operation;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.ozonehis.camel.frappe.sdk.api.FrappeClientException;
import com.ozonehis.camel.frappe.sdk.api.FrappeResponse;
import java.io.IOException;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class DefaultPutOperationTest {

@Mock
private okhttp3.OkHttpClient httpClient;

@Mock
private okhttp3.Call call;

private DefaultPutOperation defaultPutOperation;

private static AutoCloseable mocksCloser;

@BeforeEach
void setUp() {
mocksCloser = org.mockito.MockitoAnnotations.openMocks(this);
defaultPutOperation = new DefaultPutOperation("http://localhost", "doctype", httpClient, null);
}

@AfterAll
static void closeMocks() throws Exception {
mocksCloser.close();
}

@Test
@DisplayName("doResourceExecute should return FrappeResponse when request is successful")
void doResourceExecuteShouldReturnFrappeResponseWhenRequestIsSuccessful() throws IOException {
byte[] resourceAsBytes = "test".getBytes();
Request.Builder requestBuilder = new Request.Builder().url("http://localhost");

when(httpClient.newCall(any(Request.class))).thenReturn(call);
Response.Builder responseBuilder = new Response.Builder();
responseBuilder.code(204);
responseBuilder.message("Updated");
responseBuilder.request(new Request.Builder().url("http://localhost").build());
responseBuilder.body(okhttp3.ResponseBody.create("{}", okhttp3.MediaType.parse("application/json")));
responseBuilder.protocol(okhttp3.Protocol.HTTP_1_1);
when(call.execute()).thenReturn(responseBuilder.build());

FrappeResponse response = defaultPutOperation.doResourceExecute(resourceAsBytes, requestBuilder);

assertNotNull(response);
assertEquals(204, response.code());
assertEquals("Updated", response.message());
}

@Test
@DisplayName("doResourceExecute should throw IOException when request fails")
void doResourceExecuteShouldThrowIOExceptionWhenRequestFails() throws IOException {
byte[] resourceAsBytes = "test".getBytes();
Request.Builder requestBuilder = new Request.Builder().url("http://localhost");

when(httpClient.newCall(any(Request.class))).thenReturn(call);
when(call.execute()).thenThrow(IOException.class);

assertThrows(
FrappeClientException.class,
() -> defaultPutOperation.doResourceExecute(resourceAsBytes, requestBuilder));
}

@Test
@DisplayName("withName should set the name of the resource to be updated")
void withNameShouldSetTheNameOfTheResourceToBeUpdated() {
String resourceName = "testResource";
defaultPutOperation.withName(resourceName);
assertEquals(resourceName, defaultPutOperation.getNameOfResourceToBeUpdated());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class FrappeBasicAuthenticationTest {
class DefaultFrappeAuthenticationTest {

@Mock
private Request request;

private FrappeBasicAuthentication frappeBasicAuthentication;
private DefaultFrappeAuthentication defaultFrappeAuthentication;

private static AutoCloseable mocksCloser;

@BeforeEach
void setUp() {
mocksCloser = openMocks(this);
when(request.url()).thenReturn(HttpUrl.get("http://localhost:8080"));
frappeBasicAuthentication = new FrappeBasicAuthentication("username", "password");
defaultFrappeAuthentication = new DefaultFrappeAuthentication("username", "password");
}

@AfterAll
Expand All @@ -37,6 +37,6 @@ static void closeMocks() throws Exception {
@DisplayName("getBaseUrl should return correct base url")
void getBaseUrlShouldReturnCorrectBaseUrl() {
when(request.url()).thenReturn(HttpUrl.get("http://localhost:8080"));
assertEquals("http://localhost:8080", frappeBasicAuthentication.getBaseUrl(request));
assertEquals("http://localhost:8080", defaultFrappeAuthentication.getBaseUrl(request));
}
}

0 comments on commit 2debfdc

Please sign in to comment.