From 5afbc653de3f84e806a8b355a8819b0ba18337c3 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Sun, 28 Jul 2019 16:20:39 +0200 Subject: [PATCH] feat(hydra-dsl): explicit rdf:type expectation statement re #50 --- .../app/hypermedia/testing/dsl/Hydra.xtext | 7 ++- .../dsl/generator/HydraGenerator.xtend | 10 ++++ .../tests/generator/hydra/RdfTypeTest.snap | 45 ++++++++++++++++ .../tests/generator/hydra/RdfTypeTest.xtend | 54 +++++++++++++++++++ .../tests/hydra/ExpectTypeParsingTest.xtend | 42 +++++++++++++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.snap create mode 100644 app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.xtend create mode 100644 app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectTypeParsingTest.xtend diff --git a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext index c0aafb5..e89fdb0 100644 --- a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext +++ b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/Hydra.xtext @@ -15,7 +15,8 @@ TopLevelStep: @Override RepresentationStep: super | - OperationBlock + OperationBlock | + RdfTypeStatement ; @Override @@ -24,6 +25,10 @@ Identifier: PrefixedName ; +RdfTypeStatement: + ExpectModifier 'Type' id=Identifier +; + NamespaceDeclaration: 'PREFIX' prefix=PrefixDeclaration namespace=URI ; diff --git a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend index 0cf6b39..33de922 100644 --- a/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend +++ b/app.hypermedia.testing.dsl/src/main/java/app/hypermedia/testing/dsl/generator/HydraGenerator.xtend @@ -17,6 +17,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. @@ -57,6 +58,15 @@ final Map _namespaces def dispatch identifier(UriName it) { return value } + + def dispatch step(RdfTypeStatement it) { + val map = new HashMap + map.put('propertyId', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') + map.put('strict', true) + map.put('value', id.value) + + return buildStatement('Property', map) + } def dispatch identifier(PrefixedName it) { val pair = value.split(':') diff --git a/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.snap b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.snap new file mode 100644 index 0000000..441f80c --- /dev/null +++ b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.snap @@ -0,0 +1,45 @@ +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 + } +] \ No newline at end of file diff --git a/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.xtend b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.xtend new file mode 100644 index 0000000..f51b209 --- /dev/null +++ b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/generator/hydra/RdfTypeTest.xtend @@ -0,0 +1,54 @@ +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 + @BeforeAll + static def beforeAll() { + start() + } + + @AfterAll + static def afterAll() { + validateSnapshots(); + } + + @Test + def expectType_generatesStep() { + // given + val model = ''' + With Operation { + Invoke { + Expect Type + } + } + '''.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() + } +} diff --git a/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectTypeParsingTest.xtend b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectTypeParsingTest.xtend new file mode 100644 index 0000000..92eac77 --- /dev/null +++ b/app.hypermedia.testing.dsl/src/test/java/app/hypermedia/testing/dsl/tests/hydra/ExpectTypeParsingTest.xtend @@ -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 + + @Test + def void prefixedName_termCanContainNonLetterCharacters() { + // when + val result = ''' + With Operation { + Invoke { + Expect Type + } + } + '''.parse + + // then + TestHelpers.assertModelParsedSuccessfully(result) + + val operation = result.steps.get(0) as OperationBlock + val expectation = operation.invocations.get(0).children.get(0) as RdfTypeStatement + assertThat(expectation.id.value).isEqualTo('http://example.com/NewUser') + } +}