From 1fea7b40d8e2de600cc204048281d4762404d5d3 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 20 Nov 2014 10:52:05 +0000
Subject: [PATCH] EntityTagVirtualAttributeProviderTestCase.java: Removed subclass of SearchOperationWrapper by using Mockito.

---
 opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/extensions/EntityTagVirtualAttributeProviderTestCase.java |  213 ++++++++++------------------------------------------
 1 files changed, 43 insertions(+), 170 deletions(-)

diff --git a/opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/extensions/EntityTagVirtualAttributeProviderTestCase.java b/opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/extensions/EntityTagVirtualAttributeProviderTestCase.java
index 0e29a3f..3091674 100644
--- a/opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/extensions/EntityTagVirtualAttributeProviderTestCase.java
+++ b/opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/extensions/EntityTagVirtualAttributeProviderTestCase.java
@@ -32,12 +32,12 @@
 import java.util.TreeSet;
 
 import org.forgerock.i18n.LocalizableMessage;
-import org.forgerock.i18n.LocalizableMessageBuilder;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.ConditionResult;
 import org.forgerock.opendj.ldap.ModificationType;
 import org.forgerock.opendj.ldap.ResultCode;
 import org.forgerock.opendj.ldap.SearchScope;
+import org.mockito.ArgumentCaptor;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.admin.server.ConfigurationChangeListener;
 import org.opends.server.admin.std.meta.EntityTagVirtualAttributeCfgDefn.ChecksumAlgorithm;
@@ -54,7 +54,6 @@
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.core.ModifyOperation;
 import org.opends.server.core.SearchOperation;
-import org.opends.server.core.SearchOperationWrapper;
 import org.opends.server.protocols.internal.InternalClientConnection;
 import org.opends.server.protocols.internal.InternalSearchOperation;
 import org.opends.server.protocols.internal.Requests;
@@ -76,6 +75,7 @@
 
 import static java.util.Collections.*;
 
+import static org.mockito.Mockito.*;
 import static org.opends.server.protocols.internal.InternalClientConnection.*;
 import static org.opends.server.util.CollectionUtils.*;
 import static org.testng.Assert.*;
@@ -83,23 +83,20 @@
 /**
  * A set of test cases for the entity tag virtual attribute provider.
  */
-public class EntityTagVirtualAttributeProviderTestCase extends
-    ExtensionsTestCase
+public class EntityTagVirtualAttributeProviderTestCase extends ExtensionsTestCase
 {
   private static final String DESCRIPTION = "description";
   private static final String ETAG = "etag";
 
   private final ByteString dummyValue = ByteString.valueOf("dummy");
   private final EntityTagVirtualAttributeProvider provider = new EntityTagVirtualAttributeProvider();
-  private boolean changeListenerRemoved = false;
-  private boolean changeListenerAdded = false;
+  private boolean changeListenerRemoved;
+  private boolean changeListenerAdded;
 
   private final EntityTagVirtualAttributeCfg config = new EntityTagVirtualAttributeCfg()
   {
     private final TreeSet<AttributeType> excludedAttributes = new TreeSet<AttributeType>();
 
-
-
     @Override
     public void addChangeListener(
         final ConfigurationChangeListener<VirtualAttributeCfg> listener)
@@ -108,8 +105,6 @@
       throw new IllegalStateException();
     }
 
-
-
     @Override
     public void addEntityTagChangeListener(
         final ConfigurationChangeListener<EntityTagVirtualAttributeCfg> listener)
@@ -117,8 +112,6 @@
       changeListenerAdded = true;
     }
 
-
-
     @Override
     public Class<? extends EntityTagVirtualAttributeCfg> configurationClass()
     {
@@ -126,8 +119,6 @@
       return null;
     }
 
-
-
     @Override
     public DN dn()
     {
@@ -135,8 +126,6 @@
       return null;
     }
 
-
-
     @Override
     public AttributeType getAttributeType()
     {
@@ -144,8 +133,6 @@
       return null;
     }
 
-
-
     @Override
     public SortedSet<DN> getBaseDN()
     {
@@ -153,16 +140,12 @@
       return null;
     }
 
-
-
     @Override
     public ChecksumAlgorithm getChecksumAlgorithm()
     {
       return ChecksumAlgorithm.ADLER_32;
     }
 
