Skip to content

Commit a55a3c6

Browse files
committed
fix things
1 parent 09a50e7 commit a55a3c6

File tree

9 files changed

+76
-82
lines changed

9 files changed

+76
-82
lines changed

application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Grails Metadata file
2-
#Tue Dec 04 14:14:33 MST 2012
2+
#Tue Dec 04 20:56:40 MST 2012
33
app.grails.version=2.1.1
44
app.name=jsonblob
55
app.version=0.1

grails-app/conf/BuildConfig.groovy

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
grails.servlet.version = "2.5" // Change depending on target container compliance (2.5 or 3.0)
1+
grails.servlet.version = "3.0" // Change depending on target container compliance (2.5 or 3.0)
22
grails.project.class.dir = "target/classes"
33
grails.project.test.class.dir = "target/test-classes"
44
grails.project.test.reports.dir = "target/test-reports"
@@ -24,12 +24,6 @@ grails.project.dependency.resolution = {
2424

2525
mavenLocal()
2626
mavenCentral()
27-
28-
// uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
29-
//mavenRepo "http://snapshots.repository.codehaus.org"
30-
//mavenRepo "http://repository.codehaus.org"
31-
//mavenRepo "http://download.java.net/maven/2/"
32-
//mavenRepo "http://repository.jboss.com/maven2/"
3327
}
3428
dependencies {
3529
runtime "org.mongodb:mongo-java-driver:2.9.1"
@@ -39,10 +33,6 @@ grails.project.dependency.resolution = {
3933
}
4034

4135
plugins {
42-
runtime ":hibernate:$grailsVersion"
43-
44-
runtime ":jquery:1.8.3"
45-
compile ":twitter-bootstrap:2.2.1"
4636
runtime ":resources:1.2.RC2"
4737
runtime ":zipped-resources:1.0"
4838
runtime ":cached-resources:1.0"
@@ -53,14 +43,8 @@ grails.project.dependency.resolution = {
5343
excludes 'mongo-java-driver', 'gmongo'
5444
}
5545

56-
compile ':shiro:1.1.4'
57-
5846
compile ":jaxrs:0.6"
5947

6048
build ":tomcat:$grailsVersion"
61-
62-
runtime ":database-migration:1.1"
63-
64-
compile ':cache:1.0.0'
6549
}
6650
}

grails-app/conf/Config.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import grails.util.Environment
2+
13
// locations to search for config files that get merged into the main config;
24
// config files can be ConfigSlurper scripts, Java properties files, or classes
35
// in the classpath in ConfigSlurper format
@@ -77,6 +79,10 @@ log4j = {
7779
// console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
7880
//}
7981

82+
root {
83+
info()
84+
}
85+
8086
error 'org.codehaus.groovy.grails.web.servlet', // controllers
8187
'org.codehaus.groovy.grails.web.pages', // GSP
8288
'org.codehaus.groovy.grails.web.sitemesh', // layouts
@@ -89,3 +95,7 @@ log4j = {
8995
'org.hibernate',
9096
'net.sf.ehcache.hibernate'
9197
}
98+
99+
grails.app.context = "/"
100+
101+
org.grails.jaxrs.url.mappings=['/api']

grails-app/domain/jsonblob/JsonBlob.groovy

Lines changed: 0 additions & 15 deletions
This file was deleted.

grails-app/resources/jsonblob/JsonBlobCollectionResource.groovy

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,15 @@ class JsonBlobCollectionResource implements InitializingBean {
3535
}
3636
}
3737

