[SYNCOPE-1416] fixes searches for unique attributes
authorfmartelli <fabio.martelli@gmail.com>
Thu, 27 Dec 2018 11:28:03 +0000 (12:28 +0100)
committerfmartelli <fabio.martelli@gmail.com>
Thu, 27 Dec 2018 11:38:58 +0000 (12:38 +0100)
core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AnySearchTest.java
fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java

index def529c..d0b958b 100644 (file)
@@ -974,8 +974,10 @@ public class JPAAnySearchDAO extends AbstractAnySearchDAO {
         StringBuilder query = new StringBuilder("SELECT DISTINCT any_id FROM ");
         switch (cond.getType()) {
             case ISNOTNULL:
-                query.append(svs.asSearchViewSupport().attr().name).append(" WHERE schema_id=").
-                        append("'").append(checked.getLeft().getKey()).append("'");
+                query.append(checked.getLeft().isUniqueConstraint()
+                        ? svs.asSearchViewSupport().uniqueAttr().name
+                        : svs.asSearchViewSupport().attr().name).
+                        append(" WHERE schema_id=").append("'").append(checked.getLeft().getKey()).append("'");
                 break;
 
             case ISNULL:
@@ -983,8 +985,10 @@ public class JPAAnySearchDAO extends AbstractAnySearchDAO {
                         append(" WHERE any_id NOT IN ").
                         append("(").
                         append("SELECT DISTINCT any_id FROM ").
-                        append(svs.asSearchViewSupport().attr().name).append(" WHERE schema_id=").
-                        append("'").append(checked.getLeft().getKey()).append("'").
+                        append(checked.getLeft().isUniqueConstraint()
+                                ? svs.asSearchViewSupport().uniqueAttr().name
+                                : svs.asSearchViewSupport().attr().name).
+                        append(" WHERE schema_id=").append("'").append(checked.getLeft().getKey()).append("'").
                         append(")");
                 break;
 
index 1dae5c1..4223282 100644 (file)
@@ -769,6 +769,25 @@ public class AnySearchTest extends AbstractTest {
         assertEquals(
                 searchDAO.count(SyncopeConstants.FULL_ADMIN_REALMS, searchCondition, AnyTypeKind.USER),
                 users.size());
+        
+        // search by attribute with unique constraint
+        AttributeCond fullnameCond = new AttributeCond(AttributeCond.Type.ISNOTNULL);
+        fullnameCond.setSchema("fullname");
+
+        SearchCond cond = SearchCond.getLeafCond(fullnameCond);
+        assertTrue(cond.isValid());
+
+        users = searchDAO.search(cond, AnyTypeKind.USER);
+        assertEquals(5, users.size());
+        
+        fullnameCond = new AttributeCond(AttributeCond.Type.ISNULL);
+        fullnameCond.setSchema("fullname");
+
+        cond = SearchCond.getLeafCond(fullnameCond);
+        assertTrue(cond.isValid());
+
+        users = searchDAO.search(cond, AnyTypeKind.USER);
+        assertTrue(users.isEmpty());
     }
 
     @Test
index fa7c5ac..cc88ad8 100644 (file)
@@ -290,17 +290,6 @@ public class SearchITCase extends AbstractITCase {
                         and("username").equalTo("bellini").query()).
                 build());
         assertEquals(users, issueSYNCOPE1321);
-
-        // SYNCOPE-1416 (check the search for attributes of type different from stringvalue)
-        PagedResult<UserTO> issueSYNCOPE1416 = userService.search(new AnyQuery.Builder().
-                realm(SyncopeConstants.ROOT_REALM).
-                fiql(SyncopeClient.getUserSearchConditionBuilder().
-                        is("loginDate").lexicalNotBefore("2009-05-26T00:00:00+0300").
-                        and("username").equalTo("rossini").query()).
-                orderBy(SyncopeClient.getOrderByClauseBuilder().asc("loginDate").build()).
-                build());
-        assertEquals(1, issueSYNCOPE1416.getSize());
-        assertEquals("rossini", issueSYNCOPE1416.getResult().get(0).getUsername());
     }
 
     @Test
@@ -558,6 +547,36 @@ public class SearchITCase extends AbstractITCase {
     }
 
     @Test
+    public void issueSYNCOPE1416() {
+        // check the search for attributes of type different from stringvalue
+        PagedResult<UserTO> issueSYNCOPE1416 = userService.search(new AnyQuery.Builder().
+                realm(SyncopeConstants.ROOT_REALM).
+                fiql(SyncopeClient.getUserSearchConditionBuilder().
+                        is("loginDate").lexicalNotBefore("2009-05-26").
+                        and("username").equalTo("rossini").query()).
+                orderBy(SyncopeClient.getOrderByClauseBuilder().asc("loginDate").build()).
+                build());
+        assertEquals(1, issueSYNCOPE1416.getSize());
+        assertEquals("rossini", issueSYNCOPE1416.getResult().get(0).getUsername());
+        
+        // search by attribute with unique constraint
+        issueSYNCOPE1416 = userService.search(new AnyQuery.Builder().
+                realm(SyncopeConstants.ROOT_REALM).
+                fiql(SyncopeClient.getUserSearchConditionBuilder().isNotNull("fullname").query()).
+                orderBy(SyncopeClient.getOrderByClauseBuilder().asc("loginDate").build()).
+                build());
+        // some identities could have been imported by pull tasks executions
+        assertTrue(issueSYNCOPE1416.getSize() >= 5);
+        
+        issueSYNCOPE1416 = userService.search(new AnyQuery.Builder().
+                realm(SyncopeConstants.ROOT_REALM).
+                fiql(SyncopeClient.getUserSearchConditionBuilder().isNull("fullname").query()).
+                orderBy(SyncopeClient.getOrderByClauseBuilder().asc("loginDate").build()).
+                build());
+        assertEquals(0, issueSYNCOPE1416.getSize());
+    }
+    
+    @Test
     public void issueSYNCOPE1417() {
         try {
             userService.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).
@@ -588,6 +607,6 @@ public class SearchITCase extends AbstractITCase {
                                 is("loginDate").notEqualTo("2009-05-26").query()).page(1).size(1).build());
         assertTrue(unmatching.getSize() > 0);
 
-        assertEquals(total.getTotalCount(), matching.getTotalCount() + unmatching.getTotalCount());;
+        assertEquals(total.getTotalCount(), matching.getTotalCount() + unmatching.getTotalCount());
     }
 }