Skip to content

Commit

Permalink
Mocks are easier to setup by running jetty:run -P mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
sfragis committed Jan 18, 2013
1 parent 84e53da commit b95ee13
Show file tree
Hide file tree
Showing 17 changed files with 1,054 additions and 36 deletions.
43 changes: 41 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Chirp Webapp</name>
<url>http://fabiostrozzi.eu</url>
<url>http://chirp-fabiostrozzi.rhcloud.com/</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.11</junit.version>
<spring.version>3.2.0.RELEASE</spring.version>
<slf4j.version>1.7.2</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<jetty.version>8.1.8.v20121106</jetty.version>
<jackson.version>1.9.11</jackson.version>
<plugin.war.version>2.3</plugin.war.version>

<!-- Jetty default web descriptor -->
<jetty.descriptor>src/main/webapp/WEB-INF/web.xml</jetty.descriptor>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
Expand Down Expand Up @@ -88,8 +95,8 @@
<version>${jackson.version}</version>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
<finalName>chirp</finalName>
<plugins>
Expand Down Expand Up @@ -117,8 +124,40 @@
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<useTestScope>true</useTestScope>
<webAppConfig>
<overrideDescriptor>${jetty.descriptor}</overrideDescriptor>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<!-- Openshift profile running Tomcat -->
<id>openshift-tomcat</id>
<build>
<plugins>
<!-- Builds ROOT.war in case of openshift+tomcat profile -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>${plugin.war.version}</version>
<configuration>
<warName>ROOT</warName>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<!-- Local profile running Jetty using mocked services -->
<profile>
<id>mocked</id>
<properties>
<jetty.descriptor>src/test/resources/jetty/mocked-web.xml</jetty.descriptor>
</properties>
</profile>

</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AuthorizationInterceptor.java, created on Jan 17, 2013
package eu.fabiostrozzi.chirp;
package eu.fabiostrozzi.chirp.rest;

import static org.springframework.util.StringUtils.hasText;

Expand All @@ -9,7 +9,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

Expand All @@ -26,7 +25,6 @@ public class AuthorizationInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(AuthorizationInterceptor.class);

@Autowired
@Qualifier("mocked")
private ChirpsService service;

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RestController.java, created on Jan 17, 2013
package eu.fabiostrozzi.chirp;
package eu.fabiostrozzi.chirp.rest;

import static org.springframework.util.StringUtils.hasText;

