Skip to content

Commit cb54566

Browse files
service level improvements
1 parent c488931 commit cb54566

7 files changed

Lines changed: 71 additions & 41 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
3+
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
4+
<stringAttribute key="M2_GOALS" value="clean install"/>
5+
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/>
6+
<booleanAttribute key="M2_OFFLINE" value="false"/>
7+
<stringAttribute key="M2_PROFILES" value="integration"/>
8+
<listAttribute key="M2_PROPERTIES"/>
9+
<stringAttribute key="M2_RUNTIME" value="EMBEDDED"/>
10+
<booleanAttribute key="M2_SKIP_TESTS" value="false"/>
11+
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
12+
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/>
13+
<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.m2e.launching.MavenSourceLocator"/>
14+
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;sourceLookupDirector&gt;&#13;&#10;&lt;sourceContainers duplicates=&quot;true&quot;&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;default/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.debug.core.containerType.default&quot;/&gt;&#13;&#10;&lt;/sourceContainers&gt;&#13;&#10;&lt;/sourceLookupDirector&gt;&#13;&#10;"/>
15+
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
16+
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_05_64&quot; path=&quot;1&quot; type=&quot;4&quot;/&gt;&#13;&#10;"/>
17+
</listAttribute>
18+
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="true"/>
19+
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${workspace_loc:/rest-sec}"/>
20+
</launchConfiguration>

‎sec/eclipse/INTEGRATION-SANDBOX.launch‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎sec/src/main/java/org/rest/sec/persistence/service/IPrincipalService.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
import org.rest.sec.model.Principal;
66

77
public interface IPrincipalService extends IService<Principal>, INameSupport<Principal> {
8-
//
8+
9+
Principal getCurrentPrincipal();
10+
911
}

‎sec/src/main/java/org/rest/sec/persistence/service/dto/IUserService.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
import org.rest.sec.model.dto.User;
66

77
public interface IUserService extends IService<User>, INameSupport<User> {
8-
//
8+
9+
User getCurrentUser();
10+
911
}

‎sec/src/main/java/org/rest/sec/persistence/service/impl/PrincipalServiceImpl.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.data.jpa.domain.Specification;
1212
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
13+
import org.springframework.security.core.context.SecurityContextHolder;
1314
import org.springframework.stereotype.Service;
1415
import org.springframework.transaction.annotation.Transactional;
1516

@@ -34,6 +35,15 @@ public Principal findByName(final String name) {
3435
return dao.findByName(name);
3536
}
3637

38+
// other
39+
40+
@Override
41+
@Transactional(readOnly = true)
42+
public Principal getCurrentPrincipal() {
43+
final String principalName = SecurityContextHolder.getContext().getAuthentication().getName();
44+
return getDao().findByName(principalName);
45+
}
46+
3747
// Spring
3848

3949
@Override

‎sec/src/main/java/org/rest/sec/persistence/service/impl/dto/UserServiceImpl.java‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.springframework.data.domain.PageImpl;
1616
import org.springframework.data.domain.PageRequest;
1717
import org.springframework.data.domain.Sort;
18+
import org.springframework.data.domain.Sort.Direction;
1819
import org.springframework.stereotype.Service;
1920
import org.springframework.transaction.annotation.Transactional;
2021

