com.pw.ism.TestMvcConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for com.pw.ism.TestMvcConfiguration.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pw.ism;

import com.pw.ism.controllers.AdminstratorController;
import com.pw.ism.controllers.CommunicationController;
import com.pw.ism.controllers.MainController;
import com.pw.ism.controllers.MessageController;
import com.pw.ism.heartbeat.HeartbeatRepository;
import com.pw.ism.heartbeat.InMemoryHeartbeatRepository;
import com.pw.ism.message.Message;
import com.pw.ism.message.MessageRepository;

import com.pw.ism.users.UserRepository;
import java.util.Locale;
import org.mockito.Mock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 *
 * @author NRS
 */
@WebAppConfiguration
//@PropertySource({"classpath:application.properties"})
//@ComponentScan("com.pw.ism.controllers")
@EnableWebMvc
@Configuration
public class TestMvcConfiguration extends WebMvcConfigurerAdapter {

    @Mock
    public UserRepository userRepository;

    @Mock
    public MessageRepository messageRepo;

    @Mock
    JdbcTemplate jdbcTemplate;

    @Bean
    public AdminstratorController administratorController() {
        return new AdminstratorController(heartbeatRepository(), userRepository, messageRepo, jdbcTemplate);
    }

    @Bean
    public CommunicationController comunicationController() {
        return new CommunicationController(messageRepo, heartbeatRepository());
    }

    @Bean
    public MessageController messageController() {
        return new MessageController(messageRepo);
    }

    @Bean
    public MainController mainController() {
        return new MainController(userRepository);
    }

    //    @Bean
    //    public MessageRepository messageRepository() {
    //        return new InMemoryMessageRepository();
    //    }

    @Bean
    public HeartbeatRepository heartbeatRepository() {
        return new InMemoryHeartbeatRepository();
    }

    //    @Bean 
    //    public UserRepository userRepository() {
    //        return new InMemoryUserRepository();
    //    }

    @Bean
    public Converter<String, Message> messageConverter() {
        return new Converter<String, Message>() {
            @Override
            public Message convert(String id) {
                return messageRepo.findOne(Long.valueOf(id));
                //(Long.valueOf(id));
            }
        };
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }
}