Commits

Quinton Anderson  committed f58c756

Got the basic unit testing working well

  • Participants
  • Parent commits b46237b

Comments (0)

Files changed (5)

 		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
-			<version>3.8.1</version>
+			<version>4.11</version>
 			<scope>test</scope>
 		</dependency>
-		<dependency>
+        <dependency>
+            <groupId>org.jmock</groupId>
+            <artifactId>jmock-junit4</artifactId>
+            <version>2.5.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.jmock</groupId>
+            <artifactId>jmock-legacy</artifactId>
+            <version>2.5.1</version>
+            <scope>test</scope>
+        </dependency>		<dependency>
 			<groupId>storm</groupId>
 			<artifactId>storm</artifactId>
 			<version>0.8.1</version>
             <artifactId>commons-httpclient</artifactId>
             <version>3.1</version>
         </dependency>
+        <dependency>
+            <groupId>org.jmock</groupId>
+            <artifactId>jmock-junit3</artifactId>
+            <version>2.5.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.jmock</groupId>
+            <artifactId>jmock-junit3</artifactId>
+            <version>2.5.1</version>
+        </dependency>
     </dependencies>
 
 	<build>

File src/main/java/storm/cookbook/RepeatVisitBolt.java

 
     private void connectToRedis() {
         jedis = new Jedis(host, port);
+        jedis.connect();
+    }
+
+    public boolean isConnected(){
+        if(jedis == null)
+            return false;
+        return jedis.isConnected();
     }
 
     @Override

File src/test/java/storm/cookbook/StormTestCase.java

+package storm.cookbook;
+
+import backtype.storm.tuple.Tuple;
+import backtype.storm.tuple.Values;
+import junit.framework.TestCase;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.api.Action;
+import org.jmock.api.Expectation;
+import org.jmock.internal.ExpectationBuilder;
+import org.jmock.internal.ExpectationCollector;
+import org.jmock.lib.legacy.ClassImposteriser;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: admin
+ * Date: 2012/12/09
+ * Time: 8:50 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class StormTestCase {
+
+    protected Mockery context = new Mockery() {{
+        setImposteriser(ClassImposteriser.INSTANCE);
+    }};
+
+    protected Tuple getTuple(){
+        final Tuple tuple = context.mock(Tuple.class);
+        return tuple;
+    }
+
+
+}

File src/test/java/storm/cookbook/TestRepeatVisitBold.java

+package storm.cookbook;
+
+
+import backtype.storm.task.OutputCollector;
+import backtype.storm.tuple.Tuple;
+import backtype.storm.tuple.Values;
+import org.jmock.Expectations;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import redis.clients.jedis.Jedis;
+
+import java.util.*;
+
+import static junit.framework.TestCase.assertEquals;
+
+@RunWith(value = Parameterized.class)
+public class TestRepeatVisitBold extends StormTestCase {
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        Object[][] data = new Object[][] { { "192.168.33.100", "Client1", "myintranet.com", "false"  },
+                { "192.168.33.100", "Client1", "myintranet.com", "false" } ,
+                { "192.168.33.101", "Client2", "myintranet1.com", "true" },
+                { "192.168.33.102", "Client3", "myintranet2.com", "false"}};
+        return Arrays.asList(data);
+    }
+
+    private Jedis jedis;
+
+    private String ip;
+    private String clientKey;
+    private String url;
+    private String expected;
+
+    public TestRepeatVisitBold(String ip, String clientKey, String url, String expected){
+        this.clientKey = clientKey;
+        this.ip = ip;
+        this.url = url;
+        this.expected = expected;
+    }
+
+    @BeforeClass
+    public static void setupJedis(){
+        Jedis jedis = new Jedis("localhost",6379);
+        jedis.flushDB();
+        Iterator<Object[]> it = data().iterator();
+        while(it.hasNext()){
+            Object[] values = it.next();
+            if(values[3].equals("false")){
+                String key = values[2] + ":" + values[1];
+                jedis.set(key, "visited");//unique, meaning it must exist
+            }
+        }
+    }
+
+    @Test
+    public void testExecute(){
+        jedis = new Jedis("localhost",6379);
+        RepeatVisitBolt bolt = new RepeatVisitBolt();
+        Map config = new HashMap();
+        config.put("redis-host", "localhost");
+        config.put("redis-port", "6379");
+        final OutputCollector collector = context.mock(OutputCollector.class);
+        bolt.prepare(config, null, collector);
+
+        assertEquals(true, bolt.isConnected());
+
+        final Tuple tuple = getTuple();
+        context.checking(new Expectations(){{
+            oneOf(tuple).getStringByField(Fields.IP);will(returnValue(ip));
+            oneOf(tuple).getStringByField(Fields.CLIENT_KEY);will(returnValue(clientKey));
+            oneOf(tuple).getStringByField(Fields.URL);will(returnValue(url));
+            oneOf(collector).emit(new Values(clientKey, url, expected));
+        }});
+
+        bolt.execute(tuple);
+        context.assertIsSatisfied();
+
+        if(jedis != null)
+            jedis.disconnect();
+    }
+
+}

File src/test/java/storm/cookbook/TestVisitStatsBolt.java

+package storm.cookbook;
+
+import backtype.storm.task.OutputCollector;
+import backtype.storm.tuple.Tuple;
+import backtype.storm.tuple.Values;
+import junit.framework.TestCase;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.lib.legacy.ClassImposteriser;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+import storm.cookbook.VisitStatsBolt;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+@RunWith(value = Parameterized.class)
+public class TestVisitStatsBolt extends StormTestCase {
+
+    @Parameters
+    public static Collection<Object[]> data() {
+        Object[][] data = new Object[][] { { "true", "1" }, { "false", "0" }};
+        return Arrays.asList(data);
+    }
+
+    private String unique;
+    private int expected;
+
+    public TestVisitStatsBolt(String unique, String expected){
+        this.unique = unique;
+        this.expected = Integer.parseInt(expected);
+    }
+
+    @Test
+    public void testExecute(){
+        VisitStatsBolt bolt = new VisitStatsBolt();
+
+        final OutputCollector collector = context.mock(OutputCollector.class);
+        bolt.prepare(null, null, collector);
+
+        final Tuple tuple = getTuple();
+        context.checking(new Expectations(){{
+            atLeast(1).of(tuple).getStringByField(storm.cookbook.Fields.UNIQUE);will(returnValue(unique));
+            oneOf(collector).emit(new Values(1,expected));
+        }});
+
+        bolt.execute(tuple);
+        context.assertIsSatisfied();
+    }
+}