Skip to content

Commit 22d22c7

Browse files
Wosinpivovarit
authored andcommitted
BAEL-1039: Introduction to Derive4J (eugenp#5845)
1 parent 8e18c03 commit 22d22c7

File tree

9 files changed

+172
-0
lines changed

9 files changed

+172
-0
lines changed

libraries/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@
717717
<version>${suanshu.version}</version>
718718
</dependency>
719719

720+
<dependency>
721+
<groupId>org.derive4j</groupId>
722+
<artifactId>derive4j</artifactId>
723+
<version>${derive4j.version}</version>
724+
<optional>true</optional>
725+
</dependency>
720726
</dependencies>
721727

722728
<repositories>
@@ -952,6 +958,7 @@
952958
<fugue.version>4.5.1</fugue.version>
953959
<maven-bundle-plugin.version>3.3.0</maven-bundle-plugin.version>
954960
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
961+
<derive4j.version>1.1.0</derive4j.version>
955962
</properties>
956963

957964
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.derive4j.adt;
2+
3+
import org.derive4j.Data;
4+
5+
import java.util.function.Function;
6+
7+
@Data
8+
interface Either<A,B>{
9+
<X> X match(Function<A, X> left, Function<B, X> right);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.derive4j.lazy;
2+
3+
import org.derive4j.Data;
4+
import org.derive4j.Derive;
5+
import org.derive4j.Make;
6+
7+
@Data(value = @Derive(
8+
inClass = "{ClassName}Impl",
9+
make = {Make.lazyConstructor, Make.constructors}
10+
))
11+
public interface LazyRequest {
12+
interface Cases<R>{
13+
R GET(String path);
14+
R POST(String path, String body);
15+
R PUT(String path, String body);
16+
R DELETE(String path);
17+
}
18+
19+
<R> R match(LazyRequest.Cases<R> method);
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.derive4j.pattern;
2+
3+
import org.derive4j.Data;
4+
5+
@Data
6+
interface HTTPRequest {
7+
interface Cases<R>{
8+
R GET(String path);
9+
R POST(String path, String body);
10+
R PUT(String path, String body);
11+
R DELETE(String path);
12+
}
13+
14+
<R> R match(Cases<R> method);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.derive4j.pattern;
2+
3+
public class HTTPResponse {
4+
private int statusCode;
5+
private String responseBody;
6+
7+
public int getStatusCode() {
8+
return statusCode;
9+
}
10+
11+
public String getResponseBody() {
12+
return responseBody;
13+
}
14+
15+
public HTTPResponse(int statusCode, String responseBody) {
16+
this.statusCode = statusCode;
17+
this.responseBody = responseBody;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.derive4j.pattern;
2+
3+
4+
public class HTTPServer {
5+
public static String GET_RESPONSE_BODY = "Success!";
6+
public static String PUT_RESPONSE_BODY = "Resource Created!";
7+
public static String POST_RESPONSE_BODY = "Resource Updated!";
8+
public static String DELETE_RESPONSE_BODY = "Resource Deleted!";
9+
10+
public HTTPResponse acceptRequest(HTTPRequest request) {
11+
return HTTPRequests.caseOf(request)
12+
.GET((path) -> new HTTPResponse(200, GET_RESPONSE_BODY))
13+
.POST((path,body) -> new HTTPResponse(201, POST_RESPONSE_BODY))
14+
.PUT((path,body) -> new HTTPResponse(200, PUT_RESPONSE_BODY))
15+
.DELETE(path -> new HTTPResponse(200, DELETE_RESPONSE_BODY));
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.derive4j.adt;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.mockito.Mockito;
7+
import org.mockito.junit.MockitoJUnitRunner;
8+
9+
import java.util.Optional;
10+
import java.util.function.Function;
11+
@RunWith(MockitoJUnitRunner.class)
12+
public class EitherUnitTest {
13+
@Test
14+
public void testEitherIsCreatedFromRight() {
15+
Either<Exception, String> either = Eithers.right("Okay");
16+
Optional<Exception> leftOptional = Eithers.getLeft(either);
17+
Optional<String> rightOptional = Eithers.getRight(either);
18+
Assertions.assertThat(leftOptional).isEmpty();
19+
Assertions.assertThat(rightOptional).hasValue("Okay");
20+
21+
}
22+
23+
@Test
24+
public void testEitherIsMatchedWithRight() {
25+
Either<Exception, String> either = Eithers.right("Okay");
26+
Function<Exception, String> leftFunction = Mockito.mock(Function.class);
27+
Function<String, String> rightFunction = Mockito.mock(Function.class);
28+
either.match(leftFunction, rightFunction);
29+
Mockito.verify(rightFunction, Mockito.times(1)).apply("Okay");
30+
Mockito.verify(leftFunction, Mockito.times(0)).apply(Mockito.any(Exception.class));
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.derive4j.lazy;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.mockito.Mockito;
6+
7+
import java.util.function.Supplier;
8+
9+
public class LazyRequestUnitTest {
10+
11+
@Test
12+
public void givenLazyContstructedRequest_whenRequestIsReferenced_thenRequestIsLazilyContructed() {
13+
LazyRequestSupplier mockSupplier = Mockito.spy(new LazyRequestSupplier());
14+
15+
LazyRequest request = LazyRequestImpl.lazy(() -> mockSupplier.get());
16+
Mockito.verify(mockSupplier, Mockito.times(0)).get();
17+
Assert.assertEquals(LazyRequestImpl.getPath(request), "http://test.com/get");
18+
Mockito.verify(mockSupplier, Mockito.times(1)).get();
19+
20+
}
21+
22+
class LazyRequestSupplier implements Supplier<LazyRequest> {
23+
@Override
24+
public LazyRequest get() {
25+
return LazyRequestImpl.GET("http://test.com/get");
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.derive4j.pattern;
2+
3+
import org.junit.Assert;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
public class HTTPRequestUnitTest {
8+
public static HTTPServer server;
9+
10+
@BeforeClass
11+
public static void setUp() {
12+
server = new HTTPServer();
13+
}
14+
15+
@Test
16+
public void givenHttpGETRequest_whenRequestReachesServer_thenProperResponseIsReturned() {
17+
HTTPRequest postRequest = HTTPRequests.POST("http://test.com/post", "Resource");
18+
HTTPResponse response = server.acceptRequest(postRequest);
19+
Assert.assertEquals(201, response.getStatusCode());
20+
Assert.assertEquals(HTTPServer.POST_RESPONSE_BODY, response.getResponseBody());
21+
}
22+
}

0 commit comments

Comments
 (0)