Add template for assignment 2

This commit is contained in:
2021-04-26 14:12:50 +02:00
parent 6d5b4b7b48
commit dd1e95d6aa
100 changed files with 6085 additions and 0 deletions
@@ -0,0 +1,13 @@
package dst.ass2.service.facade;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceFacadeApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFacadeApplication.class, args);
}
}
@@ -0,0 +1,81 @@
package dst.ass2.service.facade;
import dst.ass2.service.api.auth.AuthenticationException;
import dst.ass2.service.api.auth.NoSuchUserException;
import dst.ass2.service.auth.client.AuthenticationClientProperties;
import dst.ass2.service.auth.client.IAuthenticationClient;
import dst.ass2.service.auth.client.impl.GrpcAuthenticationClient;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import java.net.URI;
@SpringBootConfiguration
public class ServiceFacadeApplicationConfig {
@Bean
public ResourceConfig jerseyConfig() {
return new ResourceConfig()
.packages("dst.ass2.service.facade");
}
@Bean
public URI tripServiceURI(@Value("${tripservice.uri}") URI target) {
return target;
}
@Bean
public AuthenticationClientProperties authenticationClientProperties(
@Value("${auth.host}") String host,
@Value("${auth.port}") int port) {
return new AuthenticationClientProperties(host, port);
}
@Bean
@Profile("!AuthenticationResourceTest")
// only use this when we're not running individual tests
public IAuthenticationClient grpcAuthenticationClient(AuthenticationClientProperties authenticationClientProperties) {
return new GrpcAuthenticationClient(authenticationClientProperties);
}
@Bean
@Profile("AuthenticationResourceTest")
public IAuthenticationClient mockAuthenticationClient() {
return new MockAuthenticationClient();
}
public static class MockAuthenticationClient implements IAuthenticationClient {
private static final Logger LOG = LoggerFactory.getLogger(MockAuthenticationClient.class);
public static String TOKEN = "123e4567-e89b-12d3-a456-426655440000";
@Override
public String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException {
LOG.info("Calling MockAuthenticationClient with {}, {}", email, password);
if (email.equals("junit@example.com")) {
if (password.equals("junit")) {
return TOKEN;
}
throw new AuthenticationException();
}
throw new NoSuchUserException();
}
@Override
public boolean isTokenValid(String t) {
return TOKEN.equals(t);
}
@Override
public void close() {
// pass
}
}
}
@@ -0,0 +1,97 @@
package dst.ass2.service.facade.test;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertNotNull;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import dst.ass2.service.facade.ServiceFacadeApplication;
import dst.ass2.service.facade.ServiceFacadeApplicationConfig;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServiceFacadeApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("AuthenticationResourceTest")
public class AuthenticationResourceTest {
@LocalServerPort
private int port;
private RestTemplate restTemplate;
private HttpHeaders headers;
@Before
public void setUp() {
headers = new HttpHeaders();
restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = new BufferingClientHttpRequestFactory(requestFactory);
requestFactory.setOutputStreaming(false);
restTemplate.setRequestFactory(bufferingClientHttpRequestFactory);
}
@Test
public void authenticate_withValidUser_returnsOkAndToken() throws Exception {
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
String url = url("/auth/authenticate");
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("email", "junit@example.com");
body.add("password", "junit");
HttpEntity<?> request = new HttpEntity<>(body, headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
assertThat(response.getStatusCode().series(), is(HttpStatus.Series.SUCCESSFUL));
assertNotNull(response.getBody());
assertThat(response.getBody(), is(ServiceFacadeApplicationConfig.MockAuthenticationClient.TOKEN));
}
@Test
public void authenticate_withInvalidUser_returnsAppropriateCode() throws Exception {
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
String url = url("/auth/authenticate");
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("email", "nonexisting@example.com");
body.add("password", "wrong");
HttpEntity<?> request = new HttpEntity<>(body, headers);
HttpStatus status;
try {
status = restTemplate.postForEntity(url, request, String.class).getStatusCode();
} catch (HttpClientErrorException e) {
status = e.getStatusCode();
}
assertThat("Return an appropriate error code", status, allOf(
not(HttpStatus.OK),
not(HttpStatus.NOT_FOUND),
not(HttpStatus.INTERNAL_SERVER_ERROR)
));
}
private String url(String uri) {
return "http://localhost:" + port + uri;
}
}
@@ -0,0 +1,8 @@
server.port=8090
# this is the gRPC host the client tries to connect to
auth.host=localhost
auth.port=50051
# this is the root of the remote resource that the facade accesses via its client
tripservice.uri=http://localhost:8091/
@@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %highlight(%5p) [%12.12thread] %cyan(%-40.40logger{39}): %m%n</pattern>
</encoder>
</appender>
<root level="${log.level:-INFO}">
<appender-ref ref="STDOUT"/>
</root>
</configuration>