Skip to content

Commit

Permalink
priyanka|vijay,#nil, moved motech-delivery-tools as a module of motec…
Browse files Browse the repository at this point in the history
…h-delivery
  • Loading branch information
vijayaraj committed Dec 20, 2011
1 parent b9030a8 commit 34e3c9b
Show file tree
Hide file tree
Showing 43 changed files with 1,545 additions and 30 deletions.
32 changes: 32 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
====
MOTECH PLATFORM OPENSOURCE LICENSE AGREEMENT

Copyright (c) 2011 Grameen Foundation USA. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of Grameen Foundation USA, nor its respective contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY GRAMEEN FOUNDATION USA AND ITS CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL GRAMEEN FOUNDATION USA OR ITS CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
====

10 changes: 0 additions & 10 deletions motech-delivery-config/motech-delivery-config.iml

This file was deleted.

6 changes: 2 additions & 4 deletions motech-delivery-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
<parent>
<artifactId>motech-delivery</artifactId>
<groupId>motech-delivery</groupId>
<version>0.1</version>
<version>0.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>motech-delivery-config</artifactId>


<name>Motech Delivery Config</name>
</project>
10 changes: 0 additions & 10 deletions motech-delivery-deploy/motech-delivery-deploy.iml

This file was deleted.

6 changes: 2 additions & 4 deletions motech-delivery-deploy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
<parent>
<artifactId>motech-delivery</artifactId>
<groupId>motech-delivery</groupId>
<version>0.1</version>
<version>0.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>motech-delivery-deploy</artifactId>


<name>Motech Delivery Deploy</name>
</project>
63 changes: 63 additions & 0 deletions motech-delivery-tools/motech-datetime-simulator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>motech-delivery-tools</artifactId>
<groupId>motech-delivery</groupId>
<version>0.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>motech-datetime-simulator</artifactId>
<name>Motech DateTime Simulator</name>
<packaging>jar</packaging>

