Refactor Batch (#2439)

This commit is contained in:
Grzegorz Piwowarek 2017-08-14 21:46:04 +02:00 committed by GitHub
parent dc9ecc143d
commit bcc122b724
9 changed files with 174 additions and 177 deletions

View File

@ -8,10 +8,10 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface UserRepository extends LdapRepository<User> { public interface UserRepository extends LdapRepository<User> {
public User findByUsername(String username); User findByUsername(String username);
public User findByUsernameAndPassword(String username, String password); User findByUsernameAndPassword(String username, String password);
public List<User> findByUsernameLikeIgnoreCase(String username); List<User> findByUsernameLikeIgnoreCase(String username);
} }

View File

@ -2,7 +2,11 @@ package com.baeldung.ldap.data.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.ldap.core.*; import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder; import org.springframework.ldap.support.LdapNameBuilder;
import javax.naming.Name; import javax.naming.Name;

View File

@ -1,17 +1,17 @@
package com.baeldung.ldap.data.service; package com.baeldung.ldap.data.service;
import java.security.MessageDigest; import com.baeldung.ldap.data.repository.User;
import java.security.NoSuchAlgorithmException; import com.baeldung.ldap.data.repository.UserRepository;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.support.LdapUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baeldung.ldap.data.repository.User; import java.security.MessageDigest;
import com.baeldung.ldap.data.repository.UserRepository; import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class UserService { public class UserService {
@ -21,20 +21,18 @@ public class UserService {
public Boolean authenticate(final String username, final String password) { public Boolean authenticate(final String username, final String password) {
User user = userRepository.findByUsernameAndPassword(username, password); User user = userRepository.findByUsernameAndPassword(username, password);
return user != null ? true : false; return user != null;
} }
public List<String> search(final String username) { public List<String> search(final String username) {
List<User> userList = userRepository.findByUsernameLikeIgnoreCase(username); List<User> userList = userRepository.findByUsernameLikeIgnoreCase(username);
List<String> users = null; if (userList == null) {
if (null != userList) { return Collections.emptyList();
users = new ArrayList<String>();
for (User user : userList) {
users.add(user.getUsername());
} }
}
return users;
return userList.stream()
.map(User::getUsername)
.collect(Collectors.toList());
} }
public void create(final String username, final String password) { public void create(final String username, final String password) {
@ -48,7 +46,6 @@ public class UserService {
User user = userRepository.findByUsername(username); User user = userRepository.findByUsername(username);
user.setPassword(password); user.setPassword(password);
userRepository.save(user); userRepository.save(user);
} }
private String digestSHA(final String password) { private String digestSHA(final String password) {

View File

@ -1,5 +1,6 @@
package com.baeldung.ldap.javaconfig; package com.baeldung.ldap.javaconfig;
import com.baeldung.ldap.client.LdapClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
@ -11,8 +12,6 @@ import org.springframework.data.ldap.repository.config.EnableLdapRepositories;
import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.core.support.LdapContextSource;
import com.baeldung.ldap.client.LdapClient;
@Configuration @Configuration
@PropertySource("classpath:application.properties") @PropertySource("classpath:application.properties")
@ComponentScan(basePackages = {"com.baeldung.ldap.*"}) @ComponentScan(basePackages = {"com.baeldung.ldap.*"})

View File

@ -1,7 +1,6 @@
package com.baeldung.ldap.client; package com.baeldung.ldap.client;
import java.util.List; import com.baeldung.ldap.javaconfig.TestConfig;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -13,7 +12,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.ldap.javaconfig.TestConfig; import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("testlive") @ActiveProfiles("testlive")

View File

@ -1,10 +1,7 @@
package com.baeldung.ldap.client; package com.baeldung.ldap.client;
import static org.junit.Assert.assertEquals; import com.baeldung.ldap.data.service.UserService;
import static org.junit.Assert.assertThat; import com.baeldung.ldap.javaconfig.TestConfig;
import java.util.List;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -14,8 +11,10 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.ldap.data.service.UserService; import java.util.List;
import com.baeldung.ldap.javaconfig.TestConfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("testlive") @ActiveProfiles("testlive")

View File

@ -1,5 +1,6 @@
package com.baeldung.ldap.javaconfig; package com.baeldung.ldap.javaconfig;
import com.baeldung.ldap.client.LdapClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
@ -13,8 +14,6 @@ import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.test.TestContextSourceFactoryBean; import org.springframework.ldap.test.TestContextSourceFactoryBean;
import com.baeldung.ldap.client.LdapClient;
@Configuration @Configuration
@PropertySource("classpath:test_application.properties") @PropertySource("classpath:test_application.properties")
@ComponentScan(basePackages = {"com.baeldung.ldap.*"}) @ComponentScan(basePackages = {"com.baeldung.ldap.*"})