]> git.somenet.org - pub/jan/dst18.git/blob - ass2-service/facade/src/test/java/dst/ass2/service/facade/ServiceFacadeApplicationConfig.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-service / facade / src / test / java / dst / ass2 / service / facade / ServiceFacadeApplicationConfig.java
1 package dst.ass2.service.facade;
2
3 import java.net.URI;
4
5 import org.glassfish.jersey.server.ResourceConfig;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.beans.factory.annotation.Value;
9 import org.springframework.boot.SpringBootConfiguration;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Profile;
12
13 import dst.ass2.service.api.auth.AuthenticationException;
14 import dst.ass2.service.api.auth.NoSuchUserException;
15 import dst.ass2.service.auth.client.AuthenticationClientProperties;
16 import dst.ass2.service.auth.client.IAuthenticationClient;
17 import dst.ass2.service.auth.client.impl.GrpcAuthenticationClient;
18
19 @SpringBootConfiguration
20 public class ServiceFacadeApplicationConfig {
21
22     @Bean
23     public ResourceConfig jerseyConfig() {
24         return new ResourceConfig()
25                 .packages("dst.ass2.service.facade");
26     }
27
28     @Bean
29     public URI coursePlanURI(@Value("${courseplan.uri}") URI target) {
30         return target;
31     }
32
33     @Bean
34     public AuthenticationClientProperties authenticationClientProperties(
35             @Value("${auth.host}") String host,
36             @Value("${auth.port}") int port) {
37         return new AuthenticationClientProperties(host, port);
38     }
39
40     @Bean
41     @Profile("!AuthenticationResourceTest")
42     // only use this when we're not running individual tests
43     public IAuthenticationClient grpcAuthenticationClient(AuthenticationClientProperties authenticationClientProperties) {
44         return new GrpcAuthenticationClient(authenticationClientProperties);
45     }
46
47     @Bean
48     @Profile("AuthenticationResourceTest")
49     public IAuthenticationClient mockAuthenticationClient() {
50         return new MockAuthenticationClient();
51     }
52
53     public static class MockAuthenticationClient implements IAuthenticationClient {
54
55         private static final Logger LOG = LoggerFactory.getLogger(MockAuthenticationClient.class);
56
57         public static String TOKEN = "123e4567-e89b-12d3-a456-426655440000";
58
59         @Override
60         public String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException {
61             LOG.info("Calling MockAuthenticationClient with {}, {}", email, password);
62
63             if (email.equals("junit@example.com")) {
64                 if (password.equals("junit")) {
65                     return TOKEN;
66                 }
67                 throw new AuthenticationException();
68             }
69             throw new NoSuchUserException();
70         }
71
72         @Override
73         public boolean isTokenValid(String t) {
74             return TOKEN.equals(t);
75         }
76
77         @Override
78         public void close() {
79             // pass
80         }
81     }
82 }