@@ -25,7 +26,7 @@
2526
public class UserServiceImpl implements IUserService {
2627

2728
@Autowired
28-
IPrincipalService principalService;
29+
private IPrincipalService principalService;
2930

3031
public UserServiceImpl() {
3132
super();
@@ -87,15 +88,13 @@ public List<User> findAllPaginated(final int page, final int size) {
8788
}
8889

8990
@Override
91+
@Transactional(readOnly = true)
9092
public Page<User> findAllPaginatedAndSortedRaw(final int page, final int size, final String sortBy, final String sortOrder) {
9193
final Page<Principal> principalsPaginatedAndSorted = principalService.findAllPaginatedAndSortedRaw(page, size, sortBy, sortOrder);
92-
final List<User> usersPaginated = Lists.transform(principalsPaginatedAndSorted.getContent(), new PrincipalToUserFunction());
9394

94-
Sort sortInfo = null;
95-
if (sortBy != null) {
96-
sortInfo = new Sort(sortBy);
97-
}
98-
return new PageImpl<User>(usersPaginated, new PageRequest(page, size, sortInfo), principalsPaginatedAndSorted.getTotalElements());
95+
final List<User> usersPaginatedAndSorted = Lists.transform(principalsPaginatedAndSorted.getContent(), new PrincipalToUserFunction());
96+
97+
return new PageImpl<User>(usersPaginatedAndSorted, new PageRequest(page, size, constructSort(sortBy, sortOrder)), principalsPaginatedAndSorted.getTotalElements());
9998
}
10099

101100
@Override
@@ -144,4 +143,22 @@ public long count() {
144143
throw new UnsupportedOperationException();
145144
}
146145

146+
// other
147+
148+
@Override
149+
public User getCurrentUser() {
150+
final Principal principal = principalService.getCurrentPrincipal();
151+
return new User(principal);
152+
}
153+
154+
// util
155+
156+
final Sort constructSort(final String sortBy, final String sortOrder) {
157+
Sort sortInfo = null;
158+
if (sortBy != null) {
159+
sortInfo = new Sort(Direction.fromString(sortOrder), sortBy);
160+
}
161+
return sortInfo;
162+
}
163+
147164
}

‎test/src/main/java/org/rest/common/web/base/AbstractSortRESTIntegrationTest.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ public AbstractSortRESTIntegrationTest(final Class<T> clazzToSet) {
3838
// GET (paged)
3939

4040
@Test
41-
public final void whenResourcesAreRetrievedPagedAndSorted_thenNoExceptions() {
41+
public final void whenResourcesAreRetrievedPaginatedAndSorted_thenNoExceptions() {
4242
getAPI().findAllPaginatedAndSortedAsResponse(0, 41, "name", null);
4343
}
4444

4545
@Test
46-
public final void whenResourcesAreRetrievedPagedAndSorted_then200IsReceived() {
46+
public final void whenResourcesAreRetrievedPaginatedAndSorted_then200IsReceived() {
4747
final Response response = getAPI().findAllPaginatedAndSortedAsResponse(0, 1, "name", Sort.Direction.ASC.name());
4848

4949
assertThat(response.getStatusCode(), is(200));
@@ -63,40 +63,40 @@ public final void whenResourcesAreRetrievedSorted_thenResourcesAreIndeedOrdered(
6363

6464
// When
6565
final Response response = getAPI().findAllSortedAsResponse("name", Sort.Direction.ASC.name());
66-
final List<T> resourcesPagedAndSorted = getAPI().getMarshaller().decodeList(response.asString(), clazz);
66+
final List<T> resourcesPaginatedAndSorted = getAPI().getMarshaller().decodeList(response.asString(), clazz);
6767

6868
// Then
69-
assertTrue(getOrdering().isOrdered(resourcesPagedAndSorted));
69+
assertTrue(getOrdering().isOrdered(resourcesPaginatedAndSorted));
7070
}
7171

7272
@Test
73-
public final void whenResourcesAreRetrievedPagedAndSorted_thenResourcesAreIndeedOrdered() {
73+
public final void whenResourcesAreRetrievedPaginatedAndSorted_thenResourcesAreIndeedOrdered() {
7474
getAPI().createAsResponse(getAPI().createNewEntity());
7575
getAPI().createAsResponse(getAPI().createNewEntity());
7676

7777
// When
7878
final Response response = getAPI().findAllPaginatedAndSortedAsResponse(0, 4, "name", Sort.Direction.ASC.name());
79-
final List<T> resourcesPagedAndSorted = getAPI().getMarshaller().decodeList(response.asString(), clazz);
79+
final List<T> resourcesPaginatedAndSorted = getAPI().getMarshaller().decodeList(response.asString(), clazz);
8080

8181
// Then
82-
assertTrue(getOrdering().isOrdered(resourcesPagedAndSorted));
82+
assertTrue(getOrdering().isOrdered(resourcesPaginatedAndSorted));
8383
}
8484

8585
@Test
8686
@Ignore("not necessarily true")
87-
public final void whenResourcesAreRetrievedPagedAndNotSorted_thenResourcesAreNotOrdered() {
87+
public final void whenResourcesAreRetrievedPaginatedAndNotSorted_thenResourcesAreNotOrdered() {
8888
getAPI().createAsResponse(getAPI().createNewEntity());
8989
getAPI().createAsResponse(getAPI().createNewEntity());
9090

9191
// When
92-
final List<T> resourcesPagedAndSorted = getAPI().findAllPaginated(0, 6);
92+
final List<T> resourcesPaginatedAndSorted = getAPI().findAllPaginated(0, 6);
9393

9494
// Then
95-
assertFalse(getOrdering().isOrdered(resourcesPagedAndSorted));
95+
assertFalse(getOrdering().isOrdered(resourcesPaginatedAndSorted));
9696
}
9797

9898
@Test
99-
public final void whenResourcesAreRetrievedByPagedAndWithInvalidSorting_then400IsReceived() {
99+
public final void whenResourcesAreRetrievedByPaginatedAndWithInvalidSorting_then400IsReceived() {
100100
// When
101101
final Response response = getAPI().findAllPaginatedAndSortedAsResponse(0, 4, "invalid", null);
102102

0 commit comments

Comments
 (0)