Ticket 663: ACRR gets wrong accumulation time when node load is excessive jenkins-Beast-361
authorAnders Henja <anders@henjab.se>
Mon, 5 Dec 2016 21:16:19 +0000 (22:16 +0100)
committerAnders Henja <anders@henjab.se>
Mon, 5 Dec 2016 21:16:19 +0000 (22:16 +0100)
src/eu/baltrad/beast/message/mo/BltTriggerJobMessage.java
src/eu/baltrad/beast/rules/acrr/AcrrRule.java
src/eu/baltrad/beast/rules/gra/GraRule.java
src/eu/baltrad/beast/rules/util/IRuleUtilities.java
src/eu/baltrad/beast/rules/util/RuleUtilities.java
src/eu/baltrad/beast/scheduler/BeastJobInvoker.java
test/eu/baltrad/beast/message/mo/BltTriggerJobMessageTest.java
test/eu/baltrad/beast/rules/acrr/AcrrRuleTest.java
test/eu/baltrad/beast/rules/gra/GraRuleTest.java
test/eu/baltrad/beast/scheduler/BeastJobInvokerTest.java

index 7a5109b..dc1e0fd 100644 (file)
@@ -18,6 +18,8 @@ along with the Beast library library.  If not, see <http://www.gnu.org/licenses/
 ------------------------------------------------------------------------*/
 package eu.baltrad.beast.message.mo;
 
+import java.util.Date;
+import java.text.SimpleDateFormat;
 import java.util.List;
 
 import org.dom4j.Document;