Expand All @@ -10,7 +10,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -31,13 +30,12 @@
* @author fabio
*/
@Controller
@RequestMapping(value = "/api", headers = "Accept=application/xml, application/json")
@RequestMapping
public class RestController {
private static final Logger log = LoggerFactory.getLogger(RestController.class);
public static final String TOKEN = "Chirp-Token";

@Autowired
@Qualifier("mocked")
private ChirpsService service;

@ResponseStatus(HttpStatus.NOT_FOUND)
Expand Down
103 changes: 84 additions & 19 deletions src/main/java/eu/fabiostrozzi/chirp/service/ChirpsServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// ChirpsServiceImpl.java, created on Jan 17, 2013
package eu.fabiostrozzi.chirp.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -17,34 +21,95 @@
public class ChirpsServiceImpl implements ChirpsService {
private static final Logger log = LoggerFactory.getLogger(ChirpsServiceImpl.class);

private List<User> users = new ArrayList<>();
private Map<String, List<Chirp>> chirpsOf = new Hashtable<>();
private Map<String, List<User>> followersOf = new Hashtable<>();
private Map<String, List<User>> following = new Hashtable<>();
private Map<String, String> tokenOf = new Hashtable<>();

public ChirpsServiceImpl() {
users.add(new User("fabio.strozzi", "Fabio", "Strozzi"));
users.add(new User("jack.sparrow", "Jack", "Sparrow"));
users.add(new User("tiger.man", "Tiger", "Man"));
users.add(new User("hulk.hogan", "Hulk", "Hogan"));
users.add(new User("ip.man", "Ip", "Man"));

tokenOf.put("fc57c1fc-60fd-11e2-8d5b-544249f16afb", "fabio.strozzi");
tokenOf.put("02392a98-60fe-11e2-8d6e-544249f16afb", "jack.sparrow");
tokenOf.put("03b60f6c-60fe-11e2-bd3f-544249f16afb", "tiger.man");
tokenOf.put("048ef8ea-60fe-11e2-ae31-544249f16afb", "hulk.hogan");
tokenOf.put("54dd989c-60fe-11e2-a0f3-544249f16afb", "ip.man");

List<User> list = new ArrayList<>();
list.add(users.get(1));
list.add(users.get(2));
list.add(users.get(3));
followersOf.put("fabio.strozzi", list);

list = new ArrayList<>();
list.add(users.get(0));
list.add(users.get(3));
list.add(users.get(4));
followersOf.put("jack.sparrow", list);

list = new ArrayList<>();
list.add(users.get(0));
followersOf.put("ip.man", list);

list = new ArrayList<>();
list.add(users.get(1));
list.add(users.get(4));
following.put("fabio.strozzi", list);

list = new ArrayList<>();
list.add(users.get(0));
list.add(users.get(3));
following.put("hulk.hogan", list);

List<Chirp> fabiosChirps = new ArrayList<>();
fabiosChirps.add(new Chirp("1", users.get(0), new Date(), "First chirp!"));
fabiosChirps.add(new Chirp("2", users.get(0), new Date(), "Second chirp!"));
fabiosChirps.add(new Chirp("3", users.get(0), new Date(), "Third chirp!"));
fabiosChirps.add(new Chirp("4", users.get(0), new Date(), "Last chirp!"));
chirpsOf.put("fabio.strozzi", fabiosChirps);

List<Chirp> jacksChirps = new ArrayList<>();
fabiosChirps.add(new Chirp("5", users.get(1), new Date(), "I'm a pirate!"));
fabiosChirps.add(new Chirp("6", users.get(1), new Date(), "I don't know where I am!"));
fabiosChirps.add(new Chirp("7", users.get(1), new Date(), "I like sailing!"));

jacksChirps.addAll(fabiosChirps);
chirpsOf.put("jack.sparrow", jacksChirps);
}

/*
* (non-Javadoc)
* @see eu.fabiostrozzi.chirp.ChirpsService#getFollowingOf(java.lang.String)
* @see eu.fabiostrozzi.chirp.service.ChirpsService#getFollowingOf(java.lang.String)
*/
@Override
public List<User> getFollowingOf(String user) {
// TODO
return null;
List<User> list = following.get(user);
return list == null ? new ArrayList<User>() : list;
}

/*
* (non-Javadoc)
* @see eu.fabiostrozzi.chirp.ChirpsService#getFollowersOf(java.lang.String)
* @see eu.fabiostrozzi.chirp.service.ChirpsService#getFollowersOf(java.lang.String)
*/
@Override
public List<User> getFollowersOf(String user) {
// TODO Auto-generated method stub
return null;
List<User> list = followersOf.get(user);
return list == null ? new ArrayList<User>() : list;
}

/*
* (non-Javadoc)
* @see eu.fabiostrozzi.chirp.ChirpsService#getChirpsOf(java.lang.String)
* @see eu.fabiostrozzi.chirp.service.ChirpsService#getChirpsOf(java.lang.String)
*/
@Override
public List<Chirp> getChirpsOf(String user) {
// TODO Auto-generated method stub
return null;
List<Chirp> chirps = chirpsOf.get(user);
return chirps == null ? new ArrayList<Chirp>() : chirps;
}

/*
Expand All @@ -54,17 +119,19 @@ public List<Chirp> getChirpsOf(String user) {
*/
@Override
public List<Chirp> searchChirpsOf(String user, String key) {
// TODO Auto-generated method stub
return null;
return getChirpsOf(user);
}

/*
* (non-Javadoc)
* @see eu.fabiostrozzi.chirp.ChirpsService#userExists(java.lang.String)
* @see eu.fabiostrozzi.chirp.service.ChirpsService#userExists(java.lang.String)
*/
@Override
public boolean userExists(String user) {
// TODO Auto-generated method stub
for (User u : users) {
if (u.getUsername().equals(user))
return true;
}
return false;
}

Expand All @@ -74,7 +141,7 @@ public boolean userExists(String user) {
*/
@Override
public void follow(String actor, String friend) {
// TODO Auto-generated method stub
log.info("User '{}' is no following '{}'", actor, friend);
}

/*
Expand All @@ -83,7 +150,7 @@ public void follow(String actor, String friend) {
*/
@Override
public void unfollow(String actor, String who) {
// TODO Auto-generated method stub
log.info("User '{}' is no longer following '{}'", actor, who);
}

/*
Expand All @@ -92,8 +159,7 @@ public void unfollow(String actor, String who) {
*/
@Override
public boolean isValidToken(String token) {
// TODO Auto-generated method stub
return false;
return tokenOf.get(token) != null;
}

/*
Expand All @@ -102,8 +168,7 @@ public boolean isValidToken(String token) {
*/
@Override
public String getUserByToken(String token) {
// TODO Auto-generated method stub
return null;
return tokenOf.get(token);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ log4j.category.eu.fabiostrozzi=DEBUG, FILE

# FILE ROLLER appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=gn-web-app.log
log4j.appender.FILE.File=chirp.log
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss,SSS} %C{1}:%M [%t] - %m%n
6 changes: 4 additions & 2 deletions src/main/webapp/WEB-INF/spring/chirp-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
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:component-scan base-package="eu.fabiostrozzi.chirp" />
<context:component-scan base-package="eu.fabiostrozzi.chirp.rest" />
<context:component-scan base-package="eu.fabiostrozzi.chirp.dao" />
<context:component-scan base-package="eu.fabiostrozzi.chirp.service" />

<mvc:annotation-driven />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/api/**" />
<bean class="eu.fabiostrozzi.chirp.AuthorizationInterceptor" autowire="byType"/>
<bean class="eu.fabiostrozzi.chirp.rest.AuthorizationInterceptor" autowire="byType"/>
</mvc:interceptor>
</mvc:interceptors>

Expand Down
5 changes: 4 additions & 1 deletion src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Chirp Web API</display-name>
<description>
Chirp is a Twitter-like APIs
</description>

<context-param>
<param-name>log4jConfigLocation</param-name>
Expand Down Expand Up @@ -46,7 +49,7 @@

<servlet-mapping>
<servlet-name>chirp-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

</web-app>
Loading

0 comments on commit b95ee13

Please sign in to comment.