-
-
     @Override
     public ConflictBehavior getConflictBehavior()
     {
@@ -170,16 +153,12 @@
       return null;
     }
 
-
-
     @Override
     public SortedSet<AttributeType> getExcludedAttribute()
     {
       return excludedAttributes;
     }
 
-
-
     @Override
     public SortedSet<String> getFilter()
     {
@@ -187,8 +166,6 @@
       return null;
     }
 
-
-
     @Override
     public SortedSet<DN> getGroupDN()
     {
@@ -196,8 +173,6 @@
       return null;
     }
 
-
-
     @Override
     public String getJavaClass()
     {
@@ -205,8 +180,6 @@
       return null;
     }
 
-
-
     @Override
     public Scope getScope()
     {
@@ -214,16 +187,12 @@
       return null;
     }
 
-
-
     @Override
     public boolean isEnabled()
     {
       return true;
     }
 
-
-
     @Override
     public void removeChangeListener(
         final ConfigurationChangeListener<VirtualAttributeCfg> listener)
@@ -232,8 +201,6 @@
       throw new IllegalStateException();
     }
 
-
-
     @Override
     public void removeEntityTagChangeListener(
         final ConfigurationChangeListener<EntityTagVirtualAttributeCfg> listener)
@@ -242,39 +209,31 @@
     }
   };
 
-
-
   /**
    * Ensures that the Directory Server is running.
    *
    * @throws Exception
    *           If an unexpected problem occurs.
    */
-  @BeforeClass()
+  @BeforeClass
   public void startServer() throws Exception
   {
     TestCaseUtils.startServer();
 
     // Initialize the provider.
-    config.getExcludedAttribute().add(
-        DirectoryServer.getAttributeType("modifytimestamp"));
+    config.getExcludedAttribute().add(DirectoryServer.getAttributeType("modifytimestamp"));
     provider.initializeVirtualAttributeProvider(config);
   }
 
-
-
   /**
    * Tests that approximate matching is not supported.
    */
   @Test
   public void testApproximatelyEqualTo()
   {
-    assertEquals(provider.approximatelyEqualTo(null, null, null),
-        ConditionResult.UNDEFINED);
+    assertEquals(provider.approximatelyEqualTo(null, null, null), ConditionResult.UNDEFINED);
   }
 
-
-
   /**
    * Tests that finalization removes the change listener.
    */
@@ -285,8 +244,6 @@
     assertTrue(changeListenerRemoved);
   }
 
-
-
   /**
    * Tests the getValues method returns an ETag whose value represents a 64-bit
    * non-zero long encoded as hex.
@@ -302,8 +259,6 @@
     getEntityTag(e, getRule());
   }
 
-
-
   /**
    * Tests the getValues method returns a different value for entries which are
    * different.
@@ -322,8 +277,6 @@
     assertFalse(getEntityTag(e1, rule).equals(getEntityTag(e2, rule)));
   }
 
-
-
   /**
    * Tests the getValues method ignores excluded attributes.
    *
@@ -342,8 +295,6 @@
     assertEquals(getEntityTag(e1, rule), getEntityTag(e2, rule));
   }
 
-
-
   /**
    * Tests the getValues method returns the same value for entries having the
    * same content but with attributes in a different order.
@@ -364,8 +315,6 @@
     assertEquals(getEntityTag(e1, rule), getEntityTag(e2, rule));
   }
 
-
-
   /**
    * Tests the getValues method returns the same value for different instances
    * of the same entry.
@@ -384,20 +333,15 @@
     assertEquals(getEntityTag(e1, rule), getEntityTag(e2, rule));
   }
 
-
-
   /**
    * Tests that ordering matching is not supported.
    */
   @Test
   public void testGreaterThanOrEqualTo()
   {
-    assertEquals(provider.greaterThanOrEqualTo(null, null, null),
-        ConditionResult.UNDEFINED);
+    assertEquals(provider.greaterThanOrEqualTo(null, null, null), ConditionResult.UNDEFINED);
   }
 
-
-
   /**
    * Tests hasAllValues() membership.
    *
@@ -417,8 +361,6 @@
     assertFalse(provider.hasAllValues(e, rule, Arrays.asList(value, dummyValue)));
   }
 
-
-
   /**
    * Tests that the etags are always present.
    */
@@ -428,8 +370,6 @@
     assertTrue(provider.hasValue(null, null));
   }
 
