]> git.somenet.org - pub/jan/dst18.git/blob - ass2-service/auth/src/test/java/dst/ass2/service/auth/AuthenticationServiceApplicationConfig.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-service / auth / src / test / java / dst / ass2 / service / auth / AuthenticationServiceApplicationConfig.java
1 package dst.ass2.service.auth;
2
3 import javax.persistence.EntityManager;
4 import javax.persistence.PersistenceContext;
5
6 import org.springframework.beans.BeansException;
7 import org.springframework.beans.factory.annotation.Value;
8 import org.springframework.beans.factory.config.BeanPostProcessor;
9 import org.springframework.boot.SpringBootConfiguration;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Profile;
12 import org.springframework.context.annotation.PropertySource;
13 import org.springframework.orm.jpa.JpaTransactionManager;
14 import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
15 import org.springframework.transaction.PlatformTransactionManager;
16
17 import dst.ass1.jpa.dao.IDAOFactory;
18 import dst.ass1.jpa.dao.impl.DAOFactory;
19 import dst.ass1.jpa.model.IModelFactory;
20 import dst.ass1.jpa.model.impl.ModelFactory;
21 import dst.ass1.jpa.tests.TestData;
22 import dst.ass1.jpa.util.Constants;
23 import dst.ass2.service.auth.grpc.GrpcServerProperties;
24
25 @SpringBootConfiguration
26 @PropertySource("classpath:/dst/ass2/service/auth/grpc.properties")
27 public class AuthenticationServiceApplicationConfig {
28
29     @PersistenceContext
30     private EntityManager em;
31
32     @Bean
33     public IModelFactory modelFactory() {
34         return new ModelFactory();
35     }
36
37     @Bean
38     public IDAOFactory daoFactory() {
39         return new DAOFactory(em);
40     }
41
42     @Bean
43     public GrpcServerProperties grpcServerProperties(@Value("${grpc.port}") int port) {
44         return new GrpcServerProperties(port);
45     }
46
47     @Bean
48     public SpringGrpcServerRunner springGrpcServerRunner() {
49         return new SpringGrpcServerRunner();
50     }
51
52     @Bean
53     public LocalEntityManagerFactoryBean entityManagerFactoryBean() {
54         LocalEntityManagerFactoryBean bean = new LocalEntityManagerFactoryBean();
55         bean.setPersistenceUnitName(Constants.JPA_PERSISTENCE_UNIT);
56         // fixes collection proxy problem when using jersey
57         bean.getJpaPropertyMap().put("hibernate.enable_lazy_load_no_trans", true);
58         return bean;
59     }
60
61     @Bean
62     public PlatformTransactionManager transactionManager(LocalEntityManagerFactoryBean entityManagerFactoryBean) {
63         JpaTransactionManager transactionManager = new JpaTransactionManager();
64         transactionManager.setPersistenceUnitName(Constants.JPA_PERSISTENCE_UNIT);
65         transactionManager.setEntityManagerFactory(entityManagerFactoryBean.getObject());
66         return transactionManager;
67     }
68
69     @Bean
70     @Profile("testdata")
71     public TestData testData() {
72         return new TestData();
73     }
74
75     @Bean
76     @Profile("testdata")
77     public TestDataInserter testDataInserter(TestData testData, IModelFactory modelFactory, PlatformTransactionManager transactionManager) {
78         return new TestDataInserter(testData, modelFactory, transactionManager);
79     }
80
81     @Bean
82     @Profile("testdata")
83     public AuthServiceDataInjector dataInjector(TestDataInserter testDataInserter) {
84         return new AuthServiceDataInjector(em, testDataInserter);
85     }
86
87     /**
88      * Makes sure data is in the database before the {@link ICachingAuthenticationService} is initialized.
89      */
90     public static class AuthServiceDataInjector implements BeanPostProcessor {
91         private boolean dataInjected = false;
92
93         private EntityManager em;
94         private TestDataInserter testDataInserter;
95
96         public AuthServiceDataInjector(EntityManager em, TestDataInserter testDataInserter) {
97             this.em = em;
98             this.testDataInserter = testDataInserter;
99         }
100
101         @Override
102         public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
103             if (!dataInjected && (bean instanceof ICachingAuthenticationService)) {
104                 testDataInserter.insertTestData(em);
105                 dataInjected = true;
106             }
107             return bean;
108         }
109
110         @Override
111         public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
112             return bean;
113         }
114     }
115
116 }