forked from eugenp/tutorials
-
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.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
19 changed files
with
888 additions
and
0 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
82 changes: 82 additions & 0 deletions
82
libraries-5/src/main/java/com/baeldung/activej/config/PersonModule.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,82 @@ | ||
package com.baeldung.activej.config; | ||
|
||
import com.baeldung.activej.controller.PersonController; | ||
import com.baeldung.activej.repository.PersonRepository; | ||
import com.baeldung.activej.service.PersonService; | ||
import io.activej.inject.annotation.Provides; | ||
import io.activej.inject.module.AbstractModule; | ||
|
||
import javax.sql.DataSource; | ||
import java.io.PrintWriter; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.sql.SQLFeatureNotSupportedException; | ||
import java.util.logging.Logger; | ||
|
||
public class PersonModule extends AbstractModule { | ||
|
||
@Provides | ||
PersonController personController(PersonService personService) { | ||
return new PersonController(personService); | ||
} | ||
|
||
@Provides | ||
PersonService personService(PersonRepository personRepository) { | ||
return new PersonService(personRepository); | ||
} | ||
|
||
@Provides | ||
PersonRepository personRepository(DataSource dataSource) { | ||
return new PersonRepository(dataSource); | ||
} | ||
|
||
@Provides | ||
DataSource dataSource() { | ||
return new DataSource() { | ||
@Override | ||
public Connection getConnection() throws SQLException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Connection getConnection(String username, String password) throws SQLException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public PrintWriter getLogWriter() throws SQLException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void setLogWriter(PrintWriter out) throws SQLException { | ||
|
||
} | ||
|
||
@Override | ||
public void setLoginTimeout(int seconds) throws SQLException { | ||
|
||
} | ||
|
||
@Override | ||
public int getLoginTimeout() throws SQLException { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public <T> T unwrap(Class<T> iface) throws SQLException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isWrapperFor(Class<?> iface) throws SQLException { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Logger getParentLogger() throws SQLFeatureNotSupportedException { | ||
return null; | ||
} | ||
}; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
libraries-5/src/main/java/com/baeldung/activej/controller/PersonController.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,25 @@ | ||
package com.baeldung.activej.controller; | ||
|
||
import com.baeldung.activej.service.PersonService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.activej.http.AsyncServlet; | ||
import io.activej.http.HttpRequest; | ||
import io.activej.http.HttpResponse; | ||
import io.activej.promise.Promise; | ||
|
||
public class PersonController implements AsyncServlet { | ||
private final PersonService personService; | ||
private final ObjectMapper objectMapper; | ||
|
||
public PersonController(PersonService personService) { | ||
this.personService = personService; | ||
this.objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Override | ||
public Promise<HttpResponse> serve(HttpRequest httpRequest) { | ||
return personService.findAndVerifyPerson(httpRequest.getQueryParameter("name")) | ||
.map((p) -> HttpResponse.ok200().withJson(objectMapper.writeValueAsString(p)).build()) | ||
.mapException(e -> e); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
libraries-5/src/main/java/com/baeldung/activej/model/Person.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,4 @@ | ||
package com.baeldung.activej.model; | ||
|
||
public record Person(String name, String description) { | ||
} |
5 changes: 5 additions & 0 deletions
5
libraries-5/src/main/java/com/baeldung/activej/model/VerifiedPerson.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,5 @@ | ||
package com.baeldung.activej.model; | ||
|
||
public record VerifiedPerson(String name, String description, | ||
String notes, String result) { | ||
} |
25 changes: 25 additions & 0 deletions
25
libraries-5/src/main/java/com/baeldung/activej/repository/PersonRepository.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,25 @@ | ||
package com.baeldung.activej.repository; | ||
|
||
import com.baeldung.activej.model.Person; | ||
import io.activej.promise.Promise; | ||
import io.activej.promise.Promises; | ||
|
||
import javax.sql.DataSource; | ||
import java.time.Duration; | ||
|
||
public class PersonRepository { | ||
private final DataSource dataSource; | ||
|
||
public PersonRepository(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public DataSource getDataSource() { | ||
return dataSource; | ||
} | ||
|
||
public Promise<Person> findPerson(String name) { | ||
return Promises | ||
.delay(Duration.ofMillis(100), new Person(name, name + " description")); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
libraries-5/src/main/java/com/baeldung/activej/service/PersonService.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,38 @@ | ||
package com.baeldung.activej.service; | ||
|
||
import com.baeldung.activej.model.VerifiedPerson; | ||
import com.baeldung.activej.repository.PersonRepository; | ||
import io.activej.promise.Promise; | ||
|
||
public class PersonService { | ||
private final PersonRepository personRepository; | ||
|
||
public PersonService(PersonRepository personRepository) { | ||
this.personRepository = personRepository; | ||
} | ||
|
||
public PersonRepository getPersonRepository() { | ||
return personRepository; | ||
} | ||
|
||
public Promise<VerifiedPerson> findAndVerifyPerson(String name) { | ||
return personRepository.findPerson(name) | ||
.combine(findPersonNotes(name), | ||
(person, notes) -> new VerifiedPerson(person.name(), person.description(), notes, null)) | ||
.map(person -> verify(person)); | ||
} | ||
|
||
private VerifiedPerson verify(VerifiedPerson person) { | ||
if(person.description().startsWith("Good")) { | ||
return new VerifiedPerson(person.name(), person.description(), | ||
person.notes(), "SUCCESS"); | ||
} | ||
|
||
return new VerifiedPerson(person.name(), person.description(), | ||
person.notes(), "FAIL"); | ||
} | ||
|
||
private Promise<String> findPersonNotes(String name) { | ||
return Promise.of(name + " notes"); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
libraries-5/src/test/java/com/baeldung/activej/ActiveJIntegrationTest.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,81 @@ | ||
package com.baeldung.activej; | ||
|
||
import com.baeldung.activej.config.PersonModule; | ||
import com.baeldung.activej.controller.PersonController; | ||
import com.baeldung.activej.model.VerifiedPerson; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.activej.dns.DnsClient; | ||
import io.activej.eventloop.Eventloop; | ||
import io.activej.http.*; | ||
import io.activej.inject.Injector; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.InetAddress; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ActiveJIntegrationTest { | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
private static HttpServer server; | ||
private static HttpClient client; | ||
private static int port; | ||
private static Eventloop eventloop; | ||
|
||
@BeforeAll | ||
static void setUp() throws Exception { | ||
eventloop = Eventloop.create(); | ||
|
||
PersonModule personModule = new PersonModule(); | ||
PersonController personController = Injector.of(personModule).getInstance(PersonController.class); | ||
|
||
RoutingServlet servlet = RoutingServlet.builder(eventloop) | ||
.with(HttpMethod.GET,"/person", personController) | ||
.build(); | ||
|
||
server = HttpServer.builder(eventloop, servlet) | ||
.withListenPort(8080) | ||
.build(); | ||
|
||
server.listen(); | ||
|
||
port = server.getListenAddresses().get(0).getPort(); | ||
|
||
InetAddress dnsServerAddress = InetAddress.getByName("8.8.8.8"); | ||
DnsClient dnsClient = DnsClient.builder(eventloop, dnsServerAddress).build(); | ||
client = HttpClient.builder(eventloop, dnsClient).build(); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
if (server != null) { | ||
server.close(); | ||
} | ||
} | ||
|
||
@Test | ||
void givenHttpServer_whenCallPersonEndpoint_thenExpectedVerificationResultShouldPresentInResponse() { | ||
HttpRequest request = HttpRequest.get("http://localhost:" + port + "/person?name=my-name").build(); | ||
|
||
client.request(request) | ||
.whenResult(response -> { | ||
assertEquals(response.getCode(), 200); | ||
|
||
response.loadBody() | ||
.whenResult(body -> { | ||
try { | ||
VerifiedPerson responseData = objectMapper.readValue(body.getArray(), | ||
VerifiedPerson.class); | ||
assertEquals(responseData.result(), "FAIL"); | ||
eventloop.breakEventloop(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
}); | ||
|
||
eventloop.run(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
libraries-5/src/test/java/com/baeldung/activej/ActiveJTest.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,40 @@ | ||
package com.baeldung.activej; | ||
|
||
import com.baeldung.activej.config.PersonModule; | ||
import com.baeldung.activej.repository.PersonRepository; | ||
import com.baeldung.activej.service.PersonService; | ||
import io.activej.eventloop.Eventloop; | ||
import io.activej.inject.Injector; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class ActiveJTest { | ||
|
||
@Test | ||
void givenPersonModule_whenGetTheServiceBean_thenAllTheDependenciesShouldBePresent() { | ||
PersonModule personModule = new PersonModule(); | ||
|
||
PersonService personService = Injector.of(personModule).getInstance(PersonService.class); | ||
assertNotNull(personService); | ||
PersonRepository personRepository = personService.getPersonRepository(); | ||
assertNotNull(personRepository); | ||
DataSource dataSource = personRepository.getDataSource(); | ||
assertNotNull(dataSource); | ||
} | ||
|
||
@Test | ||
void givenEventloop_whenCallFindAndVerifyPerson_thenExpectedVerificationResultShouldBePresent() { | ||
PersonModule personModule = new PersonModule(); | ||
|
||
PersonService personService = Injector.of(personModule).getInstance(PersonService.class); | ||
|
||
Eventloop eventloop = Eventloop.create(); | ||
eventloop.run(); | ||
personService.findAndVerifyPerson("Good person") | ||
.whenResult(verifiedPerson -> assertEquals("SUCCESS", verifiedPerson.result())); | ||
} | ||
} |
Oops, something went wrong.