-
-
   /**
    * Tests testHasValue membership.
    *
@@ -447,8 +387,6 @@
     assertFalse(provider.hasValue(e, rule, dummyValue));
   }
 
-
-
   /**
    * Tests that initialization adds the change listener.
    */
@@ -460,8 +398,6 @@
     assertTrue(changeListenerAdded);
   }
 
-
-
   /**
    * Tests that isConfigurationAcceptable always returns true.
    */
@@ -471,8 +407,6 @@
     assertTrue(provider.isConfigurationAcceptable(config, null));
   }
 
-
-
   /**
    * Tests that the etags are single-valued.
    */
@@ -482,8 +416,6 @@
     assertFalse(provider.isMultiValued());
   }
 
-
-
   /**
    * Tests that searching based on etag filters is not supported.
    */
@@ -494,101 +426,48 @@
     assertFalse(provider.isSearchable(null, null, true));
   }
 
-
-
   /**
    * Tests that ordering matching is not supported.
    */
   @Test
   public void testLessThanOrEqualTo()
   {
-    assertEquals(provider.lessThanOrEqualTo(null, null, null),
-        ConditionResult.UNDEFINED);
+    assertEquals(provider.lessThanOrEqualTo(null, null, null), ConditionResult.UNDEFINED);
   }
 
-
-
   /**
    * Tests that substring matching is not supported.
    */
   @Test
   public void testMatchesSubstring()
   {
-    assertEquals(provider.matchesSubstring(null, null, null, null, null),
-        ConditionResult.UNDEFINED);
+    assertEquals(provider.matchesSubstring(null, null, null, null, null), ConditionResult.UNDEFINED);
   }
 
-
-
   /**
    * Tests that searching based on etag filters is not supported.
    */
   @Test
   public void testProcessSearch()
   {
-    final SearchOperation search = new SearchOperationWrapper(null)
-    {
-      ResultCode resultCode = null;
-      LocalizableMessageBuilder message = null;
-
-
-
-      /**
-       * {@inheritDoc}
-       */
-      @Override
-      public void appendErrorMessage(final LocalizableMessage message)
-      {
-        this.message = new LocalizableMessageBuilder(message);
-      }
-
-
-
-      /**
-       * {@inheritDoc}
-       */
-      @Override
-      public LocalizableMessageBuilder getErrorMessage()
-      {
-        return message;
-      }
-
-
-
-      /**
-       * @return the resultCode
-       */
-      @Override
-      public ResultCode getResultCode()
-      {
-        return resultCode;
-      }
-
-
-
-      /**
-       * {@inheritDoc}
-       */
-      @Override
-      public void setResultCode(final ResultCode resultCode)
-      {
-        this.resultCode = resultCode;
-      }
-
-    };
+    final SearchOperation searchOp = mock(SearchOperation.class);
 
     VirtualAttributeRule rule = new VirtualAttributeRule(
         DirectoryServer.getAttributeType(ETAG), provider,
         Collections.<DN> emptySet(), SearchScope.WHOLE_SUBTREE,
         Collections.<DN> emptySet(), Collections.<SearchFilter> emptySet(),
         VirtualAttributeCfgDefn.ConflictBehavior.REAL_OVERRIDES_VIRTUAL);
-    provider.processSearch(rule, search);
-    assertEquals(search.getResultCode(), ResultCode.UNWILLING_TO_PERFORM);
-    assertNotNull(search.getErrorMessage());
+    provider.processSearch(rule, searchOp);
+
+    final ArgumentCaptor<ResultCode> resultCode = ArgumentCaptor.forClass(ResultCode.class);
+    verify(searchOp).setResultCode(resultCode.capture());
+    assertEquals(resultCode.getValue(), ResultCode.UNWILLING_TO_PERFORM);
+
+    final ArgumentCaptor<LocalizableMessage> errorMsg = ArgumentCaptor.forClass(LocalizableMessage.class);
+    verify(searchOp).appendErrorMessage(errorMsg.capture());
+    assertNotNull(errorMsg.getValue());
   }
 
