BAEL-1039: Introduction to Derive4J (#5845)
This commit is contained in:
parent
8e18c03c40
commit
22d22c7c4e
@ -717,6 +717,12 @@
|
|||||||
<version>${suanshu.version}</version>
|
<version>${suanshu.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.derive4j</groupId>
|
||||||
|
<artifactId>derive4j</artifactId>
|
||||||
|
<version>${derive4j.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
@ -952,6 +958,7 @@
|
|||||||
<fugue.version>4.5.1</fugue.version>
|
<fugue.version>4.5.1</fugue.version>
|
||||||
<maven-bundle-plugin.version>3.3.0</maven-bundle-plugin.version>
|
<maven-bundle-plugin.version>3.3.0</maven-bundle-plugin.version>
|
||||||
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
||||||
|
<derive4j.version>1.1.0</derive4j.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user