-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for resource operations
- Loading branch information
1 parent
15130b0
commit 2debfdc
Showing
12 changed files
with
260 additions
and
14 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
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
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
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
67 changes: 67 additions & 0 deletions
67
...est/java/com/ozonehis/camel/frappe/sdk/internal/operation/DefaultDeleteOperationTest.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,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()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
.../test/java/com/ozonehis/camel/frappe/sdk/internal/operation/DefaultPostOperationTest.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,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)); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...c/test/java/com/ozonehis/camel/frappe/sdk/internal/operation/DefaultPutOperationTest.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,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()); | ||
} | ||
} |
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