-
-
   /**
    * Simulates the main use case for entity tag support: optimistic concurrency.
    * <p>
@@ -636,9 +515,8 @@
     // Apply a change using the assertion control for optimistic concurrency.
     Attribute attr = Attributes.create(DESCRIPTION, "first modify");
     List<Modification> mods = newArrayList(new Modification(ModificationType.REPLACE, attr));
-    List<Control> ctrls = Collections
-        .<Control> singletonList(new LDAPAssertionRequestControl(true,
-            LDAPFilter.createEqualityFilter(ETAG, ByteString.valueOf(etag1))));
+    Control c = new LDAPAssertionRequestControl(true, LDAPFilter.createEqualityFilter(ETAG, ByteString.valueOf(etag1)));
+    List<Control> ctrls = Collections.singletonList(c);
     ModifyOperation modifyOperation = conn.processModify(userDN, mods, ctrls);
     assertEquals(modifyOperation.getResultCode(), ResultCode.SUCCESS);
 
@@ -672,8 +550,6 @@
     assertEquals(description3, description2);
   }
 
-
-
   /**
    * Tests that the etag returned with a pre-read control after a modify
    * operation is correct. See OPENDJ-861.
@@ -735,23 +611,11 @@
     assertEquals(description2, "modified value");
 
     // Now check that the pre-read is the same as the initial etag.
-    LDAPPreReadResponseControl preReadControl = null;
-    for (Control control : modifyOperation.getResponseControls())
-    {
-      if (control instanceof LDAPPreReadResponseControl)
-      {
-        preReadControl = (LDAPPreReadResponseControl) control;
-        break;
-      }
-    }
-    assertNotNull(preReadControl);
-    String etagPreRead =
-        preReadControl.getSearchEntry().parseAttribute(ETAG).asString();
+    LDAPPreReadResponseControl preReadControl = getLDAPPreReadResponseControl(modifyOperation);
+    String etagPreRead = preReadControl.getSearchEntry().parseAttribute(ETAG).asString();
     assertEquals(etagPreRead, etag1);
   }
 
-
-
   /**
    * Tests that the etag returned with a post-read control after a modify
    * operation is correct. See OPENDJ-861.
@@ -814,8 +678,7 @@
 
     // Now check that the post-read is the same as the initial etag.
     LDAPPostReadResponseControl postReadControl = getLDAPPostReadResponseControl(modifyOperation);
-    String etagPostRead =
-        postReadControl.getSearchEntry().parseAttribute(ETAG).asString();
+    String etagPostRead = postReadControl.getSearchEntry().parseAttribute(ETAG).asString();
     assertEquals(etagPostRead, etag2);
   }
 
@@ -832,6 +695,19 @@
     return null;
   }
 
+  private LDAPPreReadResponseControl getLDAPPreReadResponseControl(ModifyOperation modifyOperation)
+  {
+    for (Control control : modifyOperation.getResponseControls())
+    {
+      if (control instanceof LDAPPreReadResponseControl)
+      {
+        return (LDAPPreReadResponseControl) control;
+      }
+    }
+    fail("Expected the ModifyOperation to have a LDAPPreReadResponseControl");
+    return null;
+  }
+
   private Entry readEntry(DN userDN) throws DirectoryException
   {
     SearchRequest request = Requests.newSearchRequest(userDN, SearchScope.BASE_OBJECT).addAttribute("*", ETAG);
@@ -843,30 +719,27 @@
     return e;
   }
 
-
-
   private ByteString getEntityTag(final Entry e, VirtualAttributeRule rule)
   {
     final Attribute values = provider.getValues(e, rule);
     assertEquals(values.size(), 1);
     final ByteString value = values.iterator().next();
     assertEquals(value.length(), 16);
-    boolean gotNonZeroByte = false;
     for (int i = 0; i < 16; i++)
     {
       assertTrue(StaticUtils.isHexDigit(value.byteAt(i)));
       if (value.byteAt(i) != 0x30)
       {
-        gotNonZeroByte = true;
+        return value;
       }
     }
-    assertTrue(gotNonZeroByte);
-    return value;
+    fail("Expected to find a non zero byte");
+    return null;
   }
 
   private VirtualAttributeRule getRule()
   {
-    AttributeType type = DirectoryServer.getAttributeType("etag", false);
+    AttributeType type = DirectoryServer.getAttributeType("etag");
     return new VirtualAttributeRule(type, provider,
         Collections.<DN>emptySet(), SearchScope.WHOLE_SUBTREE,
         Collections.<DN>emptySet(),

--
Gitblit v1.10.0