@@ -40,6 +42,8 @@ public class BltTriggerJobMessage implements IBltXmlMessage {
    */
   public static final String BLT_TRIGGER_JOB = "blttriggerjob";
 
+  private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+  
   /**
    * The id for this message
    */
@@ -50,6 +54,15 @@ public class BltTriggerJobMessage implements IBltXmlMessage {
    */
   private String name = null;
   
+  
+  private Date scheduledFireTime = null;
+  
+  private Date fireTime = null;
+  
+  private Date nextFireTime = null;
+  
+  private Date prevFireTime = null;
+  
   /**
    * Optional information 
    */
@@ -67,6 +80,14 @@ public class BltTriggerJobMessage implements IBltXmlMessage {
     }
     setId(dom.valueOf("//blttriggerjob/id"));
     setName(dom.valueOf("//blttriggerjob/name"));
+    Node schedule = dom.selectSingleNode("//blttriggerjob/schedule");
+    if (schedule != null) {
+      setPrevFireTime(convertToDate(dom.valueOf("//blttriggerjob/schedule/@prevFireTime")));
+      setScheduledFireTime(convertToDate(dom.valueOf("//blttriggerjob/schedule/@scheduledFireTime")));
+      setFireTime(convertToDate(dom.valueOf("//blttriggerjob/schedule/@fireTime")));
+      setNextFireTime(convertToDate(dom.valueOf("//blttriggerjob/schedule/@nextFireTime")));
+    }
+    
     List<Node> nodes = dom.selectNodes("//blttriggerjob/arguments/arg");
     this.args = new String[nodes.size()];
     for (Node node : nodes) {
@@ -84,6 +105,14 @@ public class BltTriggerJobMessage implements IBltXmlMessage {
     el.addElement("id").addText(this.id);
     el.addElement("name").addText(this.name);
 
+    if (this.prevFireTime != null || this.scheduledFireTime != null || this.fireTime != null || this.nextFireTime != null) {
+      Element schedule = el.addElement("schedule");
+      addDateStringAttribute(schedule, "prevFireTime", this.prevFireTime);
+      addDateStringAttribute(schedule, "scheduledFireTime", this.scheduledFireTime);
+      addDateStringAttribute(schedule, "fireTime", this.fireTime);
+      addDateStringAttribute(schedule, "nextFireTime", this.nextFireTime);
+    }
+    
     Element elArgs = el.addElement("arguments");
     for (int i = 0; i < this.args.length; i++) {
       if (this.args[i] != null) {
@@ -93,8 +122,42 @@ public class BltTriggerJobMessage implements IBltXmlMessage {
     
     return document;
   }
+  
+  /**
+   * Creates a UTC formatted date string
+   * @param date the date to format
+   * @return the utc formatted string
+   */
+  protected void addDateStringAttribute(Element el, String attrname, Date date) {
+    if (date != null) {
+      String dtstr = null;
+      synchronized(DATE_FORMAT) {
+        dtstr = DATE_FORMAT.format(date);
+      }
+      el.addAttribute(attrname, dtstr);
+    }
+  }
 
   /**
+   * Converts a UTC formatted string (yyyy-MM-ddTHH:mm:ssZ) into a date object
+   * @param str the utc formatted string
+   * @return the date
+   */
+  protected Date convertToDate(String str) {
+    if (str != null && !str.equals("")) {
+      try {
+        synchronized (DATE_FORMAT) {
+          return DATE_FORMAT.parse(str);
+        }
+      } catch (Exception e) {
+        throw new MessageParserException("Failed to parse date format string: " + str, e);
+      }
+    }
+    return null;
+  }
+  
+  
+  /**
    * @param id the id to set
    */
   public void setId(String id) {
@@ -139,4 +202,36 @@ public class BltTriggerJobMessage implements IBltXmlMessage {
   public String[] getArgs() {
     return args;
   }
+
+  public Date getScheduledFireTime() {
+    return scheduledFireTime;
+  }
+
+  public void setScheduledFireTime(Date scheduledFireTime) {
+    this.scheduledFireTime = scheduledFireTime;
+  }
+
+  public Date getFireTime() {
+    return fireTime;
+  }
+
+  public void setFireTime(Date fireTime) {
+    this.fireTime = fireTime;
+  }
+
+  public Date getNextFireTime() {
+    return nextFireTime;
+  }
+
+  public void setNextFireTime(Date nextFireTime) {
+    this.nextFireTime = nextFireTime;
+  }
+
+  public Date getPrevFireTime() {
+    return prevFireTime;
+  }
+
+  public void setPrevFireTime(Date prevFireTime) {
+    this.prevFireTime = prevFireTime;
+  }
 }
index 67a32de..1186447 100644 (file)
@@ -19,7 +19,6 @@ along with the Beast library library.  If not, see <http://www.gnu.org/licenses/
 
 package eu.baltrad.beast.rules.acrr;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.HashMap;
@@ -28,7 +27,6 @@ import java.util.Map;
 
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
-import org.codehaus.jackson.map.ObjectMapper;
 import org.springframework.beans.factory.BeanInitializationException;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.util.StringUtils;
@@ -384,6 +382,9 @@ public class AcrrRule implements IRule, InitializingBean {
       if (message instanceof BltTriggerJobMessage) {
         logger.info("ENTER: execute ruleId: " + getRuleId() + ", thread: " + Thread.currentThread().getName());
         DateTime nowdt = ruleUtil.nowDT();
+        if (((BltTriggerJobMessage)message).getScheduledFireTime() != null) {
+          nowdt = ruleUtil.createDateTime(((BltTriggerJobMessage)message).getScheduledFireTime());
+        }
         DateTime nt = ruleUtil.createNominalTime(nowdt, getFilesPerHourInterval());
         List<CatalogEntry> entries = findFiles(nowdt);
         List<String> uuids = ruleUtil.getUuidStringsFromEntries(entries);
index bbfb25f..969148b 100644 (file)
@@ -96,7 +96,11 @@ public class GraRule extends AcrrRule {
     try {
       if (message instanceof BltTriggerJobMessage) {
         logger.info("ENTER: execute ruleId: " + getRuleId() + ", thread: " + Thread.currentThread().getName());
-        DateTime nt = getNominalTime(getNowDT());
+        DateTime nowdt = getNowDT();
+        if (((BltTriggerJobMessage)message).getScheduledFireTime() != null) {
+          nowdt = getRuleUtilities().createDateTime(((BltTriggerJobMessage)message).getScheduledFireTime());
+        }        
+        DateTime nt = getNominalTime(nowdt);
         List<CatalogEntry> entries = findFiles(nt);
         List<String> uuids = getRuleUtilities().getUuidStringsFromEntries(entries);
         
index 33cb609..a5de5b9 100644 (file)
@@ -141,6 +141,13 @@ public interface IRuleUtilities {
   public DateTime createDateTime(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second);
   
   /**
+   * Creates a date time instance from a util date.
+   * @param date the date
+   * @return the date time
+   */
+  public DateTime createDateTime(java.util.Date date);
+  
+  /**
    * Creates the nominal time from current time and an interval.
    * 
    * The interval must be evenly dividable by 60. E.g. 1,2,3,4,...,15,..30,60.
index 8d115a5..5570556 100644 (file)
@@ -279,6 +279,12 @@ public class RuleUtilities implements IRuleUtilities {
     return new DateTime(year, month, dayOfMonth, hourOfDay, minute, second);
   }
   
+  @Override
+  public DateTime createDateTime(java.util.Date date) {
+    GregorianCalendar c = new GregorianCalendar();
+    c.setTime(date);
+    return createDateTime(c);
+  }
   
   /**
    * @see eu.baltrad.beast.rules.util.IRuleUtilities#getSourcesFromEntries(java.util.List)
index 51618ec..8c4c722 100644 (file)
@@ -18,6 +18,8 @@ along with the Beast library library.  If not, see <http://www.gnu.org/licenses/
 ------------------------------------------------------------------------*/
 package eu.baltrad.beast.scheduler;
 
+import java.util.Date;
+
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 import org.quartz.Job;
@@ -45,7 +47,12 @@ public class BeastJobInvoker implements Job {
     String id = ctx.getTrigger().getKey().getName();
     String name = detail.getKey().getName();
     logger.debug("Running triggered job message with id="+id+" and name="+name);
-    BltTriggerJobMessage msg = createMessage(id, name);
+    Date scheduledFireTime = ctx.getScheduledFireTime();
+    Date fireTime = ctx.getFireTime();
+    Date prevFireTime = ctx.getPreviousFireTime();
+    Date nextFireTime = ctx.getNextFireTime();
+    
+    BltTriggerJobMessage msg = createMessage(id, name, scheduledFireTime, fireTime, prevFireTime, nextFireTime);
     mgr.manage(msg);
   }
   
@@ -55,10 +62,14 @@ public class BeastJobInvoker implements Job {
    * @param name the name
    * @return the message
    */
-  protected BltTriggerJobMessage createMessage(String id, String name) {
+  protected BltTriggerJobMessage createMessage(String id, String name, Date scheduledFireTime, Date fireTime, Date prevFireTime, Date nextFireTime) {
     BltTriggerJobMessage result = new BltTriggerJobMessage();
     result.setId(id);
     result.setName(name);
+    result.setScheduledFireTime(scheduledFireTime);
+    result.setFireTime(fireTime);
+    result.setPrevFireTime(prevFireTime);
+    result.setNextFireTime(nextFireTime);
     return result;
   }
 }
index 109b94a..2196dfc 100644 (file)
@@ -19,8 +19,14 @@ along with the Beast library library.  If not, see <http://www.gnu.org/licenses/
 package eu.baltrad.beast.message.mo;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
+
 import org.dom4j.Document;
 import org.dom4j.DocumentHelper;
 import org.dom4j.Element;
@@ -128,6 +134,87 @@ public class BltTriggerJobMessageTest {
   }
   
   @Test
+  public void testFromDocument_noTimes() throws Exception {
+    Document document = DocumentHelper.createDocument();
+    Element el = document.addElement("blttriggerjob");
+    el.addElement("id").addText("a.id");
+    el.addElement("name").addText("a.name");
+    
+    // execute
+    BltTriggerJobMessage classUnderTest = new BltTriggerJobMessage();
+    classUnderTest.fromDocument(document);
+    
+    // verify
+    assertEquals("a.id", classUnderTest.getId());
+    assertEquals("a.name", classUnderTest.getName());
+    assertNull(classUnderTest.getScheduledFireTime());
+    assertNull(classUnderTest.getFireTime());
+    assertNull(classUnderTest.getNextFireTime());
+    assertNull(classUnderTest.getPrevFireTime());
+    
+    String[] args = classUnderTest.getArgs();
+    assertEquals(0, args.length);
+  }
+
+  @Test
+  public void testFromDocument_times() throws Exception {
+    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+//    dateFormat.parse("2016-12-01T09:59:00Z");
+    
+    Document document = DocumentHelper.createDocument();
+    Element el = document.addElement("blttriggerjob");
+    el.addElement("id").addText("a.id");
+    el.addElement("name").addText("a.name");
+    Element schedule = el.addElement("schedule");
+    schedule.addAttribute("prevFireTime", "2016-12-01T09:59:00Z");
+    schedule.addAttribute("scheduledFireTime", "2016-12-01T10:00:00Z");
+    schedule.addAttribute("fireTime", "2016-12-01T10:01:00Z");
+    schedule.addAttribute("nextFireTime", "2016-12-01T10:02:00Z");
+    
+    // execute
+    BltTriggerJobMessage classUnderTest = new BltTriggerJobMessage();
+    classUnderTest.fromDocument(document);
+    
+    // verify
+    assertEquals("a.id", classUnderTest.getId());
+    assertEquals("a.name", classUnderTest.getName());
+    assertEquals("2016-12-01T09:59:00Z", dateFormat.format(classUnderTest.getPrevFireTime()));
+    assertEquals("2016-12-01T10:00:00Z", dateFormat.format(classUnderTest.getScheduledFireTime()));
+    assertEquals("2016-12-01T10:01:00Z", dateFormat.format(classUnderTest.getFireTime()));
+    assertEquals("2016-12-01T10:02:00Z", dateFormat.format(classUnderTest.getNextFireTime()));
+    
+    String[] args = classUnderTest.getArgs();
+    assertEquals(0, args.length);
+  }
+
+  @Test
+  public void testFromDocument_onlyScheduledTime() throws Exception {
+    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+    
+    Document document = DocumentHelper.createDocument();
+    Element el = document.addElement("blttriggerjob");
+    el.addElement("id").addText("a.id");
+    el.addElement("name").addText("a.name");
+    Element schedule = el.addElement("schedule");
+    schedule.addAttribute("scheduledFireTime", "2016-12-01T10:00:00Z");
+    
+    // execute
+    BltTriggerJobMessage classUnderTest = new BltTriggerJobMessage();
+    classUnderTest.fromDocument(document);
+    
+    // verify
+    assertEquals("a.id", classUnderTest.getId());
+    assertEquals("a.name", classUnderTest.getName());
+    assertNull(classUnderTest.getPrevFireTime());
+    assertEquals("2016-12-01T10:00:00Z", dateFormat.format(classUnderTest.getScheduledFireTime()));
+    assertNull(classUnderTest.getFireTime());
+    assertNull(classUnderTest.getNextFireTime());
+    
+    String[] args = classUnderTest.getArgs();
+    assertEquals(0, args.length);
+  }
+
+  @Test
   public void testToDocument() throws Exception {
     BltTriggerJobMessage classUnderTest = new BltTriggerJobMessage();
     classUnderTest.setId("a.id");
@@ -140,4 +227,31 @@ public class BltTriggerJobMessageTest {
     assertEquals("-k", result.valueOf("//blttriggerjob/arguments/arg[1]"));
     assertEquals("val", result.valueOf("//blttriggerjob/arguments/arg[2]"));
   }
+  
+  @Test
+  public void toDocument_schedule() throws Exception {
+    Calendar c = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC+1"));
+    c.set(2016, Calendar.DECEMBER, 1, 10, 0);
+    c.set(Calendar.SECOND, 0);
+    
+    BltTriggerJobMessage classUnderTest = new BltTriggerJobMessage();
+    classUnderTest.setId("a.id");
+    classUnderTest.setName("a.name");
+    classUnderTest.setPrevFireTime(c.getTime());
+    c.set(Calendar.MINUTE, 1);
+    classUnderTest.setScheduledFireTime(c.getTime());
+    c.set(Calendar.MINUTE, 2);
+    classUnderTest.setFireTime(c.getTime());
+    c.set(Calendar.MINUTE, 3);
+    classUnderTest.setNextFireTime(c.getTime());
+    
+    Document result = classUnderTest.toDocument();
+    assertEquals("blttriggerjob", result.getRootElement().getName());
+    assertEquals("a.id", result.valueOf("//blttriggerjob/id"));
+    assertEquals("a.name", result.valueOf("//blttriggerjob/name"));
+    assertEquals("2016-12-01T11:00:00Z", result.valueOf("//blttriggerjob/schedule/@prevFireTime"));
+    assertEquals("2016-12-01T11:01:00Z", result.valueOf("//blttriggerjob/schedule/@scheduledFireTime"));
+    assertEquals("2016-12-01T11:02:00Z", result.valueOf("//blttriggerjob/schedule/@fireTime"));
+    assertEquals("2016-12-01T11:03:00Z", result.valueOf("//blttriggerjob/schedule/@nextFireTime"));
+  }
 }
index b6c64e9..b285df6 100644 (file)
@@ -316,6 +316,77 @@ public class AcrrRuleTest extends EasyMockSupport {
   }
   
   @Test
+  public void handle_withScheduledTime() {
+    java.util.Date scheduledDate = new java.util.Date();
+    BltTriggerJobMessage message = new BltTriggerJobMessage();
+    message.setScheduledFireTime(scheduledDate);
+    
+    // Setup
+    final Methods methods = createMock(Methods.class);
+
+    DateTime dtNow = new DateTime();
+    DateTime dtOther = new DateTime();
+    DateTime nominalTime = new DateTime();
+    List<CatalogEntry> entries = new ArrayList<CatalogEntry>();
+    List<String> uuids = new ArrayList<String>();
+    uuids.add("A");
+    uuids.add("B");
+    
+    expect(ruleUtil.nowDT()).andReturn(dtNow);
+    expect(ruleUtil.createDateTime(scheduledDate)).andReturn(dtOther);
+    expect(ruleUtil.createNominalTime(dtOther, 15)).andReturn(nominalTime);
+    expect(methods.findFiles(dtOther)).andReturn(entries);
+    expect(ruleUtil.getUuidStringsFromEntries(entries)).andReturn(uuids);
+    
+    classUnderTest = new AcrrRule() {
+      @Override
+      protected List<CatalogEntry> findFiles(DateTime now) {
+        return methods.findFiles(now);
+      }
+    };
+    classUnderTest.setRuleUtilities(ruleUtil);
+    classUnderTest.setCatalog(catalog);
+    classUnderTest.setArea("swegmaps");
+    classUnderTest.setZrA(10.0);
+    classUnderTest.setZrB(11.0);
+    classUnderTest.setFilesPerHour(4);
+    classUnderTest.setHours(6);
+    classUnderTest.setAcceptableLoss(20);
+    classUnderTest.setQuantity("DBZH");
+    classUnderTest.setDistancefield("se.distancefield");
+    classUnderTest.setApplyGRA(true);
+    
+    
+    replayAll();
+    
+    // Test
+    BltGenerateMessage result = (BltGenerateMessage)classUnderTest.handle(message);
+    
+    // Verify
+    verifyAll();
+    
+    String dateStr = new Formatter().format("%d%02d%02d",nominalTime.getDate().year(), nominalTime.getDate().month(), nominalTime.getDate().day()).toString();
+    String timeStr = new Formatter().format("%02d%02d%02d",nominalTime.getTime().hour(), nominalTime.getTime().minute(), nominalTime.getTime().second()).toString();
+    
+    assertEquals("eu.baltrad.beast.GenerateAcrr", result.getAlgorithm());
+    assertEquals(2, result.getFiles().length);
+    assertEquals("A", result.getFiles()[0]);
+    assertEquals("B", result.getFiles()[1]);
+    assertEquals(11, result.getArguments().length);
+    assertEquals("--area=swegmaps", result.getArguments()[0]);
+    assertEquals("--date="+dateStr, result.getArguments()[1]);
+    assertEquals("--time="+timeStr, result.getArguments()[2]);
+    assertEquals("--zra=10.0", result.getArguments()[3]);
+    assertEquals("--zrb=11.0", result.getArguments()[4]);
+    assertEquals("--hours=6", result.getArguments()[5]);
+    assertEquals("--N=25", result.getArguments()[6]);
+    assertEquals("--accept=20", result.getArguments()[7]);
+    assertEquals("--quantity=DBZH", result.getArguments()[8]);
+    assertEquals("--distancefield=se.distancefield", result.getArguments()[9]);
+    assertEquals("--applygra=true", result.getArguments()[10]);
+  }
+  
+  @Test
   public void test_filterEntries() {
     final Methods methods = createMock(Methods.class);
 
index 8eac3f0..a224d92 100644 (file)
@@ -21,13 +21,24 @@ package eu.baltrad.beast.rules.gra;
 
 import static org.junit.Assert.*;
 
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
 import org.easymock.EasyMockSupport;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
 
 import eu.baltrad.bdb.util.DateTime;
 import eu.baltrad.beast.db.Catalog;
+import eu.baltrad.beast.db.CatalogEntry;
+import eu.baltrad.beast.message.mo.BltGenerateMessage;
+import eu.baltrad.beast.message.mo.BltTriggerJobMessage;
 import eu.baltrad.beast.rules.util.IRuleUtilities;
 
 /**
@@ -39,6 +50,12 @@ public class GraRuleTest extends EasyMockSupport {
   private IRuleUtilities ruleUtil = null;
   private GraRule classUnderTest = null;
 
+  private interface Methods {
+    DateTime getNominalTime(DateTime now);
+
+    List<CatalogEntry> findFiles(DateTime now);
+  };
+
   /**
    * @throws java.lang.Exception
    */
@@ -71,7 +88,7 @@ public class GraRuleTest extends EasyMockSupport {
   public void testGetType() {
     assertEquals("blt_gra", classUnderTest.getType());
   }
-  
+
   @Test
   public void testSetGetFirstTermUTC() {
     assertEquals(6, classUnderTest.getFirstTermUTC());
@@ -80,7 +97,7 @@ public class GraRuleTest extends EasyMockSupport {
       assertEquals(i, classUnderTest.getFirstTermUTC());
     }
   }
-  
+
   @Test
   public void testSetFirstTermUTC_invalid() {
     try {
@@ -91,7 +108,7 @@ public class GraRuleTest extends EasyMockSupport {
     }
     assertEquals(6, classUnderTest.getFirstTermUTC());
   }
-  
+
   @Test
   public void testSetGetInterval() {
     assertEquals(12, classUnderTest.getInterval());
@@ -112,7 +129,7 @@ public class GraRuleTest extends EasyMockSupport {
     classUnderTest.setInterval(24);
     assertEquals(24, classUnderTest.getInterval());
   }
-  
+
   @Test
   public void testSetInterval_invalid() {
     try {
@@ -123,7 +140,7 @@ public class GraRuleTest extends EasyMockSupport {
     }
     assertEquals(12, classUnderTest.getInterval());
   }
-  
+
   @Test
   public void testIntervalHoursSame() {
     assertEquals(12, classUnderTest.getHours());
@@ -149,36 +166,97 @@ public class GraRuleTest extends EasyMockSupport {
     assertEquals(12, classUnderTest.getHours());
     assertEquals(12, classUnderTest.getInterval());
   }
-  
+
+  @Test
+  public void handle() {
+    final Methods methods = createMock(Methods.class);
+    List<CatalogEntry> entries = new ArrayList<CatalogEntry>();
+    List<String> uuids = new ArrayList<String>();
+    uuids.add("abc-def-ghi");
+    Date scheduledDate = new Date();
+    DateTime nowDt = new DateTime();
+    DateTime newDt = new DateTime();
+    DateTime nominalDt = new DateTime(2016,2,3,10,15,0);
+    BltTriggerJobMessage message = new BltTriggerJobMessage();
+    message.setScheduledFireTime(scheduledDate);
+
+    classUnderTest = new GraRule() {
+      @Override
+      protected DateTime getNominalTime(DateTime dt) {
+        return methods.getNominalTime(dt);
+      }
+
+      @Override
+      protected List<CatalogEntry> findFiles(DateTime now) {
+        return methods.findFiles(now);
+      }
+    };
+    classUnderTest.setCatalog(catalog);
+    classUnderTest.setRuleUtilities(ruleUtil);
+    classUnderTest.setArea("myarea");
+    classUnderTest.setZrA(1.1);
+    classUnderTest.setZrB(2.2);
+    classUnderTest.setFilesPerHour(4);
+    classUnderTest.setHours(12);
+    classUnderTest.setAcceptableLoss(2);
+    expect(ruleUtil.nowDT()).andReturn(nowDt);
+    expect(ruleUtil.createDateTime(scheduledDate)).andReturn(newDt);
+    expect(methods.getNominalTime(newDt)).andReturn(nominalDt);
+    expect(methods.findFiles(nominalDt)).andReturn(entries);
+    expect(ruleUtil.getUuidStringsFromEntries(entries)).andReturn(uuids);
+    
+    replayAll();
+    
+    BltGenerateMessage result = (BltGenerateMessage)classUnderTest.handle(message);
+    
+    verifyAll();
+    assertEquals("eu.baltrad.beast.CreateGraCoefficient", result.getAlgorithm());
+    assertEquals(10, result.getArguments().length);
+    assertEquals("--area=myarea", result.getArguments()[0]);
+    assertEquals("--date=20160203", result.getArguments()[1]);
+    assertEquals("--time=101500", result.getArguments()[2]);
+    assertEquals("--zra=1.1", result.getArguments()[3]);
+    assertEquals("--zrb=2.2", result.getArguments()[4]);
+    assertEquals("--interval=12", result.getArguments()[5]);
+    assertEquals("--N=49", result.getArguments()[6]);
+    assertEquals("--accept=2", result.getArguments()[7]);
+    assertEquals("--quantity=DBZH", result.getArguments()[8]);
+    assertEquals("--distancefield=eu.baltrad.composite.quality.distance.radar", result.getArguments()[9]);
+    
+    assertEquals(1,result.getFiles().length);
+    assertEquals("abc-def-ghi", result.getFiles()[0]);
+    
+  }
+
   @Test
   public void testGetNominalTime() {
     classUnderTest.setInterval(12);
     classUnderTest.setFirstTermUTC(6);
-    
-    assertEquals(new DateTime(2013,5,3,18,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,5,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,6,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,7,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,8,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,17,5,4)));
-    assertEquals(new DateTime(2013,5,4,18,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,19,5,4)));
+
+    assertEquals(new DateTime(2013, 5, 3, 18, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 5, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 6, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 7, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 8, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 17, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 18, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 19, 5, 4)));
 
     classUnderTest.setInterval(12);
     classUnderTest.setFirstTermUTC(18);
-    assertEquals(new DateTime(2013,5,3,18,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,5,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,6,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,7,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,8,5,4)));
-    assertEquals(new DateTime(2013,5,4,6,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,17,5,4)));
-    assertEquals(new DateTime(2013,5,4,18,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,19,5,4)));
+    assertEquals(new DateTime(2013, 5, 3, 18, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 5, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 6, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 7, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 8, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 6, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 17, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 18, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 19, 5, 4)));
 
     classUnderTest.setInterval(6);
     classUnderTest.setFirstTermUTC(23);
-    assertEquals(new DateTime(2013,5,3,23,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,0,5,4)));
-    assertEquals(new DateTime(2013,5,3,23,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,1,5,4)));
-    assertEquals(new DateTime(2013,5,3,23,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,2,5,4)));
-    assertEquals(new DateTime(2013,5,3,23,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,3,5,4)));
-    assertEquals(new DateTime(2013,5,3,23,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,4,5,4)));
-    assertEquals(new DateTime(2013,5,4,5,0,0), classUnderTest.getNominalTime(new DateTime(2013,5,4,5,5,4)));
+    assertEquals(new DateTime(2013, 5, 3, 23, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 0, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 3, 23, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 1, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 3, 23, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 2, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 3, 23, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 3, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 3, 23, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 4, 5, 4)));
+    assertEquals(new DateTime(2013, 5, 4, 5, 0, 0), classUnderTest.getNominalTime(new DateTime(2013, 5, 4, 5, 5, 4)));
   }
-  
+
 }
index a968dc1..984c9df 100644 (file)
@@ -20,6 +20,8 @@ package eu.baltrad.beast.scheduler;
 
 import static org.easymock.EasyMock.expect;
 
+import java.util.Date;
+
 import org.easymock.EasyMockSupport;
 import org.junit.After;
 import org.junit.Assert;
@@ -40,7 +42,7 @@ import eu.baltrad.beast.message.mo.BltTriggerJobMessage;
  */
 public class BeastJobInvokerTest extends EasyMockSupport {
   private static interface MockMethods {
-    public BltTriggerJobMessage createMessage(String id, String name);
+    public BltTriggerJobMessage createMessage(String id, String name, Date scheduledTime, Date fireTime, Date prevTime, Date nextTime);
   };
   private JobExecutionContext ctx = null;
   private IBltMessageManager msgManager = null;
@@ -53,8 +55,8 @@ public class BeastJobInvokerTest extends EasyMockSupport {
     ctx = createMock(JobExecutionContext.class);
     msgManager = createMock(IBltMessageManager.class);
     classUnderTest = new BeastJobInvoker() {
-      protected BltTriggerJobMessage createMessage(String id, String name) {
-        return methods.createMessage(id, name);
+      protected BltTriggerJobMessage createMessage(String id, String name, Date scheduledTime, Date fireTime, Date prevTime, Date nextTime) {
+        return methods.createMessage(id, name, scheduledTime, fireTime, prevTime, nextTime);
       }
     };
   }
@@ -67,7 +69,12 @@ public class BeastJobInvokerTest extends EasyMockSupport {
   @Test
   public void testExecute() throws Exception {
     CronTrigger trigger = createMock(CronTrigger.class);
-    JobDetail detail = createMock(JobDetail.class); //new JobDetail();
+    JobDetail detail = createMock(JobDetail.class);
+    Date scheduledFireTime = new Date();
+    Date prevFireTime = new Date();
+    Date nextFireTime = new Date();
+    Date fireTime = new Date();
+    
     TriggerKey key = new TriggerKey("a.id","beast");
     JobKey jobKey = new JobKey("a.name", "beast");
     JobDataMap jobDataMap = new JobDataMap();
@@ -81,7 +88,12 @@ public class BeastJobInvokerTest extends EasyMockSupport {
     
     expect(ctx.getTrigger()).andReturn(trigger);
     expect(trigger.getKey()).andReturn(key);
-    expect(methods.createMessage("a.id","a.name")).andReturn(msg);
+    expect(ctx.getScheduledFireTime()).andReturn(scheduledFireTime);
+    expect(ctx.getFireTime()).andReturn(fireTime);
+    expect(ctx.getPreviousFireTime()).andReturn(prevFireTime);
+    expect(ctx.getNextFireTime()).andReturn(nextFireTime);
+    
+    expect(methods.createMessage("a.id","a.name", scheduledFireTime, fireTime, prevFireTime, nextFireTime)).andReturn(msg);
     
     msgManager.manage(msg);
 
@@ -95,8 +107,16 @@ public class BeastJobInvokerTest extends EasyMockSupport {
   @Test
   public void testCreateMessage() throws Exception {
     classUnderTest = new BeastJobInvoker();
-    BltTriggerJobMessage result = classUnderTest.createMessage("a.id", "a.name");
+    Date scheduledFireTime = new Date();
+    Date fireTime = new Date();
+    Date prevFireTime = new Date();
+    Date nextFireTime = new Date();
+    BltTriggerJobMessage result = classUnderTest.createMessage("a.id", "a.name", scheduledFireTime, fireTime, prevFireTime, nextFireTime);
     Assert.assertEquals("a.id", result.getId());
     Assert.assertEquals("a.name", result.getName());
+    Assert.assertSame(scheduledFireTime, result.getScheduledFireTime());
+    Assert.assertSame(fireTime, result.getFireTime());
+    Assert.assertSame(prevFireTime, result.getPrevFireTime());
+    Assert.assertSame(nextFireTime, result.getNextFireTime());
   }
 }