Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Address CatPicOfTheDay Issues in #25
Browse files Browse the repository at this point in the history
The main fix is correcting 'u' to 'p' in second matching section, around
line 260 of CatPicOfTheDayService.java.

In addition, a new stateful testing system for testing, with a sample
implementation for the CatPicOfTheService.
This contains some basic examples, and two of the examples listed in #25.
  • Loading branch information
javajawa committed May 7, 2016
1 parent d786674 commit 6473040
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public void handle(Message m)

if ( tokeniser.startsWith( "next" ) )
{
log(Level.FINER, "Processing message - next");
lastUpdated.set( Calendar.YEAR, 1971 );
synchronized( this )
{
Expand All @@ -192,13 +193,15 @@ public void handle(Message m)

if ( tokeniser.startsWith( "suggest" ) )
{
log(Level.FINER, "Processing message - suggest");
m.replyToAll( getApiCat() );
return;
}

if ( tokeniser.startsWith( "queue" ) )
{
m.reply( "Current queue lengtth: " + queue.size() );
log(Level.FINER, "Processing message - queue");
m.reply( "Current queue length: " + queue.size() );
for ( String s : queue )
{
m.reply( s );
Expand All @@ -207,16 +210,19 @@ public void handle(Message m)

if ( tokeniser.startsWith( "remove" ) )
{
log(Level.FINER, "Processing message - remove");
tokeniser.consume( "remove" );

synchronized (queue )
synchronized (queue)
{
if (queue.remove(tokeniser.toString()))
{
log(Level.FINER, "removed {0}", tokeniser.toString());
m.reply( "java.util.List.remove returned true" );
}
else
{
log(Level.FINER, "could not removed {0}", tokeniser.toString());
m.reply( "java.util.List.remove returned false" );
}
}
Expand All @@ -241,7 +247,7 @@ public void handle(Message m)

while (p.find())
{
if (!queue.contains(u.group(0)) && queue.add(p.group(0)))
if (!queue.contains(p.group(0)) && queue.add(p.group(0)))
{
m.reply(p.group(0) + " added");
}
Expand Down
184 changes: 184 additions & 0 deletions test/uk/co/harcourtprogramming/docitten/CatPicOfTheDayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package uk.co.harcourtprogramming.docitten;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import uk.co.harcourtprogramming.docitten.utility.Conversation;
import uk.co.harcourtprogramming.internetrelaycats.Message;
import uk.co.harcourtprogramming.internetrelaycats.TestingRelayCat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
* <p>Tests the parsing of messages to find web links</p>
*/
@RunWith(Parameterized.class)
public class CatPicOfTheDayTest
{
/**
* @return The parameter data for a parameterised test
*/
@Parameterized.Parameters
public static List<Conversation[]> data()
{
List<Conversation[]> conversations = new ArrayList<>(3);

conversations.add(new Conversation[] {
new Conversation("Null Test")
});

conversations.add(new Conversation[] {
new Conversation("Empty Queue Check")
.send("bob", null, "catpic queue" )
.recieve(TestingRelayCat.NAME, "bob", "Current queue length: 0")
});

conversations.add(new Conversation[] {
new Conversation("Add to Queue")
.send("bob", null, "catpic http://i.imgur.com/zZ0iGK5.png" )
.recieve(TestingRelayCat.NAME, "bob", "http://i.imgur.com/zZ0iGK5.png added")
.send("bob", null, "catpic queue" )
.recieve(TestingRelayCat.NAME, "bob", "Current queue length: 1")
.recieve(TestingRelayCat.NAME, "bob", "http://i.imgur.com/zZ0iGK5.png")
});

conversations.add(new Conversation[] {
new Conversation("Add to Queue in channel")
.send("bob", "#doc", TestingRelayCat.NAME + ": catpic http://i.imgur.com/zZ0iGK5.png" )
.recieve(TestingRelayCat.NAME, "bob", "http://i.imgur.com/zZ0iGK5.png added")
.send("bob", "#doc", TestingRelayCat.NAME + ": catpic queue" )
.recieve(TestingRelayCat.NAME, "bob", "Current queue length: 1")
.recieve(TestingRelayCat.NAME, "bob", "http://i.imgur.com/zZ0iGK5.png")
});

conversations.add(new Conversation[] {
new Conversation("Add to Queue in channel")
.send("bob", "#doc", "catpic http://i.imgur.com/zZ0iGK5.png" )
});

conversations.add(new Conversation[] {
new Conversation("Add twice to Queue")
.send("bob", null, "catpic http://i.imgur.com/zZ0iGK5.png" )
.recieve(TestingRelayCat.NAME, "bob", "http://i.imgur.com/zZ0iGK5.png added")
.send("bob", null, "catpic http://i.imgur.com/zZ0iGK5.png" )
.send("bob", null, "catpic queue" )
.recieve(TestingRelayCat.NAME, "bob", "Current queue length: 1")
.recieve(TestingRelayCat.NAME, "bob", "http://i.imgur.com/zZ0iGK5.png")
});

conversations.add(new Conversation[] {
new Conversation("Add then remove Queue")
.send("bob", null, "catpic http://i.imgur.com/zZ0iGK5.png" )
.recieve(TestingRelayCat.NAME, "bob", "http://i.imgur.com/zZ0iGK5.png added")
.send("bob", null, "catpic remove http://i.imgur.com/zZ0iGK5.png" )
.recieve(TestingRelayCat.NAME, "bob", "java.util.List.remove returned true")
.send("bob", null, "catpic queue" )
.recieve(TestingRelayCat.NAME, "bob", "Current queue length: 0")
});

conversations.add(new Conversation[] {
new Conversation("Remove Non-Existant")
.send("bob", null, "catpic remove http://i.imgur.com/zZ0iGK5.png" )
.recieve(TestingRelayCat.NAME, "bob", "java.util.List.remove returned false")
});

// From issue #25 (https://github.com/javajawa/DoCitten/issues/25)
conversations.add(new Conversation[] {
new Conversation("Add to Queue -- Issue #21, example 1")
.send("javajawa", null, "catpic add https://vine.co/v/ipJWFHrKTWY/embed/simple?audio=1" )
.recieve(TestingRelayCat.NAME, "javajawa", "https://vine.co/v/ipJWFHrKTWY/embed/simple?audio=1 added")
.send("javajawa", null, "catpic queue" )
.recieve(TestingRelayCat.NAME, "javajawa", "Current queue length: 1")
.recieve(TestingRelayCat.NAME, "javajawa", "https://vine.co/v/ipJWFHrKTWY/embed/simple?audio=1")
});

// From issue #25 (https://github.com/Edgemaster/DoCitten/issues/25)
conversations.add(new Conversation[] {
new Conversation("Add to Queue -- Issue #25, exmaple 2")
.send("Edgemaster", null, "catpic add https://scontent-lhr3-1.xx.fbcdn.net/hphotos-xfp1/v/t1.0-9/fr/cp0/e15/q65/12742637_1687632408151978_4655998892409811438_n.jpg?oh=d97f9b95c50ed8b0c806a3ef34dd2290&oe=57B2ABD7" )
.recieve(TestingRelayCat.NAME, "Edgemaster", "https://scontent-lhr3-1.xx.fbcdn.net/hphotos-xfp1/v/t1.0-9/fr/cp0/e15/q65/12742637_1687632408151978_4655998892409811438_n.jpg?oh=d97f9b95c50ed8b0c806a3ef34dd2290&oe=57B2ABD7 added")
.send("Edgemaster", null, "catpic queue" )
.recieve(TestingRelayCat.NAME, "Edgemaster", "Current queue length: 1")
.recieve(TestingRelayCat.NAME, "Edgemaster", "https://scontent-lhr3-1.xx.fbcdn.net/hphotos-xfp1/v/t1.0-9/fr/cp0/e15/q65/12742637_1687632408151978_4655998892409811438_n.jpg?oh=d97f9b95c50ed8b0c806a3ef34dd2290&oe=57B2ABD7")
});

return conversations;
}
/**
* <p>Parameter data for this test instance</p>
*/
private final Conversation conversation;

private final TestingRelayCat cat;

/**
* Create a LinkServiceTest with a given parameter
*
* @param conversation the link data for this test
*/
public CatPicOfTheDayTest(Conversation conversation)
{
this.conversation = conversation;

cat = new TestingRelayCat();
cat.addService(new CatPicOfTheDayService(cat));
}

@Test
public void testConversation()
{
Iterator<Conversation.Line> states = conversation.iterator();
int i = 0;
Conversation.Line l;

while (states.hasNext())
{
++i;
l = states.next();

switch ( l.getType() )
{
case Conversation.SEND:
assertNull("Extra message recieved at " + conversation.getName() + ':' + i, cat.getOutput());

cat.inputMessage(l.getSource(), l.getTarget(), l.getData());
break;

case Conversation.RECIEVE:
Message message = cat.getOutput();

assertNotNull("No message recieved at " + conversation.getName() + ':' + i, message);

assertEquals("Incorrect source at " + conversation.getName() + ':' + i, l.getSource(), message.getSender());
assertEquals("Incorrect target at " + conversation.getName() + ':' + i, l.getTarget(), message.getChannel());
assertEquals("Incorrect message at " + conversation.getName() + ':' + i, l.getData(), message.getMessage());

break;

case Conversation.WAIT:
int time = Integer.parseInt( l.getData() );

try
{
Thread.sleep( time );
}
catch (InterruptedException ex)
{
throw new RuntimeException("", ex);
}

break;

default:
throw new IllegalStateException();
}
}

assertNull("Extra message recieved at " + conversation.getName() + ':' + i, cat.getOutput());
}
}
109 changes: 109 additions & 0 deletions test/uk/co/harcourtprogramming/docitten/utility/Conversation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package uk.co.harcourtprogramming.docitten.utility;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* <p>
* The Conversation class represents a simple state machine
* for use in testing Service features which are stateful.
* </p>
* <p>
* Conversations are made up of multiple 'Line' which are
* either sent or received to the IRC connection in the order
* supplied, checking that the state is correctly followed.
* </p>
*/
public class Conversation implements Iterable<Conversation.Line>
{
public static final int SEND = 0;
public static final int RECIEVE = 1;

/**
* <p>The name of this conversation</p>
*/
private final String name;
/**
* <p>The set of lines that are to be run through in the state machine</p>
*/
public final List<Conversation.Line> links = new ArrayList<>(16);

public Conversation(String name)
{
this.name = name;
}

public String getName()
{
return name;
}

public Conversation send(String source, String target, String line)
{
this.links.add(new Line(SEND, source, target, line));

return this;
}

public Conversation recieve(String source, String target, String line)
{
this.links.add(new Line(RECIEVE, source, target, line));

return this;
}

@Override
public Iterator<Line> iterator()
{
return links.iterator();
}

public class Line
{
private final int type;
private final String source;
private final String target;
private final String data;

public Line( int type, String source, String target, String data )
{
this.type = type;
this.source = source;
this.target = target;
this.data = data;
}

/**
* @return the type
*/
public int getType()
{
return type;
}

/**
* @return the source
*/
public String getSource()
{
return source;
}

/**
* @return the target
*/
public String getTarget()
{
return target;
}

/**
* @return the data
*/
public String getData()
{
return data;
}
}
}

0 comments on commit 6473040

Please sign in to comment.