Merge "Extract approvals into properties for CommentAddedEvent"
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractor.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractor.java
index 539b646..133c995 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractor.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractor.java
@@ -18,6 +18,7 @@
 
 import com.google.common.collect.Sets;
 import com.google.gerrit.server.data.AccountAttribute;
+import com.google.gerrit.server.data.ApprovalAttribute;
 import com.google.gerrit.server.data.ChangeAttribute;
 import com.google.gerrit.server.data.PatchSetAttribute;
 import com.google.gerrit.server.data.RefUpdateAttribute;
@@ -98,4 +99,11 @@
         refUpdateAttribute.refName));
     return properties;
   }
+
+  public Set<Property>extractFrom(ApprovalAttribute approvalAttribute) {
+    Set<Property> properties = Sets.newHashSet();
+    properties.add(propertyFactory.create("approval-" +
+        approvalAttribute.type, approvalAttribute.value));
+    return properties;
+  }
 }
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java
index e6f9968..fe69773 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java
@@ -20,6 +20,7 @@
 import com.google.common.collect.Sets;
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.reviewdb.client.PatchSet;
+import com.google.gerrit.server.data.ApprovalAttribute;
 import com.google.gerrit.server.events.ChangeAbandonedEvent;
 import com.google.gerrit.server.events.ChangeEvent;
 import com.google.gerrit.server.events.ChangeMergedEvent;
@@ -130,8 +131,13 @@
     common.addAll(propertyAttributeExtractor.extractFrom(event.change));
     common.addAll(propertyAttributeExtractor.extractFrom(event.patchSet));
     common.addAll(propertyAttributeExtractor.extractFrom(event.author, "commenter"));
+    if (event.approvals != null) {
+      for (ApprovalAttribute approvalAttribute : event.approvals) {
+        common.addAll(propertyAttributeExtractor.extractFrom(
+            approvalAttribute));
+      }
+    }
     common.add(propertyFactory.create("comment", event.comment));
-    //TODO approvals
     PatchSet.Id patchSetId = newPatchSetId(event.change.number,
         event.patchSet.number);
     return issueExtractor.getIssueIds(event.change.project,
diff --git a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractorTest.java b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractorTest.java
index 0aa5da5..a106d67 100644
--- a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractorTest.java
+++ b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyAttributeExtractorTest.java
@@ -21,6 +21,7 @@
 import com.google.common.collect.Sets;
 import com.google.gerrit.server.config.FactoryModule;
 import com.google.gerrit.server.data.AccountAttribute;
+import com.google.gerrit.server.data.ApprovalAttribute;
 import com.google.gerrit.server.data.ChangeAttribute;
 import com.google.gerrit.server.data.PatchSetAttribute;
 import com.google.gerrit.server.data.RefUpdateAttribute;
@@ -296,6 +297,27 @@
     assertEquals("Properties do not match", expected, actual);
   }
 
+  public void testApprovalAttribute() {
+    ApprovalAttribute approvalAttribute = new ApprovalAttribute();
+    approvalAttribute.type = "TestType";
+    approvalAttribute.value = "TestValue";
+
+    Property propertyApproval = createMock(Property.class);
+    expect(propertyFactory.create("approval-TestType", "TestValue"))
+        .andReturn(propertyApproval);
+
+    replayMocks();
+
+    PropertyAttributeExtractor extractor =
+        injector.getInstance(PropertyAttributeExtractor.class);
+
+    Set<Property> actual = extractor.extractFrom(approvalAttribute);
+
+    Set<Property> expected = Sets.newHashSet();
+    expected.add(propertyApproval);
+    assertEquals("Properties do not match", expected, actual);
+  }
+
   public void setUp() throws Exception {
     super.setUp();
     injector = Guice.createInjector(new TestModule());
diff --git a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java
index 5c331a7..421038d 100644
--- a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java
+++ b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java
@@ -24,6 +24,7 @@
 import com.google.gerrit.reviewdb.client.PatchSet;
 import com.google.gerrit.server.config.FactoryModule;
 import com.google.gerrit.server.data.AccountAttribute;
+import com.google.gerrit.server.data.ApprovalAttribute;
 import com.google.gerrit.server.data.ChangeAttribute;
 import com.google.gerrit.server.data.PatchSetAttribute;
 import com.google.gerrit.server.data.RefUpdateAttribute;
@@ -181,7 +182,7 @@
     eventHelper(event, "ChangeRestoredEvent", "change-restored", common, true);
   }
 
-  public void testCommentAddedEvent() {
+  public void testCommentAddedEventWOApprovals() {
     CommentAddedEvent event = new CommentAddedEvent();
 
     ChangeAttribute changeAttribute = createMock(ChangeAttribute.class);
@@ -221,6 +222,60 @@
     eventHelper(event, "CommentAddedEvent", "comment-added", common, true);
   }
 
+  public void testCommentAddedEventWApprovals() {
+    CommentAddedEvent event = new CommentAddedEvent();
+
+    ChangeAttribute changeAttribute = createMock(ChangeAttribute.class);
+    event.change = changeAttribute;
+    Property propertyChange = createMock(Property.class);
+    expect(propertyAttributeExtractor.extractFrom(changeAttribute))
+        .andReturn(Sets.newHashSet(propertyChange));
+
+    AccountAttribute accountAttribute = createMock(AccountAttribute.class);
+    event.author = accountAttribute;
+    Property propertySubmitter = createMock(Property.class);
+    expect(propertyAttributeExtractor.extractFrom(accountAttribute,
+        "commenter")).andReturn(Sets.newHashSet(propertySubmitter));
+
+    PatchSetAttribute patchSetAttribute = createMock(PatchSetAttribute.class);
+    event.patchSet = patchSetAttribute;
+    Property propertyPatchSet = createMock(Property.class);
+    expect(propertyAttributeExtractor.extractFrom(patchSetAttribute))
+        .andReturn(Sets.newHashSet(propertyPatchSet));
+
+    ApprovalAttribute approvalAttribute1 = createMock(ApprovalAttribute.class);
+    Property propertyApproval1 = createMock(Property.class);
+    expect(propertyAttributeExtractor.extractFrom(approvalAttribute1))
+        .andReturn(Sets.newHashSet(propertyApproval1));
+    ApprovalAttribute approvalAttribute2 = createMock(ApprovalAttribute.class);
+    Property propertyApproval2 = createMock(Property.class);
+    expect(propertyAttributeExtractor.extractFrom(approvalAttribute2))
+        .andReturn(Sets.newHashSet(propertyApproval2));
+    ApprovalAttribute approvalAttributes[] = { approvalAttribute1,
+        approvalAttribute2 };
+    event.approvals = approvalAttributes;
+
+    event.comment = "testComment";
+    Property propertyComment = createMock(Property.class);
+    expect(propertyFactory.create("comment", "testComment"))
+        .andReturn(propertyComment);
+
+    changeAttribute.project = "testProject";
+    changeAttribute.number = "176";
+    patchSetAttribute.revision = "testRevision";
+    patchSetAttribute.number = "3";
+
+    Set<Property> common = Sets.newHashSet();
+    common.add(propertyChange);
+    common.add(propertySubmitter);
+    common.add(propertyPatchSet);
+    common.add(propertyComment);
+    common.add(propertyApproval1);
+    common.add(propertyApproval2);
+
+    eventHelper(event, "CommentAddedEvent", "comment-added", common, true);
+  }
+
   public void testPatchSetCreatedEvent() {
     PatchSetCreatedEvent event = new PatchSetCreatedEvent();