38-
@GET
39-
Response readAll() {
40-
def allBlobs = jsonBlobResourceService.readAll()
41-
def jsonBlobs = allBlobs.collect {
42-
objectMapper.writeValueAsString(it)
43-
}
44-
Response.ok(jsonBlobs).build()
45-
}
46-
4738
@Path('/{id}')
48-
JsonBlobResource getResource(@PathParam('id') Long id) {
39+
JsonBlobResource getResource(@PathParam('id') String id) {
4940
new JsonBlobResource(jsonBlobResourceService: jsonBlobResourceService, objectMapper: objectMapper, id:id)
5041
}
5142

5243
void afterPropertiesSet() throws Exception {
5344
def om = new ObjectMapper()
5445

55-
Module jacksonMongoModule = new SimpleModule("MongoModule", new Version(1, 0, 0, null))
46+
def jacksonMongoModule = new SimpleModule("MongoModule", new Version(1, 0, 0, null))
5647
jacksonMongoModule.addSerializer(ObjectId, new JsonSerializer<ObjectId>() {
5748
@Override
5849
void serialize(ObjectId t, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {

grails-app/resources/jsonblob/JsonBlobResource.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class JsonBlobResource {
1616
def blob = jsonBlobResourceService.read(id)
1717
Response.ok(objectMapper.writeValueAsString(blob)).build()
1818
}
19-
19+
2020
@PUT
2121
Response update(String json) {
22-
def updatedBlob = jsonBlobResourceService.update(id, dto)
22+
def updatedBlob = jsonBlobResourceService.update(id, json)
2323
Response.ok(objectMapper.writeValueAsString(updatedBlob)).build()
2424
}
25-
25+
2626
@DELETE
2727
void delete() {
2828
jsonBlobResourceService.delete(id)

grails-app/services/jsonblob/JsonBlobResourceService.groovy

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package jsonblob
22

33
import com.gmongo.GMongo
4+
import com.mongodb.BasicDBObject
45
import com.mongodb.util.JSON
6+
import org.bson.types.ObjectId
57
import org.grails.jaxrs.provider.DomainObjectNotFoundException
68

79
class JsonBlobResourceService {
@@ -12,38 +14,38 @@ class JsonBlobResourceService {
1214
mongo.getDB("jsonblob").getCollection("blob")
1315
}
1416

17+
private def getDBObject(String objectId) {
18+
new BasicDBObject("_id", new ObjectId(objectId))
19+
}
20+
1521
def create(String json) {
1622
def parsed = JSON.parse(json)
1723
blobCollection().insert(parsed)
1824
parsed
1925
}
2026

2127
def read(String id) {
22-
def obj = blobCollection().findOne(_id: id)
28+
def obj = blobCollection().findOne(getDBObject(id))
2329
if (!obj) {
24-
throw new DomainObjectNotFoundException(String.class, id)
30+
throw new DomainObjectNotFoundException(ObjectId.class, id)
2531
}
2632
obj
2733
}
2834

29-
def readAll() {
30-
blobCollection().find()
31-
}
32-
3335
def update(String id, String json) {
34-
def obj = blobCollection().findOne(_id: id)
36+
def obj = blobCollection().findOne(getDBObject(id))
3537
if (!obj) {
36-
throw new DomainObjectNotFoundException(String.class, id)
38+
throw new DomainObjectNotFoundException(ObjectId.class, id)
3739
}
3840
def parsed = com.mongodb.util.JSON.parse(json)
39-
db.doctor.update([_id:id],parsed)
40-
obj
41+
blobCollection().update(getDBObject(id), parsed)
42+
parsed
4143
}
4244

4345
void delete(String id) {
44-
def obj = blobCollection().findOne(_id: id)
46+
def obj = blobCollection().findOne(getDBObject(id))
4547
if (obj) {
46-
blobCollection().remove(_id: id)
48+
blobCollection().remove(getDBObject(id))
4749
}
4850
}
4951

test/integration/jsonblob/JsonBlobApiTest.groovy

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,61 @@ class JsonBlobApiTest extends IntegrationTestCase {
1010
@Test
1111
void testPostAndGet() {
1212
def headers = ['Content-Type':'application/json', 'Accept':'application/json']
13-
def content = '{"class":"jsonblob.JsonBlob","blob":{"data": "hi"}}'
1413

15-
// create new person
16-
sendRequest('/api/jsonBlob', 'POST', headers, content.bytes)
14+
def jsonBuilder = new groovy.json.JsonBuilder()
15+
jsonBuilder.dogs {
16+
bella {
17+
breed 'bernese mountain dog'
18+
age '5'
19+
address(city: 'Denver', country: 'USA', zip: 80210)
20+
}
21+
}
22+
23+
def apiBase = '/api/jsonBlob'
24+
25+
// create new blob
26+
sendRequest(apiBase, 'POST', headers, jsonBuilder.toString().bytes)
27+
28+
def locationHeader = response.getHeader('Location')
29+
def relativePath = locationHeader.substring(locationHeader.indexOf(apiBase))
1730

1831
assertEquals(201, response.status)
1932
assertTrue(response.contentAsString.length() > 0)
2033
assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
21-
assertTrue(response.getHeader('Location') ==~ /.*\/api\/jsonBlob\/.*/)
34+
assertTrue(relativePath.startsWith("$apiBase/"))
2235

23-
// get list of persons
24-
sendRequest('/api/jsonBlob', 'GET', headers)
36+
// get the newly created blob
37+
sendRequest(relativePath, 'GET', headers)
2538

2639
assertEquals(200, response.status)
2740
assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
2841
assertTrue(response.contentAsString.length() > 0)
42+
43+
jsonBuilder.pigs {
44+
wilbur {
45+
breed 'rambunctious pig'
46+
age '2'
47+
}
48+
}
49+
50+
// update blob
51+
sendRequest(relativePath, 'PUT', headers, jsonBuilder.toString().bytes)
52+
53+
assertEquals(200, response.status)
54+
assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
55+
assertTrue(response.contentAsString.contains("pigs"))
56+
57+
// delete blob
58+
sendRequest(relativePath, 'DELETE', headers)
59+
60+
assertEquals(204, response.status)
61+
assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
62+
assertTrue(response.contentAsString == null || response.contentAsString == "")
63+
64+
// get deleted blob
65+
sendRequest(relativePath, 'GET', headers)
66+
67+
assertEquals(404, response.status)
2968
}
3069

3170
}

test/unit/jsonblob/JsonBlobTests.groovy

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)