This is an automated email from the ASF dual-hosted git repository.

cschneider pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 8e6dfce  SLING-9445 - DistributionEvent (#4)
8e6dfce is described below

commit 8e6dfce8e39870ceec8874467eb850044815e1fb
Author: Christian Schneider <[email protected]>
AuthorDate: Wed May 13 16:56:28 2020 +0200

    SLING-9445 - DistributionEvent (#4)
---
 pom.xml                                            |  6 ++
 .../distribution/event/DistributionEvent.java      | 91 ++++++++++++++++++++++
 .../distribution/event/DistributionEventTest.java  | 69 ++++++++++++++++
 3 files changed, 166 insertions(+)

diff --git a/pom.xml b/pom.xml
index ed6997b..6af374c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -104,6 +104,12 @@
             <artifactId>jsr305</artifactId>
             <version>2.0.0</version>
         </dependency>
+        
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
diff --git 
a/src/main/java/org/apache/sling/distribution/event/DistributionEvent.java 
b/src/main/java/org/apache/sling/distribution/event/DistributionEvent.java
new file mode 100644
index 0000000..7512065
--- /dev/null
+++ b/src/main/java/org/apache/sling/distribution/event/DistributionEvent.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.sling.distribution.event;
+
+import static 
org.apache.sling.distribution.event.DistributionEventProperties.DISTRIBUTION_COMPONENT_KIND;
+import static 
org.apache.sling.distribution.event.DistributionEventProperties.DISTRIBUTION_COMPONENT_NAME;
+import static 
org.apache.sling.distribution.event.DistributionEventProperties.DISTRIBUTION_PACKAGE_ID;
+import static 
org.apache.sling.distribution.event.DistributionEventProperties.DISTRIBUTION_PATHS;
+import static 
org.apache.sling.distribution.event.DistributionEventProperties.DISTRIBUTION_TYPE;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.osgi.service.event.Event;
+
+public class DistributionEvent {
+
+    private final String packageId;
+    private final String componentName;
+    private final String componentKind;
+    private final String distType;
+    private final String[] distPaths;
+
+    public DistributionEvent(
+            String packageId,
+            String componentName,
+            String componentKind,
+            String distType,
+            String[] distPaths) {
+        this.packageId = packageId;
+        this.componentName = componentName;
+        this.componentKind = componentKind;
+        this.distType = distType;
+        this.distPaths = distPaths;
+    }
+
+    public String getPackageId() {
+        return packageId;
+    }
+
+    public String getComponentName() {
+        return componentName;
+    }
+
+    public String getComponentKind() {
+        return componentKind;
+    }
+
+    public String getDistType() {
+        return distType;
+    }
+
+    public String[] getDistPaths() {
+        return distPaths;
+    }
+
+    public Event toEvent(String topic) {
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(DISTRIBUTION_PACKAGE_ID, packageId);
+        props.put(DISTRIBUTION_COMPONENT_NAME, componentName);
+        props.put(DISTRIBUTION_COMPONENT_KIND, componentKind);
+        props.put(DISTRIBUTION_TYPE, distType);
+        props.put(DISTRIBUTION_PATHS, distPaths);
+        return new Event(topic, props);
+    }
+
+    public static DistributionEvent fromEvent(Event event) {
+        return new DistributionEvent(
+                event.getProperty(DISTRIBUTION_PACKAGE_ID).toString(), 
+                event.getProperty(DISTRIBUTION_COMPONENT_NAME).toString(),
+                event.getProperty(DISTRIBUTION_COMPONENT_KIND).toString(),
+                event.getProperty(DISTRIBUTION_TYPE).toString(),
+                (String[])event.getProperty(DISTRIBUTION_PATHS));
+    }
+}
diff --git 
a/src/test/java/org/apache/sling/distribution/event/DistributionEventTest.java 
b/src/test/java/org/apache/sling/distribution/event/DistributionEventTest.java
new file mode 100644
index 0000000..ce9a006
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/distribution/event/DistributionEventTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.sling.distribution.event;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.service.event.Event;
+
+public class DistributionEventTest {
+
+    private static final String PATH1 = "/test";
+    private static final String DIST_TYPE = "ADD";
+    private static final String NAME = "myagent";
+    private static final String KIND = "agent";
+    private static final String PKG_ID = "pkg-1";
+    private DistributionEvent event;
+
+    @Before
+    public void before() {
+        event = new DistributionEvent(PKG_ID, NAME, KIND, DIST_TYPE, new 
String[] {PATH1});
+    }
+
+    @Test
+    public void testToEvent() {
+        Event osgiEvent = 
event.toEvent(DistributionEventTopics.AGENT_PACKAGE_CREATED);
+        assertThat(osgiEvent.getTopic(), 
equalTo(DistributionEventTopics.AGENT_PACKAGE_CREATED));
+        
assertThat((String)osgiEvent.getProperty(DistributionEventProperties.DISTRIBUTION_PACKAGE_ID),
 equalTo(PKG_ID));
+        
assertThat((String)osgiEvent.getProperty(DistributionEventProperties.DISTRIBUTION_COMPONENT_KIND),
 equalTo(KIND));
+        
assertThat((String)osgiEvent.getProperty(DistributionEventProperties.DISTRIBUTION_COMPONENT_NAME),
 equalTo(NAME));
+        
assertThat((String)osgiEvent.getProperty(DistributionEventProperties.DISTRIBUTION_TYPE),
 equalTo(DIST_TYPE));
+        String[] paths = 
(String[])(osgiEvent.getProperty(DistributionEventProperties.DISTRIBUTION_PATHS));
+        assertThat(Arrays.asList(paths), CoreMatchers.hasItems(PATH1));
+    }
+    
+    @Test
+    public void testFromEvent() {
+        Event osgiEvent = 
event.toEvent(DistributionEventTopics.AGENT_PACKAGE_CREATED);
+        DistributionEvent event2 = DistributionEvent.fromEvent(osgiEvent);
+        assertThat(event2.getPackageId(), equalTo(PKG_ID));
+        assertThat(event2.getComponentKind(), equalTo(KIND));
+        assertThat(event2.getComponentName(), equalTo(NAME));
+        assertThat(event2.getDistType(), equalTo(DIST_TYPE));
+        String[] paths = event2.getDistPaths();
+        assertThat(Arrays.asList(paths), CoreMatchers.hasItems(PATH1));
+    }
+
+}

Reply via email to