Skip to content

Commit

Permalink
feat(hydra-dsl): explicit rdf:type expectation statement
Browse files Browse the repository at this point in the history
re #50
  • Loading branch information
tpluscode committed Jul 28, 2019
1 parent 1671363 commit 9d6195b
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ TopLevelStep:
@Override
RepresentationStep:
super |
OperationBlock
OperationBlock |
RdfTypeStatement
;

@Override
Expand All @@ -24,6 +25,10 @@ Identifier:
PrefixedName
;

RdfTypeStatement:
ExpectModifier 'Type' id=Identifier
;

NamespaceDeclaration:
'PREFIX' prefix=PrefixDeclaration namespace=URI
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import java.util.Map
import app.hypermedia.testing.dsl.hydra.HydraScenario
import org.eclipse.emf.common.util.EList
import org.eclipse.emf.ecore.EObject
import app.hypermedia.testing.dsl.hydra.RdfTypeStatement

/**
* Generates code from your model files on save.
Expand Down Expand Up @@ -79,6 +80,15 @@ final Map<String, String> _namespaces
def dispatch identifier(UriName it) {
return value
}

def dispatch step(RdfTypeStatement it) {
val map = new HashMap<String, Object>
map.put('propertyId', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
map.put('strict', true)
map.put('value', id.identifier)

return buildStatement('Property', map)
}

def dispatch identifier(PrefixedName it) {
val pair = value.split(':')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
app.hypermedia.testing.dsl.tests.generator.hydra.RdfTypeTest.expectTypeWithPrefix_generatesStep=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"propertyId": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"strict": true,
"type": "Property",
"value": "http://example.com/NewUser"
},
"empty": false
}
],
"empty": false
},
"type": "Invocation"
},
"empty": false
}
],
"empty": false
},
"operationId": "http://example.com/CreateUser",
"strict": false,
"type": "Operation"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]


app.hypermedia.testing.dsl.tests.generator.hydra.RdfTypeTest.expectType_generatesStep=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"propertyId": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"strict": true,
"type": "Property",
"value": "http://example.com/NewUser"
},
"empty": false
}
],
"empty": false
},
"type": "Invocation"
},
"empty": false
}
],
"empty": false
},
"operationId": "http://example.com/CreateUser",
"strict": false,
"type": "Operation"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package app.hypermedia.testing.dsl.tests.generator.hydra

import org.eclipse.xtext.generator.InMemoryFileSystemAccess
import org.junit.jupiter.api.^extension.ExtendWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import org.eclipse.xtext.testing.InjectWith
import com.google.inject.Inject
import org.eclipse.xtext.testing.util.ParseHelper
import app.hypermedia.testing.dsl.hydra.HydraScenario
import org.junit.jupiter.api.Test
import static io.github.jsonSnapshot.SnapshotMatcher.*
import org.eclipse.xtext.generator.IGenerator2
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.AfterAll
import org.json.JSONObject
import app.hypermedia.testing.dsl.tests.HydraInjectorProvider
import org.eclipse.xtext.generator.GeneratorContext

@ExtendWith(InjectionExtension)
@InjectWith(HydraInjectorProvider)
class RdfTypeTest {
@Inject IGenerator2 generator
@Inject extension ParseHelper<HydraScenario>
@BeforeAll
static def beforeAll() {
start()
}

@AfterAll
static def afterAll() {
validateSnapshots();
}

@Test
def expectType_generatesStep() {
// given
val model = '''
With Operation <http://example.com/CreateUser> {
Invoke {
Expect Type <http://example.com/NewUser>
}
}
'''.parse

// when
val fsa = new InMemoryFileSystemAccess()
generator.doGenerate(model.eResource, fsa, new GeneratorContext())
println(fsa.textFiles)

// then
val file = new JSONObject(fsa.textFiles.values.get(0).toString)
expect(file).toMatchSnapshot()
}

@Test
def expectTypeWithPrefix_generatesStep() {
// given
val model = '''
PREFIX ex: <http://example.com/>
With Operation ex:CreateUser {
Invoke {
Expect Type ex:NewUser
}
}
'''.parse

// when
val fsa = new InMemoryFileSystemAccess()
generator.doGenerate(model.eResource, fsa, new GeneratorContext())
println(fsa.textFiles)

// then
val file = new JSONObject(fsa.textFiles.values.get(0).toString)
expect(file).toMatchSnapshot()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* generated by Xtext 2.18.0
*/
package app.hypermedia.testing.dsl.tests.hydra

import app.hypermedia.testing.dsl.hydra.HydraScenario
import app.hypermedia.testing.dsl.tests.HydraInjectorProvider
import com.google.inject.Inject
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import org.eclipse.xtext.testing.util.ParseHelper
import org.junit.jupiter.api.^extension.ExtendWith
import org.junit.jupiter.api.Test
import static org.assertj.core.api.Assertions.*
import app.hypermedia.testing.dsl.tests.TestHelpers
import app.hypermedia.testing.dsl.hydra.RdfTypeStatement
import app.hypermedia.testing.dsl.hydra.OperationBlock

@ExtendWith(InjectionExtension)
@InjectWith(HydraInjectorProvider)
class ExpectTypeParsingTest {
@Inject extension ParseHelper<HydraScenario>

@Test
def void prefixedName_termCanContainNonLetterCharacters() {
// when
val result = '''
With Operation <http://example.com/CreateUser> {
Invoke {
Expect Type <http://example.com/NewUser>
}
}
'''.parse

// then
TestHelpers.assertModelParsedSuccessfully(result)

val operation = result.steps.get(0) as OperationBlock
val expectation = operation.invocations.get(0).response.children.get(0) as RdfTypeStatement
assertThat(expectation.id.value).isEqualTo('http://example.com/NewUser')
}
}

0 comments on commit 9d6195b

Please sign in to comment.