Add json validation tests

This commit adds JSON structure validation tests to ensure that the
output is backward compatible with older versions if we enhance it
in the future.

Partly fixes gh-21
This commit is contained in:
Stephane Nicoll 2014-08-19 13:50:41 +02:00
parent ccbfde3b5e
commit 766a5fef89
3 changed files with 164 additions and 2 deletions

View File

@ -56,6 +56,11 @@
<artifactId>xmlunit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
@ -73,6 +78,11 @@
<artifactId>xmlunit</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -16,12 +16,19 @@
package io.spring.initializr.web
import io.spring.initializr.support.ProjectAssert
import org.junit.Test
import java.nio.charset.Charset
import io.spring.initializr.support.ProjectAssert
import org.json.JSONObject
import org.junit.Test
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import org.springframework.core.io.ClassPathResource
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.test.context.ActiveProfiles
import org.springframework.util.StreamUtils
import static org.junit.Assert.*
@ -55,6 +62,20 @@ class MainControllerIntegrationTests extends AbstractMainControllerIntegrationTe
.isGradleProject()
}
@Test // Test that the current output is exactly what we expect
void validateCurrentProjectMetadata() {
String json = restTemplate.getForObject(createUrl('/'), String.class)
JSONObject expected = readJson('1.0')
JSONAssert.assertEquals(expected, new JSONObject(json), JSONCompareMode.STRICT)
}
@Test // Test that the current code complies "at least" with 1.0
void validateProjectMetadata10() {
String json = restTemplate.getForObject(createUrl('/'), String.class)
JSONObject expected = readJson('1.0')
JSONAssert.assertEquals(expected, new JSONObject(json), JSONCompareMode.LENIENT)
}
// Existing tests for backward compatibility
@Test
@ -147,4 +168,16 @@ class MainControllerIntegrationTests extends AbstractMainControllerIntegrationTe
archiveFile
}
private static JSONObject readJson(String version) {
def resource = new ClassPathResource('metadata/test-default-' + version + '.json')
def stream = resource.getInputStream()
try {
String json = StreamUtils.copyToString(stream, Charset.forName('UTF-8'))
new JSONObject(json)
} finally {
stream.close()
}
}
}

View File

@ -0,0 +1,119 @@
{"dependencies": [
{
"name": "Core",
"content": [
{
"name": "Web",
"id": "web"
},
{
"name": "Security",
"id": "security"
},
{
"name": "Data JPA",
"id": "data-jpa"
}
]
},
{
"name": "Other",
"content": [
{
"name": "Foo",
"id": "org.acme:foo"
},
{
"name": "Bar",
"id": "org.acme:bar"
}
]
}
], "types": [
{
"name": "Maven POM",
"id": "pom.xml",
"action": "/pom.xml",
"default": false
},
{
"name": "Maven Project",
"id": "starter.zip",
"action": "/starter.zip",
"default": true
},
{
"name": "Gradle Config",
"id": "build.gradle",
"action": "/build.gradle",
"default": false
},
{
"name": "Gradle Project",
"id": "gradle.zip",
"action": "/starter.zip",
"default": false
}
], "packagings": [
{
"name": "Jar",
"id": "jar",
"default": true
},
{
"name": "War",
"id": "war",
"default": false
}
], "javaVersions": [
{
"name": "1.6",
"id": "1.6",
"default": false
},
{
"name": "1.7",
"id": "1.7",
"default": true
},
{
"name": "1.8",
"id": "1.8",
"default": false
}
], "languages": [
{
"name": "Groovy",
"id": "groovy",
"default": false
},
{
"name": "Java",
"id": "java",
"default": true
}
], "bootVersions": [
{
"name": "Latest SNAPSHOT",
"id": "1.2.0.BUILD-SNAPSHOT",
"default": false
},
{
"name": "1.1.5",
"id": "1.1.5.RELEASE",
"default": true
},
{
"name": "1.0.2",
"id": "1.0.2.RELEASE",
"default": false
}
], "defaults": {
"groupId": "org.test",
"artifactId": "demo",
"version": "0.0.1-SNAPSHOT",
"name": "demo",
"description": "Demo project for Spring Boot",
"packageName": "demo"
}
}