Commit d91b0311 authored by Matija Obreza's avatar Matija Obreza
Browse files

QueryDsl for OAuth model

- Missing @Transactional on #updateClient
- #autocomplete uses QueryDsl
- Added tests for #autocomplete
parent 14cc2b4b
......@@ -46,6 +46,7 @@
<includes>
<!-- List packages to be processed -->
<include>org.genesys.blocks.security.model</include>
<include>org.genesys.blocks.oauth.model</include>
</includes>
<outputDirectory>target/generated-sources/querydsl</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
......
......@@ -21,13 +21,14 @@ import org.genesys.blocks.oauth.model.AccessToken;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.stereotype.Repository;
/**
* The Interface AccessTokenRepository.
*/
@Repository
public interface AccessTokenRepository extends JpaRepository<AccessToken, String> {
public interface AccessTokenRepository extends JpaRepository<AccessToken, String>, QueryDslPredicateExecutor<AccessToken> {
/**
* Find by authentication id.
......
......@@ -16,18 +16,15 @@
package org.genesys.blocks.oauth.persistence;
import org.genesys.blocks.oauth.model.OAuthClient;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* The Interface OAuthClientRepository.
*/
@Repository
public interface OAuthClientRepository extends JpaRepository<OAuthClient, Long> {
public interface OAuthClientRepository extends JpaRepository<OAuthClient, Long>, QueryDslPredicateExecutor<OAuthClient> {
/**
* Find by client id.
......@@ -46,14 +43,4 @@ public interface OAuthClientRepository extends JpaRepository<OAuthClient, Long>
*/
OAuthClient findByIdAndVersion(long id, int version);
/**
* Autocomplete OAuth clients by title.
*
* @param title the title
* @param pageable the pageable
* @return list of auth clients
*/
@Query("select client from OAuthClient client where client.title like ?1")
List<OAuthClient> autocompleteByTitle(String title, Pageable pageable);
}
......@@ -19,13 +19,14 @@ import java.util.List;
import org.genesys.blocks.oauth.model.RefreshToken;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.stereotype.Repository;
/**
* The Interface RefreshTokenRepository.
*/
@Repository
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, String> {
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, String>, QueryDslPredicateExecutor<RefreshToken> {
/**
* Find by client id.
......
......@@ -24,11 +24,14 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.oauth.model.AccessToken;
import org.genesys.blocks.oauth.model.OAuthClient;
import org.genesys.blocks.oauth.model.OAuthRole;
import org.genesys.blocks.oauth.model.QOAuthClient;
import org.genesys.blocks.oauth.model.RefreshToken;
import org.genesys.blocks.oauth.persistence.AccessTokenRepository;
import org.genesys.blocks.oauth.persistence.OAuthClientRepository;
......@@ -634,16 +637,20 @@ public class OAuthServiceImpl implements OAuthClientDetailsService, OAuthTokenSt
* int, org.genesys.blocks.oauth.model.OAuthClient)
*/
@Override
@Transactional
public OAuthClient updateClient(final long id, final int version, final OAuthClient updates) {
final OAuthClient client = oauthClientRepository.findByIdAndVersion(id, version);
client.apply(updates);
return oauthClientRepository.save(client);
}
@Override
public List<OAuthClient> autocompleteClients(final String title) {
if (StringUtils.isBlank(title) || title.length() < 4)
return Collections.emptyList();
return oauthClientRepository.autocompleteByTitle(title + "%", new PageRequest(0, 10, new Sort("title")));
}
@Override
public List<OAuthClient> autocompleteClients(final String title) {
if (StringUtils.isBlank(title) || title.length() < 4)
return Collections.emptyList();
LOG.debug("Autocomplete for={}", title);
Predicate predicate = QOAuthClient.oAuthClient.title.startsWith(title);
return oauthClientRepository.findAll(predicate, new PageRequest(0, 10, new Sort("title"))).getContent();
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.security.oauth2;
package org.genesys.blocks.oauth;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
......@@ -87,12 +87,7 @@ public class OAuth2GrantTypeTest extends AbstractRestTest {
final MockHttpServletResponse response = mockMvc.perform(post("/oauth/token").header("Authorization", "Basic " + new String(Base64Utils.encode((DEFAULT_CLIENT_ID + ":"
+ DEFAULT_CLIENT_SECRET).getBytes()))).param("grant_type", "client_credentials").param("client_id", DEFAULT_CLIENT_ID).param("scope", "read")).andReturn().getResponse();
OAuth2AccessToken accessToken = null;
try {
accessToken = objectMapper.readValue(response.getContentAsByteArray(), OAuth2AccessToken.class);
} finally {
return accessToken;
}
return objectMapper.readValue(response.getContentAsByteArray(), OAuth2AccessToken.class);
}
private OAuthClient makeDefault() {
......
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.oauth;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import org.genesys.blocks.oauth.model.OAuthClient;
import org.genesys.blocks.security.rest.AbstractRestTest;
import org.junit.Before;
import org.junit.Test;
/**
* @author Matija Obreza
*/
public class OAuthClientTest extends AbstractRestTest {
@Before
public void setUp() {
}
@Test
public void createClient() {
OAuthClient client = oauthClientDetailsService.addClient("JUnit OAuth Client", null, null, null, null);
assertThat("OAuthClient#id must be generated", client.getId(), not(nullValue()));
assertThat("OAuthClient#clientId must be generated", client.getClientId(), not(nullValue()));
assertThat("OAuthClient#clientSecret must be generated", client.getClientSecret(), not(nullValue()));
final OAuthClient storedClient = oAuthClientRepository.findOne(client.getId());
assertThat("Could not load persisted OAuthClient", storedClient, not(nullValue()));
assertThat("OAuthClient#id does not match", storedClient.getId(), equalTo(client.getId()));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(client.getTitle().substring(0, 10)), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(client.getTitle().substring(0, 5)), hasSize(1));
}
@Test
public void updateClient() {
OAuthClient client = oauthClientDetailsService.addClient("JUnit OAuth Client", null, null, null, null);
client.setTitle("AAAABBBBCCC");
OAuthClient updatedClient = oauthClientDetailsService.updateClient(client.getId(), client.getVersion(), client);
assertThat("OAuthClient#title must be updated", updatedClient.getTitle(), is("AAAABBBBCCC"));
assertThat("OAuthClient#clientId must not be updated", updatedClient.getClientId(), is(client.getClientId()));
assertThat("OAuthClient#clientSecret must not be updated", updatedClient.getClientSecret(), is(client.getClientSecret()));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(updatedClient.getTitle().substring(0, 10)), hasSize(1));
assertThat("Autocomplete must return the 1 client", oauthClientDetailsService.autocompleteClients(updatedClient.getTitle().substring(0, 5)), hasSize(1));
}
}
......@@ -50,7 +50,7 @@ import org.springframework.transaction.annotation.Transactional;
@Configuration
@EnableAspectJAutoProxy
@Import({ DatabaseConfig.class })
@ComponentScan(basePackages = { "org.genesys.blocks.oauth.service", "org.genesys.blocks.security.service", "org.genesys.blocks.security.component" })
@ComponentScan(basePackages = { "org.genesys.blocks.security.service", "org.genesys.blocks.security.component" })
public class ApplicationConfig {
@Bean
......
......@@ -18,6 +18,8 @@ package org.genesys.blocks.security.tests;
import org.genesys.blocks.oauth.persistence.AccessTokenRepository;
import org.genesys.blocks.oauth.persistence.OAuthClientRepository;
import org.genesys.blocks.oauth.persistence.RefreshTokenRepository;
import org.genesys.blocks.oauth.service.OAuthClientDetailsService;
import org.genesys.blocks.oauth.service.OAuthTokenStoreService;
import org.genesys.blocks.security.config.ApplicationConfig;
import org.genesys.blocks.security.config.AuthorizationServerConfig;
import org.genesys.blocks.security.config.SecurityConfig;
......@@ -48,6 +50,12 @@ public abstract class BaseTest {
@Autowired
protected BasicUserService<UserRole, TestUser> testUserService;
@Autowired
protected OAuthClientDetailsService oauthClientDetailsService;
@Autowired
protected OAuthTokenStoreService oauthTokenStoreService;
@Autowired
protected OAuthClientRepository oAuthClientRepository;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment