You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2021/06/04 13:32:48 UTC

[GitHub] [sling-org-apache-sling-event] angulito commented on a change in pull request #13: Add scheduled jobs for inventory printer in json format

angulito commented on a change in pull request #13:
URL: https://github.com/apache/sling-org-apache-sling-event/pull/13#discussion_r645573837



##########
File path: src/test/java/org/apache/sling/event/impl/jobs/console/InventoryPluginTest.java
##########
@@ -0,0 +1,303 @@
+/*
+ * 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.event.impl.jobs.console;
+
+
+import org.apache.felix.inventory.Format;
+import org.apache.sling.event.impl.jobs.JobConsumerManager;
+import org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration;
+import org.apache.sling.event.impl.jobs.config.JobManagerConfiguration;
+import org.apache.sling.event.impl.jobs.config.QueueConfigurationManager;
+import org.apache.sling.event.jobs.JobManager;
+import org.apache.sling.event.jobs.Queue;
+import org.apache.sling.event.jobs.QueueConfiguration;
+import org.apache.sling.event.jobs.ScheduleInfo;
+import org.apache.sling.event.jobs.ScheduledJobInfo;
+import org.apache.sling.event.jobs.Statistics;
+import org.apache.sling.event.jobs.TopicStatistics;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+
+public class InventoryPluginTest extends Mockito {
+    @Mock
+    private JobManager jobManager;
+
+    @Mock
+    private JobManagerConfiguration configuration;
+
+    @Mock
+    private JobConsumerManager jobConsumerManager;
+
+    @InjectMocks
+    private InventoryPlugin inventoryPlugin;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        Mockito.when(jobManager.getStatistics()).thenReturn(Mockito.mock(Statistics.class));
+        Queue mockQueue = Mockito.mock(Queue.class);
+        Mockito.when(mockQueue.getStatistics()).thenReturn(Mockito.mock(Statistics.class));
+
+        ScheduledJobInfo mockScheduledJobInfo = Mockito.mock(ScheduledJobInfo.class);
+        Mockito.when(mockScheduledJobInfo.getJobTopic()).thenReturn("topic");
+        ScheduleInfo mockScheduleInfo1 = Mockito.mock(ScheduleInfo.class);
+        Mockito.when(mockScheduleInfo1.getType()).thenReturn(ScheduleInfo.ScheduleType.HOURLY);
+        Mockito.when(mockScheduleInfo1.getMinuteOfHour()).thenReturn(40);
+        ScheduleInfo mockScheduleInfo2 = Mockito.mock(ScheduleInfo.class);
+        Mockito.when(mockScheduleInfo2.getType()).thenReturn(ScheduleInfo.ScheduleType.CRON);
+        Mockito.when(mockScheduleInfo2.getExpression()).thenReturn("0 * * * *");
+        Mockito.when(mockScheduledJobInfo.getSchedules()).thenReturn(new ArrayList<>(Arrays.asList(mockScheduleInfo1,
+         mockScheduleInfo2)));
+        Mockito.when(jobManager.getScheduledJobs()).thenReturn(new ArrayList<>(Arrays.asList(mockScheduledJobInfo)));
+
+        QueueConfiguration mockConfiguration = Mockito.mock(QueueConfiguration.class);
+        Mockito.when(mockConfiguration.getType()).thenReturn(QueueConfiguration.Type.ORDERED);
+        Mockito.when(mockQueue.getConfiguration()).thenReturn(mockConfiguration);
+        Mockito.when(jobManager.getQueues()).thenReturn(new ArrayList<>(Arrays.asList(mockQueue)));
+
+        TopicStatistics topicStatistics = Mockito.mock(TopicStatistics.class);
+        Mockito.when(jobManager.getTopicStatistics()).thenReturn(new ArrayList<>(Arrays.asList(topicStatistics)));
+
+        QueueConfigurationManager mockQueueConfigurationManager = Mockito.mock(QueueConfigurationManager.class);
+        InternalQueueConfiguration mockMainInternalQueueConfiguration = Mockito.mock(InternalQueueConfiguration.class);
+        Mockito.when(mockMainInternalQueueConfiguration.getType()).thenReturn(QueueConfiguration.Type.ORDERED);
+        Mockito.when(mockQueueConfigurationManager.getMainQueueConfiguration()).thenReturn(mockMainInternalQueueConfiguration);
+        InternalQueueConfiguration mockConfigInternalQueueConfiguration =
+                Mockito.mock(InternalQueueConfiguration.class);
+        Mockito.when(mockConfigInternalQueueConfiguration.getType()).thenReturn(QueueConfiguration.Type.ORDERED);
+        Mockito.when(mockQueueConfigurationManager.getConfigurations()).thenReturn(new InternalQueueConfiguration[]{mockConfigInternalQueueConfiguration});
+        Mockito.when(configuration.getQueueConfigurationManager()).thenReturn(mockQueueConfigurationManager);
+    }
+
+    @Test
+    public void printTextFormat() {
+        StringWriter writerOutput = new StringWriter();
+        PrintWriter printWriter = new PrintWriter(writerOutput);
+
+        inventoryPlugin.print(printWriter, Format.TEXT, false);
+
+        String output = writerOutput.toString();
+        assertEquals("Apache Sling Job Handling\n" +
+                "-------------------------\n" +
+                "Overall Statistics\n" +
+                "Start Time : 01:00:00:000 1970-Jan-01\n" +
+                "Local topic consumers: \n" +
+                "Last Activated : 01:00:00:000 1970-Jan-01\n" +
+                "Last Finished : 01:00:00:000 1970-Jan-01\n" +
+                "Queued Jobs : 0\n" +
+                "Active Jobs : 0\n" +
+                "Jobs : 0\n" +
+                "Finished Jobs : 0\n" +
+                "Failed Jobs : 0\n" +
+                "Cancelled Jobs : 0\n" +
+                "Processed Jobs : 0\n" +
+                "Average Processing Time : -\n" +
+                "Average Waiting Time : -\n" +
+                "\n" +
+                "Topology Capabilities\n" +
+                "No topology information available !\n" +
+                "Scheduled Jobs\n" +
+                "Schedule\n" +
+                "Job Topic< : topic\n" +
+                "Schedules : HOURLY 40, CRON 0 * * * *\n\n\n" +
+                "Active JobQueue: null \n" +
+                "Statistics\n" +
+                "Start Time : 01:00:00:000 1970-Jan-01\n" +
+                "Last Activated : 01:00:00:000 1970-Jan-01\n" +
+                "Last Finished : 01:00:00:000 1970-Jan-01\n" +
+                "Queued Jobs : 0\n" +
+                "Active Jobs : 0\n" +
+                "Jobs : 0\n" +
+                "Finished Jobs : 0\n" +
+                "Failed Jobs : 0\n" +
+                "Cancelled Jobs : 0\n" +
+                "Processed Jobs : 0\n" +
+                "Average Processing Time : -\n" +
+                "Average Waiting Time : -\n" +
+                "Status Info : null\n" +
+                "Configuration\n" +
+                "Type : Ordered\n" +
+                "Topics : \n" +
+                "Max Parallel : 0\n" +
+                "Max Retries : 0\n" +
+                "Retry Delay : 0 ms\n" +
+                "Priority : null\n" +
+                "\n" +
+                "Topic Statistics - null\n" +
+                "Last Activated : 01:00:00:000 1970-Jan-01\n" +
+                "Last Finished : 01:00:00:000 1970-Jan-01\n" +
+                "Finished Jobs : 0\n" +
+                "Failed Jobs : 0\n" +
+                "Cancelled Jobs : 0\n" +
+                "Processed Jobs : 0\n" +
+                "Average Processing Time : -\n" +
+                "Average Waiting Time : -\n" +
+                "\n" +
+                "Apache Sling Job Handling - Job Queue Configurations\n" +
+                "----------------------------------------------------\n" +
+                "Job Queue Configuration: null\n" +
+                "Valid : false\n" +
+                "Type : Ordered\n" +
+                "Topics : \n" +
+                "Max Parallel : 0\n" +
+                "Max Retries : 0\n" +
+                "Retry Delay : 0 ms\n" +
+                "Priority : null\n" +
+                "Ranking : 0\n" +
+                "\n" +
+                "Job Queue Configuration: null\n" +
+                "Valid : false\n" +
+                "Type : Ordered\n" +
+                "Topics : \n" +
+                "Max Parallel : 0\n" +
+                "Max Retries : 0\n" +
+                "Retry Delay : 0 ms\n" +
+                "Priority : null\n" +
+                "Ranking : 0\n\n", output);
+    }
+
+    @Test
+    public void printJsonFormat() {
+        StringWriter writerOutput = new StringWriter();
+        PrintWriter printWriter = new PrintWriter(writerOutput);
+
+        inventoryPlugin.print(printWriter, Format.JSON, false);
+
+        String output = writerOutput.toString();
+        assertEquals("{\n" +

Review comment:
       > hence the use of printf to generate JSON I suppose 
   
   yes, unfortunately, the usage of printf is inherited from the old code
   
   > In that case I think that's fine, but it would be nice to also check that the output is valid JSON by parsing it, which I don't think is tested now.
   
   This is a good point, particularly given the old inherited code where we are forming JSON manually. I tried to follow the json path suggestion, please let me know your thoughts, and thanks for it.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org