<properties>
<spring.version>3.0.5.RELEASE</spring.version>
<slf4j.version>1.6.1</slf4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<motech.version>0.3-SNAPSHOT</motech.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.motechproject</groupId>
<artifactId>motech-platform-common</artifactId>
<version>${motech.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.motechproject.deliverytools.datetimesimulator.domain;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.motechproject.util.DateUtil;
import org.motechproject.util.datetime.DateTimeSource;

public class TimeMachine implements DateTimeSource {
private String today;
private String hour;
private String minute;
private DateTimeSource dateTimeSource;

public TimeMachine(DateTimeSource dateTimeSource) {
this.dateTimeSource = dateTimeSource;
}

@Override
public DateTimeZone timeZone() {
return dateTimeSource.timeZone();
}

@Override
public DateTime now() {
DateTime dateTime = new DateTime(timeZone());
LocalDate today = today();
dateTime = dateTime.withYear(today.getYear()).withMonthOfYear(today.getMonthOfYear()).withDayOfMonth(today.getDayOfMonth());

if (StringUtils.isEmpty(hour) || StringUtils.isEmpty(minute)) return dateTime;
return dateTime.withHourOfDay(Integer.parseInt(hour)).withMinuteOfHour(Integer.parseInt(minute));
}

@Override
public LocalDate today() {
if (StringUtils.isEmpty(today)) return new LocalDate(timeZone());

LocalDate configuredDate = LocalDate.parse(today);
return new LocalDate(timeZone()).withYear(configuredDate.getYear()).withMonthOfYear(configuredDate.getMonthOfYear()).withDayOfMonth(configuredDate.getDayOfMonth());
}

public void update(String date, String hour, String minute) {
this.today = StringUtils.isEmpty(date) ? this.today : date;
this.hour = StringUtils.isEmpty(hour) ? this.hour : hour;
this.minute = StringUtils.isEmpty(minute) ? this.minute : minute;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.motechproject.deliverytools.datetimesimulator.web;

import org.apache.log4j.Logger;
import org.motechproject.deliverytools.datetimesimulator.domain.TimeMachine;
import org.motechproject.util.DateTimeSourceUtil;
import org.motechproject.util.DateUtil;
import org.motechproject.util.datetime.DefaultDateTimeSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@RequestMapping("/motech-delivery-tools/datetime")
@Controller
public class DateTimeController {
private Logger logger = Logger.getLogger(this.getClass().getName());

@RequestMapping(value = "update", method = RequestMethod.GET)
@ResponseBody
public String update(@RequestParam String date, @RequestParam String hour, @RequestParam String minute, HttpServletResponse response) {
try {
TimeMachine sourceInstance;
if (!(DateTimeSourceUtil.SourceInstance instanceof TimeMachine)) {
DateTimeSourceUtil.SourceInstance = new TimeMachine(DateTimeSourceUtil.SourceInstance);
}
sourceInstance = (TimeMachine) DateTimeSourceUtil.SourceInstance;
sourceInstance.update(date, hour, minute);
return String.format("Successfully set datetime to: %s", DateUtil.now());
} catch (Exception e) {
response.setStatus(500);
logger.error(String.format("Could not set the datetime from Date=%s, Hour=%s, Minute=%s. Did you use something like: 2011-10-17", date, hour, minute), e);
return e.toString();
}
}

@RequestMapping(value = "get", method = RequestMethod.GET)
@ResponseBody
public String get() {
return DateUtil.today().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.motechproject.deliverytools.datetimesimulator"/>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.motechproject.deliverytools.datetimesimulator.domain;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
import org.motechproject.util.datetime.DateTimeSource;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TimeMachineTest {
private TimeMachine timeMachine;

@Before
public void setUp() {
DateTimeSource dateTimeSource = mock(DateTimeSource.class);
when(dateTimeSource.timeZone()).thenReturn(DateTimeZone.UTC);
timeMachine = new TimeMachine(dateTimeSource);
}

@Test
public void whenDateAndTimeIsNotSetShouldBeToday() {
assertNotNull(timeMachine.today());
assertNotNull(timeMachine.now());
}

@Test
public void whenOnlyDateIsSetup() {
timeMachine.update("2011-10-17", "", null);
assertDate();
}

private void assertDate() {
//Joda should have used interface for getYear/month etc methods
LocalDate today = timeMachine.today();
assertNotNull(today);
assertEquals(2011, today.getYear());
assertEquals(10, today.getMonthOfYear());
assertEquals(17, today.getDayOfMonth());

DateTime now = timeMachine.now();
assertNotNull(now);
assertEquals(2011, now.getYear());
assertEquals(10, now.getMonthOfYear());
assertEquals(17, now.getDayOfMonth());
}

@Test
public void whenBothDateAndTimeIsSet() {
timeMachine.update("2011-10-17", "14", "20");
assertDate();
DateTime now = timeMachine.now();
assertEquals(14, now.getHourOfDay());
assertEquals(20, now.getMinuteOfHour());
}

@Test
public void useTheLastSetValues() {
timeMachine.update("2011-10-17", "14", "20");
timeMachine.update(null, null, null);
assertDate();
DateTime now = timeMachine.now();
assertEquals(14, now.getHourOfDay());
assertEquals(20, now.getMinuteOfHour());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.motechproject.deliverytools.datetimesimulator.web;

import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Test;
import org.motechproject.util.DateTimeSourceUtil;
import org.motechproject.util.DateUtil;
import org.motechproject.util.datetime.DateTimeConfiguration;
import org.motechproject.util.datetime.ExternalDateTimeSource;

import javax.servlet.http.HttpServletResponse;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.*;

public class DateTimeControllerTest {
private DateTimeController controller;
private HttpServletResponse servletResponse;

@Before
public void setUp() {
controller = new DateTimeController();
servletResponse = mock(HttpServletResponse.class);
}

@Test
public void invalidInput() {
controller.update("afsdff", null, null, servletResponse);
verify(servletResponse).setStatus(500);
}

@Test
public void todayIsNotSetWhenExternalDateTimeSourceIsNotSet() {
controller.update("2011-10-17", null, null, servletResponse);
verifyZeroInteractions(servletResponse);
}

@Test
public void todayIsSet() {
DateTimeConfiguration dateTimeConfiguration = mock(DateTimeConfiguration.class);
when(dateTimeConfiguration.timeZone()).thenReturn(DateTimeZone.UTC);
DateTimeSourceUtil.SourceInstance = new ExternalDateTimeSource(dateTimeConfiguration);

controller.update("2011-10-17", "", "", servletResponse);
assertEquals(10, DateUtil.today().getMonthOfYear());
assertEquals(17, DateUtil.now().getDayOfMonth());
verifyZeroInteractions(servletResponse);
}

@Test
public void setTimeWithoutDate() {
DateTimeConfiguration dateTimeConfiguration = mock(DateTimeConfiguration.class);
when(dateTimeConfiguration.timeZone()).thenReturn(DateTimeZone.UTC);
DateTimeSourceUtil.SourceInstance = new ExternalDateTimeSource(dateTimeConfiguration);

controller.update("2011-10-17", "14", "30", servletResponse);
assertEquals(10, DateUtil.today().getMonthOfYear());
assertEquals(30, DateUtil.now().getMinuteOfHour());
verifyZeroInteractions(servletResponse);
}
}
Loading

0 comments on commit 34e3c9b

Please sign in to comment.