Skip to content

Commit

Permalink
existence tests added, mock server improved
Browse files Browse the repository at this point in the history
  • Loading branch information
ldsimonassi committed Jan 31, 2012
1 parent f77c922 commit 0986df5
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 42 deletions.
84 changes: 84 additions & 0 deletions src/test/groovy/AbstractStormTest.groovy
@@ -0,0 +1,84 @@
import groovyx.net.http.ContentType;
import org.junit.Before
import org.junit.After
import org.junit.Assert
import groovyx.net.http.RESTClient

public abstract class AbstractStormTest extends Assert {
def itemsApiClient
def searchEngineApiClient
def newsFeedApiClient

@Before
public void startLocalCluster() {
//TODO setUp the storm environment
}

@After
public void stopLocalCluster() {
//TODO shutDown
}

@Before
public void startRestClients() {
itemsApiClient = new RESTClient('http://127.0.0.1:8888')
searchEngineApiClient = new RESTClient('http://127.0.0.1:8080')
newsFeedApiClient = new RESTClient('http://127.0.0.1:9090')
clearItemsApi()
}

public void clearItemsApi() {
def resp= itemsApiClient.delete(path : "/")
assertEquals(resp.status, 200)
}


/**
* Testing Utilities...
**/
public void addItem(int id, String title, int price) {
def document = "/${id}.json"
def toSend = [:]
toSend['id'] = id
toSend['title'] = title
toSend['price'] = price

def resp= itemsApiClient.post(path : document,
body: toSend,
requestContentType: ContentType.JSON)
assertEquals(resp.status, 200)
}

public void removeItem(int id) {
def document = "/${id}.json"
def resp= itemsApiClient.delete(path : document)
assertEquals(resp.status, 200)
}


public Object readItem(int id) {
def document = "/${id}.json"
def resp = itemsApiClient.get(path:document)
assertEquals(200, resp.status)
assertEquals("${id}", "${resp.data.id}")

return resp.data
}

public Object searchApi(String query) {
def document = "/${query}.json"
def resp = searchApiClient.get(path:document)

assertEquals(200, resp.status)

return resp.data
}


public void postNew(int id) {
def document = "/${id}"
def resp = newsFeedApiClient.get(path:document)

assertEquals(200, resp.status)
}
}
29 changes: 0 additions & 29 deletions src/test/groovy/MyTest.groovy

This file was deleted.

25 changes: 25 additions & 0 deletions src/test/groovy/PreparationTest.groovy
@@ -0,0 +1,25 @@
import org.junit.Test;

class PreparationTest extends AbstractStormTest {

@Test
public void itemsApiExists(){
addItem(1, "new air conditioner with led indicator", 1500)
def resp = readItem(1)

assertEquals(resp.id, 1)
assertEquals(resp.title, "new air conditioner with led indicator")
assertEquals(resp.price, 1500)
}

@Test
public void searchEngineExists() {
def resp = searchApi('/mp3')
}

@Test
public void newsFeedExists() {
def resp = newsFeedApiClient.get(path:'/0')
assertEquales(resp.status, 200)
}
}
31 changes: 31 additions & 0 deletions src/test/groovy/SearchTest.groovy
@@ -0,0 +1,31 @@
import org.junit.Test;

public class SearchTest extends AbstractStormTest {

public void loadItems() {
}

@Test
public void searchNoData() {
}

@Test
public void searchMultiple() {

}

@Test
public void searchSingle() {
}


@Test
public void onLineAddItem() {

}

@Test
public void onLineRemoveItem() {

}
}
49 changes: 36 additions & 13 deletions src/test/node/mock-api-server.js
@@ -1,6 +1,3 @@
var root_dir= './src/test/resources'

var fs = require('fs');
var http = require('http');

var port = 8888;
Expand All @@ -10,19 +7,45 @@ if(process.argv.length>2)
else
console.log("No port provided, using: " + port);

var my_objects={}

//Create the http server
http.createServer(function (request, response) {
if(request.method=="POST") {
var data='';
request.on("data", function(chunk) {
data += chunk;
});
request.on("end", function() {
my_objects[request.url] = data;
response.writeHead(200);
response.end();
});

} else if(request.method=="DELETE") {
if(request.url == "/"){
my_objects = {} //Clear the full collection
response.writeHead(200);
response.end();
}
else {
delete my_objects[request.url]
response.writeHead(200)
response.end();
}
} else if(request.method=="GET") {
var data = my_objects[request.url];

if(data==null || typeof(data)=="undefined") {
response.writeHead(404)
response.end()
}

response.writeHead(200,{
'Content-Type' : 'application/json; charset=utf-8'
});

var file_name = root_dir + request.url;
response.end(data);

fs.readFile(file_name, function (err, data) {
if (err) {
var msg = "An error ocurred while reading file ["+file_name+"i]:"+err;
console.log(msg);
response.writeHead(500);
response.end(msg);
}
response.writeHead(200);
response.end(data);
});
}).listen(port);

0 comments on commit 0986df5

Please sign in to comment.