Fixed conflicts
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.derive4j.adt;
|
||||
|
||||
import org.derive4j.Data;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@Data
|
||||
interface Either<A,B>{
|
||||
<X> X match(Function<A, X> left, Function<B, X> right);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.derive4j.lazy;
|
||||
|
||||
import org.derive4j.Data;
|
||||
import org.derive4j.Derive;
|
||||
import org.derive4j.Make;
|
||||
|
||||
@Data(value = @Derive(
|
||||
inClass = "{ClassName}Impl",
|
||||
make = {Make.lazyConstructor, Make.constructors}
|
||||
))
|
||||
public interface LazyRequest {
|
||||
interface Cases<R>{
|
||||
R GET(String path);
|
||||
R POST(String path, String body);
|
||||
R PUT(String path, String body);
|
||||
R DELETE(String path);
|
||||
}
|
||||
|
||||
<R> R match(LazyRequest.Cases<R> method);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.derive4j.pattern;
|
||||
|
||||
import org.derive4j.Data;
|
||||
|
||||
@Data
|
||||
interface HTTPRequest {
|
||||
interface Cases<R>{
|
||||
R GET(String path);
|
||||
R POST(String path, String body);
|
||||
R PUT(String path, String body);
|
||||
R DELETE(String path);
|
||||
}
|
||||
|
||||
<R> R match(Cases<R> method);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.derive4j.pattern;
|
||||
|
||||
public class HTTPResponse {
|
||||
private int statusCode;
|
||||
private String responseBody;
|
||||
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
public String getResponseBody() {
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
public HTTPResponse(int statusCode, String responseBody) {
|
||||
this.statusCode = statusCode;
|
||||
this.responseBody = responseBody;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.derive4j.pattern;
|
||||
|
||||
|
||||
public class HTTPServer {
|
||||
public static String GET_RESPONSE_BODY = "Success!";
|
||||
public static String PUT_RESPONSE_BODY = "Resource Created!";
|
||||
public static String POST_RESPONSE_BODY = "Resource Updated!";
|
||||
public static String DELETE_RESPONSE_BODY = "Resource Deleted!";
|
||||
|
||||
public HTTPResponse acceptRequest(HTTPRequest request) {
|
||||
return HTTPRequests.caseOf(request)
|
||||
.GET((path) -> new HTTPResponse(200, GET_RESPONSE_BODY))
|
||||
.POST((path,body) -> new HTTPResponse(201, POST_RESPONSE_BODY))
|
||||
.PUT((path,body) -> new HTTPResponse(200, PUT_RESPONSE_BODY))
|
||||
.DELETE(path -> new HTTPResponse(200, DELETE_RESPONSE_BODY));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.derive4j.adt;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EitherUnitTest {
|
||||
@Test
|
||||
public void testEitherIsCreatedFromRight() {
|
||||
Either<Exception, String> either = Eithers.right("Okay");
|
||||
Optional<Exception> leftOptional = Eithers.getLeft(either);
|
||||
Optional<String> rightOptional = Eithers.getRight(either);
|
||||
Assertions.assertThat(leftOptional).isEmpty();
|
||||
Assertions.assertThat(rightOptional).hasValue("Okay");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEitherIsMatchedWithRight() {
|
||||
Either<Exception, String> either = Eithers.right("Okay");
|
||||
Function<Exception, String> leftFunction = Mockito.mock(Function.class);
|
||||
Function<String, String> rightFunction = Mockito.mock(Function.class);
|
||||
either.match(leftFunction, rightFunction);
|
||||
Mockito.verify(rightFunction, Mockito.times(1)).apply("Okay");
|
||||
Mockito.verify(leftFunction, Mockito.times(0)).apply(Mockito.any(Exception.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.derive4j.lazy;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LazyRequestUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLazyContstructedRequest_whenRequestIsReferenced_thenRequestIsLazilyContructed() {
|
||||
LazyRequestSupplier mockSupplier = Mockito.spy(new LazyRequestSupplier());
|
||||
|
||||
LazyRequest request = LazyRequestImpl.lazy(() -> mockSupplier.get());
|
||||
Mockito.verify(mockSupplier, Mockito.times(0)).get();
|
||||
Assert.assertEquals(LazyRequestImpl.getPath(request), "http://test.com/get");
|
||||
Mockito.verify(mockSupplier, Mockito.times(1)).get();
|
||||
|
||||
}
|
||||
|
||||
class LazyRequestSupplier implements Supplier<LazyRequest> {
|
||||
@Override
|
||||
public LazyRequest get() {
|
||||
return LazyRequestImpl.GET("http://test.com/get");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.derive4j.pattern;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HTTPRequestUnitTest {
|
||||
public static HTTPServer server;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
server = new HTTPServer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpGETRequest_whenRequestReachesServer_thenProperResponseIsReturned() {
|
||||
HTTPRequest postRequest = HTTPRequests.POST("http://test.com/post", "Resource");
|
||||
HTTPResponse response = server.acceptRequest(postRequest);
|
||||
Assert.assertEquals(201, response.getStatusCode());
|
||||
Assert.assertEquals(HTTPServer.POST_RESPONSE_BODY, response.getResponseBody());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.baeldung.fj;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -30,7 +32,7 @@ public class FunctionalJavaUnitTest {
|
||||
List<Integer> fList1 = fList.map(timesTwo);
|
||||
List<Integer> fList2 = fList.map(i -> i * 2);
|
||||
|
||||
assertEquals(fList1.equals(fList2), true);
|
||||
assertTrue(fList1.equals(fList2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -41,7 +43,7 @@ public class FunctionalJavaUnitTest {
|
||||
List<Integer> fList2 = fList.map(plusOne).map(timesTwo);
|
||||
Show.listShow(Show.intShow).println(fList2);
|
||||
|
||||
assertEquals(fList1.equals(fList2), false);
|
||||
assertFalse(fList1.equals(fList2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -49,10 +51,8 @@ public class FunctionalJavaUnitTest {
|
||||
List<Integer> fList = List.list(3, 4, 5, 6);
|
||||
List<Boolean> evenList = fList.map(isEven);
|
||||
List<Boolean> evenListTrueResult = List.list(false, true, false, true);
|
||||
List<Boolean> evenListFalseResult = List.list(true, false, false, true);
|
||||
|
||||
assertEquals(evenList.equals(evenListTrueResult), true);
|
||||
assertEquals(evenList.equals(evenListFalseResult), false);
|
||||
assertTrue(evenList.equals(evenListTrueResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,10 +60,8 @@ public class FunctionalJavaUnitTest {
|
||||
List<Integer> fList = List.list(3, 4, 5, 6);
|
||||
fList = fList.map(i -> i + 100);
|
||||
List<Integer> resultList = List.list(103, 104, 105, 106);
|
||||
List<Integer> falseResultList = List.list(15, 504, 105, 106);
|
||||
|
||||
assertEquals(fList.equals(resultList), true);
|
||||
assertEquals(fList.equals(falseResultList), false);
|
||||
assertTrue(fList.equals(resultList));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -71,23 +69,19 @@ public class FunctionalJavaUnitTest {
|
||||
Array<Integer> array = Array.array(3, 4, 5, 6);
|
||||
Array<Integer> filteredArray = array.filter(isEven);
|
||||
Array<Integer> result = Array.array(4, 6);
|
||||
Array<Integer> wrongResult = Array.array(3, 5);
|
||||
|
||||
assertEquals(filteredArray.equals(result), true);
|
||||
assertEquals(filteredArray.equals(wrongResult), false);
|
||||
assertTrue(filteredArray.equals(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForLowerCase_givenStringArray_returnResult() {
|
||||
Array<String> array = Array.array("Welcome", "To", "baeldung");
|
||||
assertTrue(array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)));
|
||||
|
||||
Array<String> array2 = Array.array("Welcome", "To", "Baeldung");
|
||||
Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
|
||||
Boolean isExist2 = array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
|
||||
Boolean isAll = array.forall(s -> List.fromString(s).forall(Characters.isLowerCase));
|
||||
|
||||
assertEquals(isExist, true);
|
||||
assertEquals(isExist2, false);
|
||||
assertEquals(isAll, false);
|
||||
assertFalse(array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase)));
|
||||
|
||||
assertFalse(array.forall(s -> List.fromString(s).forall(Characters.isLowerCase)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -102,9 +96,9 @@ public class FunctionalJavaUnitTest {
|
||||
Option<Integer> result2 = n2.bind(function);
|
||||
Option<Integer> result3 = n3.bind(function);
|
||||
|
||||
assertEquals(result1, Option.none());
|
||||
assertEquals(result2, Option.some(102));
|
||||
assertEquals(result3, Option.none());
|
||||
assertEquals(Option.none(), result1);
|
||||
assertEquals(Option.some(102), result2);
|
||||
assertEquals(Option.none(), result3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -112,11 +106,11 @@ public class FunctionalJavaUnitTest {
|
||||
Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27);
|
||||
int sumAll = intArray.foldLeft(Integers.add, 0);
|
||||
|
||||
assertEquals(sumAll, 260);
|
||||
assertEquals(260, sumAll);
|
||||
|
||||
int sumEven = intArray.filter(isEven).foldLeft(Integers.add, 0);
|
||||
|
||||
assertEquals(sumEven, 148);
|
||||
assertEquals(148, sumEven);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user