Permalink
Browse files

added some tests

  • Loading branch information...
jettro committed Dec 7, 2009
1 parent f9e7b92 commit a820a55dc33e4796d3ac9dc186418c8d3e4dd00c
View
@@ -1,5 +1,5 @@
#Grails Metadata file
-#Mon Nov 30 14:18:12 CET 2009
+#Fri Dec 04 08:26:37 CET 2009
app.grails.version=1.2-M4
app.name=myserverpark
app.servlet.version=2.4
@@ -1,4 +1,11 @@
+import org.joda.time.DateTime
+
class CommentController {
def scaffold = true
+ def stats = {
+ def comments = Comment.findAllByDateCreatedGreaterThan((new DateTime()).minusDays(7))
+
+ [comments:comments]
+ }
}
@@ -8,7 +8,7 @@ class Environment {
static hasMany = [servers:Server,links:Link]
static constraints = {
- name(blank:false, maxSize:50)
+ name(blank:false, maxSize:50, unique:true)
lastDeploy(blank:true)
}
@@ -15,6 +15,7 @@ class CommentService {
}
def comment = new Comment(server:server, name:name, content:content)
if (comment.save()) {
+ log.debug "new comment is created"
return comment
} else {
log.info "Error while saving a comment"
@@ -0,0 +1,20 @@
+<%@ page contentType="text/html;charset=UTF-8" %>
+<html>
+<head>
+ <title>Simple GSP page</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ <meta name="layout" content="main"/>
+ <g:javascript>
+ function addComment(e,divId) {
+ $(divId).value = e.responseText;
+ window.alert($(divId).value);
+ }
+ </g:javascript>
+
+</head>
+<body>
+ <g:each in="${comments}" var="comment">
+ ${comment.name}<br/>
+ </g:each>
+</body>
+</html>
@@ -0,0 +1,26 @@
+import grails.test.GrailsUnitTestCase
+
+class CommentServiceTests extends GrailsUnitTestCase {
+
+ void testCreateComment_NoServerId() {
+ mockLogging CommentService, true
+ mockDomain Comment
+ mockDomain Server
+
+ def commentService = new CommentService()
+ shouldFail CommentException, {
+ commentService.createComment(99l,"My server does not exist","I could type a lot of content here")
+ }
+ }
+
+ void testCreateComment() {
+ mockLogging CommentService, true
+ mockDomain Comment
+ mockDomain Server,[new Server(id:3,name:"app server",function:"serve site")]
+
+ def commentService = new CommentService()
+ def newComment = commentService.createComment(3l,"jettro","my content")
+ assertEquals "jettro",newComment.name
+ assertEquals 1, Comment.list().size()
+ }
+}
@@ -1,4 +1,5 @@
import grails.test.*
+import org.joda.time.DateTime
class EnvironmentTests extends GrailsUnitTestCase {
protected void setUp() {
@@ -9,7 +10,26 @@ class EnvironmentTests extends GrailsUnitTestCase {
super.tearDown()
}
- void testSomething() {
+ void testUniqueNameConstraint() {
+ def dev = new Environment(name:"Development",lastDeploy:new DateTime())
+ def acc = new Environment(name:"Acceptation",lastDeploy:new DateTime())
+ mockForConstraintsTests (Environment,[dev,acc])
+ def tstEnv = new Environment(name:"Development",lastDeploy:new DateTime())
+ assertFalse tstEnv.validate()
+ assertEquals "unique", tstEnv.errors["name"]
+ }
+
+ void testNameConstraints() {
+ mockForConstraintsTests (Environment)
+
+ def tstEnv = new Environment()
+ assertFalse tstEnv.validate()
+ assertEquals "nullable", tstEnv.errors["name"]
+
+ tstEnv = new Environment(name:"Nu moeten we een hele lange string tikken die meer dan" +
+ "50 karakters heeft en daardoor niet door de validatie komt")
+ assertFalse tstEnv.validate()
+ assertEquals "maxSize", tstEnv.errors["name"]
}
}
@@ -10,7 +10,7 @@ class ServerControllerTests extends ControllerUnitTestCase {
}
void testList() {
- // TODO look at bug in intellij
+
mockDomain(Server,[new Server(name:"name",function:"function")])
this.controller.list()
}
@@ -9,6 +9,29 @@ class ServerTests extends GrailsUnitTestCase {
super.tearDown()
}
- void testList() {
+ void testConstraints_noEnvironment() {
+ def server = new Server(name:"serv 1",function:"site server")
+ mockForConstraintsTests Server,[server]
+
+ assertFalse server.validate()
+ assertEquals "nullable",server.errors["environment"]
+ }
+
+ void testExternalIpValidation() {
+ assertTrue validateServer("123.123.123.123").validate()
+ assertTrue validateServer("255.123.23.123").validate()
+ assertFalse validateServer("255.123.23.1233").validate()
+ assertFalse validateServer("255.123.23").validate()
+ assertTrue validateServer("123.123.123.6").validate()
+ }
+
+ Server validateServer(externalIp) {
+ def dev = new Environment(name:"Development")
+ def okStatus = new Status(name:"OK")
+ def server = new Server(
+ name:"my server",function:"testing",externalIp:externalIp,environment:dev,status:okStatus)
+
+ mockForConstraintsTests Server, [server]
+ return server
}
}

0 comments on commit a820a55

Please sign in to comment.