From 4431e313a3d4c2f9d5b626e3b8d638596ee21725 Mon Sep 17 00:00:00 2001 From: Guido Grune <100089178+gg-dim@users.noreply.github.com> Date: Wed, 6 Jul 2022 11:15:30 +0200 Subject: [PATCH 01/19] Add sonatype snapshot url --- .../resources/template/workspace.bnd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/org.gecko.emf.util.bnd.library.workspace/resources/template/workspace.bnd b/org.gecko.emf.util.bnd.library.workspace/resources/template/workspace.bnd index 484934d2..7d5a638c 100644 --- a/org.gecko.emf.util.bnd.library.workspace/resources/template/workspace.bnd +++ b/org.gecko.emf.util.bnd.library.workspace/resources/template/workspace.bnd @@ -1,7 +1,7 @@ -plugin.geckoEMFUtil: \ aQute.bnd.repository.maven.provider.MavenBndRepository;\ releaseUrl = "https://repo.maven.apache.org/maven2/,https://devel.data-in-motion.biz/nexus/repository/dim-release/"; \ - snapshotUrl = "https://devel.data-in-motion.biz/nexus/repository/dim-snapshot/";\ + snapshotUrl = "https://devel.data-in-motion.biz/nexus/repository/dim-snapshot/,https://oss.sonatype.org/content/repositories/snapshots/";\ index = "${.}/geckoEMFUtil.maven" ;\ readOnly = true;\ - name="GeckoEMF Util Dependencies" \ No newline at end of file + name="GeckoEMF Util Dependencies" From 6a12b3c38d7a473228f9112475eead258844bae7 Mon Sep 17 00:00:00 2001 From: Mark Hoffmann Date: Fri, 15 Jul 2022 20:16:49 +0200 Subject: [PATCH 02/19] #3 - set context attribute of ROOT_ELEMENT is set Signed-off-by: Mark Hoffmann --- .../emf/json/configuration/ConfigurableJsonResource.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index 800020c4..8f91cfdb 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -11,6 +11,7 @@ */ package org.gecko.emf.json.configuration; +import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.ROOT_ELEMENT; import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.RESOURCE; import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.RESOURCE_SET; @@ -216,6 +217,10 @@ protected void doLoad(InputStream inputStream, Map options) throws IOExcep ContextAttributes attributes = EMFContext.from(options).withPerCallAttribute(RESOURCE_SET, getResourceSet()) .withPerCallAttribute(RESOURCE, this); + EClass eclass = getOrDefault(options, EMFJs.OPTION_ROOT_ELEMENT, null); + if (eclass != null) { + attributes = attributes.withPerCallAttribute(ROOT_ELEMENT, eclass); + } configureMapper(options).reader().with(attributes).forType(Resource.class).withValueToUpdate(this) .readValue(inputStream); From 9a88225f6b7fce2151f28c152db5079a01740ac3 Mon Sep 17 00:00:00 2001 From: Mark Hoffmann Date: Fri, 15 Jul 2022 20:59:46 +0200 Subject: [PATCH 03/19] fixes #3 - added root_element attribute - added tests Signed-off-by: Mark Hoffmann --- org.gecko.emf.json.tests/bnd.bnd | 6 +- .../gecko/emf/json/tests/RootElementTest.java | 124 ++++++++++++++++++ org.gecko.emf.json.tests/test.bndrun | 7 +- 3 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java diff --git a/org.gecko.emf.json.tests/bnd.bnd b/org.gecko.emf.json.tests/bnd.bnd index 7a9faf5d..9b5ff3ed 100644 --- a/org.gecko.emf.json.tests/bnd.bnd +++ b/org.gecko.emf.json.tests/bnd.bnd @@ -6,4 +6,8 @@ javac.target: 11 Bundle-Version: 1.0.1.SNAPSHOT -buildpath: \ - org.eclipse.emfcloud.emfjson-jackson;version=latest \ No newline at end of file + org.eclipse.emfcloud.emfjson-jackson;version=latest,\ + org.gecko.emf.osgi.component,\ + org.eclipse.emf.ecore.xmi,\ + org.eclipse.emf.ecore,\ + org.gecko.emf.osgi.example.model.basic \ No newline at end of file diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java new file mode 100644 index 00000000..e8e8209c --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java @@ -0,0 +1,124 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.gecko.emf.json.configuration.ConfigurableJsonResource; +import org.gecko.emf.json.configuration.ConfigurableJsonResourceFactory; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.osgi.example.model.basic.BasicFactory; +import org.gecko.emf.osgi.example.model.basic.BasicPackage; +import org.gecko.emf.osgi.example.model.basic.Person; +import org.gecko.emf.osgi.example.model.basic.impl.BasicPackageImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * + * @author mark + * @since 15.07.2022 + */ +public class RootElementTest { + + private ResourceSetImpl resourceSet; + private BasicPackage packageImpl; + private BasicFactory factoryImpl; + + @BeforeEach + public void beforeEach() { + resourceSet = new ResourceSetImpl(); + resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE); + packageImpl = BasicPackageImpl.init(); + factoryImpl = BasicFactory.eINSTANCE; + resourceSet.getPackageRegistry().put(BasicPackage.eNS_URI, packageImpl); + resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("json", new ConfigurableJsonResourceFactory()); + } + + @Test + public void testSaveJson() { + Resource resource = resourceSet.createResource(URI.createURI("test.json")); + assertNotNull(resource); + assertTrue(resource instanceof ConfigurableJsonResource); + + Person p = factoryImpl.createPerson(); + p.setFirstName("Emil"); + p.setLastName("Tester"); + resource.getContents().add(p); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + resource.save(baos, null); + } catch (IOException e) { + fail("Error saving Person"); + } + String result = new String(baos.toByteArray()); + System.out.println(result); + } + + @Test + public void testLoadJsonError() { + String json = "{\n" + + " \"firstName\" : \"Emil\",\n" + + " \"lastName\" : \"Tester\"\n" + + "}"; + Resource loadResource = resourceSet.createResource(URI.createURI("test-load-error.json")); + assertNotNull(loadResource); + assertTrue(loadResource instanceof ConfigurableJsonResource); + + ByteArrayInputStream bais = new ByteArrayInputStream(json.getBytes()); + try { + loadResource.load(bais, null); + assertEquals(0, loadResource.getContents().size()); + } catch (IOException e) { + fail("Error loading Person"); + } + } + + @Test + public void testLoadJson() { + String json = "{\n" + + " \"firstName\" : \"Emil\",\n" + + " \"lastName\" : \"Tester\"\n" + + "}"; + Resource loadResource = resourceSet.createResource(URI.createURI("test-load.json")); + assertNotNull(loadResource); + assertTrue(loadResource instanceof ConfigurableJsonResource); + + ByteArrayInputStream bais = new ByteArrayInputStream(json.getBytes()); + try { + Map loadOptions = new HashMap(); + loadOptions.put(EMFJs.OPTION_ROOT_ELEMENT, packageImpl.getPerson()); + loadResource.load(bais, loadOptions); + assertEquals(1, loadResource.getContents().size()); + Person p = (Person) loadResource.getContents().get(0); + assertEquals("Emil", p.getFirstName()); + assertEquals("Tester", p.getLastName()); + } catch (IOException e) { + fail("Error loading Person"); + } + } + +} diff --git a/org.gecko.emf.json.tests/test.bndrun b/org.gecko.emf.json.tests/test.bndrun index 774cb76c..798b2a14 100644 --- a/org.gecko.emf.json.tests/test.bndrun +++ b/org.gecko.emf.json.tests/test.bndrun @@ -12,7 +12,6 @@ com.fasterxml.jackson.dataformat.jackson-dataformat-properties;version='[2.13.2,2.13.3)',\ com.fasterxml.jackson.dataformat.jackson-dataformat-yaml;version='[2.13.2,2.13.3)',\ de.undercouch.bson4jackson;version='[2.13.1,2.13.2)',\ - emfjson-jackson;version='[2.1.0,2.1.1)',\ junit-jupiter-api;version='[5.8.2,5.8.3)',\ junit-platform-commons;version='[1.8.2,1.8.3)',\ net.bytebuddy.byte-buddy;version='[1.12.8,1.12.9)',\ @@ -32,9 +31,11 @@ org.opentest4j;version='[1.2.0,1.2.1)',\ org.osgi.service.component;version='[1.4.0,1.4.1)',\ org.osgi.util.function;version='[1.1.0,1.1.1)',\ - org.osgi.util.promise;version='[1.1.1,1.1.2)',\ org.osgi.util.pushstream;version='[1.0.1,1.0.2)',\ - org.yaml.snakeyaml;version='[1.30.0,1.30.1)' + org.yaml.snakeyaml;version='[1.30.0,1.30.1)',\ + org.apache.felix.converter;version='[1.0.18,1.0.19)',\ + org.eclipse.emfcloud.emfjson-jackson;version='[2.1.0,2.1.1)',\ + org.osgi.util.promise;version='[1.2.0,1.2.1)' From f974d409dd3e188cc1dbedc88aedd23f61e67594 Mon Sep 17 00:00:00 2001 From: Mark Hoffmann Date: Fri, 15 Jul 2022 21:00:03 +0200 Subject: [PATCH 04/19] fixes #3 - added root_element attribute - added tests Signed-off-by: Mark Hoffmann --- cnf/central.mvn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cnf/central.mvn b/cnf/central.mvn index 82756dc8..2885985a 100644 --- a/cnf/central.mvn +++ b/cnf/central.mvn @@ -157,7 +157,6 @@ org.geckoprojects.bnd:org.gecko.bnd.jacoco.library:1.1.1 #org.geckoprojects.emf:org.gecko.emf.osgi.api:4.1.1.202202162308 org.geckoprojects.emf:org.gecko.emf.osgi.bnd.library.workspace:4.1.1-SNAPSHOT -org.geckoprojects.emf:org.gecko.emf.osgi.example.model.basic:4.1.1-SNAPSHOT com.fasterxml.jackson.core:jackson-core:2.13.2 com.fasterxml.jackson.core:jackson-databind:2.13.2 @@ -168,3 +167,4 @@ org.eclipse.emfcloud:emfjson-jackson:2.1.0-SNAPSHOT de.undercouch:bson4jackson:2.13.1 org.yaml:snakeyaml:1.30 #org.emfjson:emfjson-jackson:1.3.0 +org.geckoprojects.emf:org.gecko.emf.osgi.example.model.basic:4.1.1.202202162309 From 2bd8756a3692b6dec8bc83466318a92045e70924 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Fri, 22 Jul 2022 06:20:23 +0200 Subject: [PATCH 05/19] added support for multiple type package URIs in ConfigurableJsonResource Signed-off-by: Ilenia Salvadori --- org.gecko.emf.json.tests/test.bndrun | 6 ++- .../ConfigurableJsonResource.java | 42 +++++++++++++++---- .../org/gecko/emf/json/constants/EMFJs.java | 6 +-- .../emf/jaxrs/EMFJsonAnnotationConverter.java | 4 +- .../jaxrs/annotations/json/EMFJSONConfig.java | 4 +- 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/org.gecko.emf.json.tests/test.bndrun b/org.gecko.emf.json.tests/test.bndrun index 798b2a14..5e9e8d63 100644 --- a/org.gecko.emf.json.tests/test.bndrun +++ b/org.gecko.emf.json.tests/test.bndrun @@ -29,13 +29,15 @@ org.mockito.mockito-core;version='[4.4.0,4.4.1)',\ org.objenesis;version='[3.2.0,3.2.1)',\ org.opentest4j;version='[1.2.0,1.2.1)',\ - org.osgi.service.component;version='[1.4.0,1.4.1)',\ org.osgi.util.function;version='[1.1.0,1.1.1)',\ org.osgi.util.pushstream;version='[1.0.1,1.0.2)',\ org.yaml.snakeyaml;version='[1.30.0,1.30.1)',\ org.apache.felix.converter;version='[1.0.18,1.0.19)',\ org.eclipse.emfcloud.emfjson-jackson;version='[2.1.0,2.1.1)',\ - org.osgi.util.promise;version='[1.2.0,1.2.1)' + org.osgi.util.promise;version='[1.2.0,1.2.1)',\ + org.gecko.emf.bson;version=snapshot,\ + org.gecko.emf.collections;version=snapshot,\ + org.osgi.service.cm;version='[1.6.0,1.6.1)' diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index 8f91cfdb..3f71614a 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -11,18 +11,22 @@ */ package org.gecko.emf.json.configuration; -import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.ROOT_ELEMENT; import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.RESOURCE; import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.RESOURCE_SET; +import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.ROOT_ELEMENT; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.lang.reflect.Array; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.TimeZone; +import java.util.stream.Collectors; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EClass; @@ -179,12 +183,21 @@ private EcoreTypeInfo getTypeInfo(Map options, String typeField, USE typeU reader = EcoreTypeInfo.DEFAULT_VALUE_READER; writer = EcoreTypeInfo.DEFAULT_VALUE_WRITER; } - String typePackageURI = getOrDefault(options, EMFJs.OPTION_TYPE_PACKAGE_URI, null); - if (typePackageURI != null) { - EPackage ePackage = (EPackage) getResourceSet().getEObject(URI.createURI(typePackageURI), true); - reader = (value, context) -> EMFContext.findEClassByName(value, ePackage); + List typePackageURIS = getOrDefaultAsList(options, EMFJs.OPTION_TYPE_PACKAGE_URIS, Collections.emptyList()); + + List ePackages = typePackageURIS.stream().map(typePackageURI -> { + if(typePackageURI != null) { + EPackage ePackage = (EPackage) getResourceSet().getPackageRegistry().getEPackage(typePackageURI); + return ePackage; + } + return null; + }).filter(ePackage -> ePackage != null).collect(Collectors.toList()); + + reader = (value, context) -> ePackages.stream() + .map(ePackage-> { + return EMFContext.findEClassByName(value, ePackage); + }).filter(eClass -> eClass != null).findFirst().orElse(null); - } return new EcoreTypeInfo(typeField, reader, writer); } @@ -203,6 +216,21 @@ private T getOrDefault(Map options, String key, T defaultvalue) { return (T) value; } + @SuppressWarnings("unchecked") + private List getOrDefaultAsList(Map options, String key, List defaultvalue) { + Object value = options.get(key); + if(value == null) { + return (List) defaultvalue; + } + if(value instanceof List) { + return (List) value; + } + else if(value instanceof Array) { + return (List) Arrays.asList(value); + } + return List.of((T) value); + } + @Override protected void doLoad(InputStream inputStream, Map options) throws IOException { if (options == null) { @@ -223,7 +251,7 @@ protected void doLoad(InputStream inputStream, Map options) throws IOExcep } configureMapper(options).reader().with(attributes).forType(Resource.class).withValueToUpdate(this) - .readValue(inputStream); + .readValue(inputStream); } } diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/constants/EMFJs.java b/org.gecko.emf.json/src/org/gecko/emf/json/constants/EMFJs.java index 1e1d832f..1bd26324 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/constants/EMFJs.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/constants/EMFJs.java @@ -78,12 +78,12 @@ public final class EMFJs { */ public static final String OPTION_TYPE_USE = "OPTION_TYPE_USE"; /** - * Specify the package uri that will be use to denote the type of objects. + * Specify the list of package uri that will be use to denote the type of objects. *

- * By default null will be use + * By default an empty list will be used *

*/ - public static final String OPTION_TYPE_PACKAGE_URI = "OPTION_TYPE_PACKAGE_URI"; + public static final String OPTION_TYPE_PACKAGE_URIS = "OPTION_TYPE_PACKAGE_URIS"; /** * Specify the field name that will be use to denote a reference. This option is * use when a reference is created as a json object. diff --git a/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java b/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java index 6b7e9455..7228e0aa 100644 --- a/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java +++ b/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java @@ -85,8 +85,8 @@ public void convertAnnotation(Annotation annotation, boolean serialize, Map Date: Mon, 25 Jul 2022 15:48:14 +0200 Subject: [PATCH 06/19] - updated basic model dependency - updated tests to account for new basic model version - typePackageUris is now a String[] with empty array as default - update reader only if list of packages is not empty Signed-off-by: Ilenia Salvadori --- cnf/central.mvn | 2 +- .../emf/bson/tests/BsonConfiguratorTest.java | 38 +++++------- org.gecko.emf.bson.tests/test.bndrun | 18 ++++-- .../gecko/emf/json/tests/RootElementTest.java | 62 ++++++++++++------- org.gecko.emf.json.tests/test.bndrun | 6 +- .../ConfigurableJsonResource.java | 11 ++-- .../emf/jaxrs/EMFJsonAnnotationConverter.java | 2 +- .../jaxrs/annotations/json/EMFJSONConfig.java | 4 +- 8 files changed, 86 insertions(+), 57 deletions(-) diff --git a/cnf/central.mvn b/cnf/central.mvn index 2885985a..f79e02a3 100644 --- a/cnf/central.mvn +++ b/cnf/central.mvn @@ -167,4 +167,4 @@ org.eclipse.emfcloud:emfjson-jackson:2.1.0-SNAPSHOT de.undercouch:bson4jackson:2.13.1 org.yaml:snakeyaml:1.30 #org.emfjson:emfjson-jackson:1.3.0 -org.geckoprojects.emf:org.gecko.emf.osgi.example.model.basic:4.1.1.202202162309 +org.geckoprojects.emf:org.gecko.emf.osgi.example.model.basic:4.1.1-SNAPSHOT diff --git a/org.gecko.emf.bson.tests/src/org/gecko/emf/bson/tests/BsonConfiguratorTest.java b/org.gecko.emf.bson.tests/src/org/gecko/emf/bson/tests/BsonConfiguratorTest.java index 21494b7f..788173eb 100644 --- a/org.gecko.emf.bson.tests/src/org/gecko/emf/bson/tests/BsonConfiguratorTest.java +++ b/org.gecko.emf.bson.tests/src/org/gecko/emf/bson/tests/BsonConfiguratorTest.java @@ -11,6 +11,7 @@ */ package org.gecko.emf.bson.tests; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -24,9 +25,7 @@ import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.util.EcoreUtil; -import org.gecko.emf.osgi.ResourceFactoryConfigurator; import org.gecko.emf.osgi.example.model.basic.BasicFactory; import org.gecko.emf.osgi.example.model.basic.BasicPackage; import org.gecko.emf.osgi.example.model.basic.Contact; @@ -35,6 +34,7 @@ import org.gecko.emf.osgi.example.model.basic.GenderType; import org.gecko.emf.osgi.example.model.basic.Person; import org.gecko.emf.osgi.example.model.basic.util.BasicResourceFactoryImpl; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.osgi.service.cm.Configuration; @@ -52,6 +52,11 @@ @ExtendWith(BundleContextExtension.class) @ExtendWith(ServiceExtension.class) public class BsonConfiguratorTest { + + @BeforeEach + public void beforeEach(@InjectService(timeout = 2000) BasicFactory bp) { + System.out.println("Test"); + } @Test public void testBson(@InjectService ConfigurationAdmin ca) throws IOException { @@ -67,14 +72,18 @@ public void testBson(@InjectService ConfigurationAdmin ca) throws IOException { System.out.println(c.getPid());//foo~bar System.out.println(c.getFactoryPid());//foo } - + @Test - public void testBson(@InjectService(filter = "(component.name=EMFBsonConfigurator)") ServiceAware sa, @InjectService BasicFactory bf, @InjectService BasicPackage bp) { + public void testBson(@InjectService(timeout = 2000) ServiceAware rsAware, + @InjectService BasicFactory bf, + @InjectService BasicPackage bp) { + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); - System.out.println(sa.getServiceReference().getPropertyKeys()); - ResourceSet resourceSet = createResourceSet(bp); - ResourceFactoryConfigurator configurator = sa.getService(); - configurator.configureResourceFactory(resourceSet.getResourceFactoryRegistry()); + resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("test", new BasicResourceFactoryImpl()); Person p = createSamplePerson(bf); Resource xmiResource = resourceSet.createResource(URI.createURI("person.test")); @@ -154,17 +163,4 @@ private Person createSamplePerson(BasicFactory bf) { p.getContact().add(email); return p; } - - /** - * @param bp - * @return - */ - private ResourceSet createResourceSet(BasicPackage bp) { - ResourceSet resourceSet = new ResourceSetImpl(); - resourceSet.getPackageRegistry().put(BasicPackage.eNS_URI, bp); - resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("test", new BasicResourceFactoryImpl()); - resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(BasicPackage.eCONTENT_TYPE, new BasicResourceFactoryImpl()); - return resourceSet; - } - } diff --git a/org.gecko.emf.bson.tests/test.bndrun b/org.gecko.emf.bson.tests/test.bndrun index 089d2688..a90f53f8 100644 --- a/org.gecko.emf.bson.tests/test.bndrun +++ b/org.gecko.emf.bson.tests/test.bndrun @@ -1,5 +1,8 @@ -runrequires: \ - bnd.identity;id='org.gecko.emf.bson.tests' + bnd.identity;id='org.gecko.emf.bson.tests',\ + bnd.identity;id='org.apache.felix.gogo.command',\ + bnd.identity;id='org.apache.felix.gogo.runtime',\ + bnd.identity;id='org.apache.felix.gogo.shell' -runfw: org.apache.felix.framework -runee: JavaSE-11 -runbundles.junit5: ${test.runbundles} @@ -7,9 +10,7 @@ org.apache.felix.configadmin;version='[1.9.22,1.9.23)',\ org.eclipse.emf.ecore.xmi;version='[2.16.0,2.16.1)',\ org.opentest4j;version='[1.2.0,1.2.1)',\ - org.osgi.service.component;version='[1.4.0,1.4.1)',\ org.osgi.util.function;version='[1.1.0,1.1.1)',\ - org.osgi.util.promise;version='[1.1.1,1.1.2)',\ com.fasterxml.jackson.core.jackson-annotations;version='[2.13.2,2.13.3)',\ com.fasterxml.jackson.core.jackson-core;version='[2.13.2,2.13.3)',\ com.fasterxml.jackson.core.jackson-databind;version='[2.13.2,2.13.3)',\ @@ -23,7 +24,6 @@ org.gecko.emf.osgi.example.model.basic;version='[4.1.1,4.1.2)',\ com.fasterxml.jackson.dataformat.jackson-dataformat-properties;version='[2.13.2,2.13.3)',\ com.fasterxml.jackson.dataformat.jackson-dataformat-yaml;version='[2.13.2,2.13.3)',\ - emfjson-jackson;version='[2.1.0,2.1.1)',\ org.gecko.emf.bson;version=snapshot,\ org.gecko.emf.json;version=snapshot,\ org.yaml.snakeyaml;version='[1.30.0,1.30.1)',\ @@ -31,4 +31,12 @@ junit-jupiter-api;version='[5.8.2,5.8.3)',\ junit-jupiter-params;version='[5.8.2,5.8.3)',\ org.osgi.test.common;version='[1.1.0,1.1.1)',\ - org.osgi.test.junit5;version='[1.1.0,1.1.1)' \ No newline at end of file + org.osgi.test.junit5;version='[1.1.0,1.1.1)',\ + org.apache.felix.converter;version='[1.0.18,1.0.19)',\ + org.eclipse.emfcloud.emfjson-jackson;version='[2.1.0,2.1.1)',\ + org.osgi.service.cm;version='[1.6.0,1.6.1)',\ + org.osgi.util.promise;version='[1.2.0,1.2.1)',\ + org.apache.felix.gogo.command;version='[1.1.2,1.1.3)',\ + org.apache.felix.gogo.runtime;version='[1.1.4,1.1.5)',\ + org.apache.felix.gogo.shell;version='[1.1.4,1.1.5)',\ + assertj-core;version='[3.22.0,3.22.1)' \ No newline at end of file diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java index e8e8209c..907d10f8 100644 --- a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/RootElementTest.java @@ -11,6 +11,7 @@ */ package org.gecko.emf.json.tests; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -23,18 +24,16 @@ import java.util.Map; import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EcorePackage; import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.ecore.resource.ResourceSet; import org.gecko.emf.json.configuration.ConfigurableJsonResource; -import org.gecko.emf.json.configuration.ConfigurableJsonResourceFactory; import org.gecko.emf.json.constants.EMFJs; import org.gecko.emf.osgi.example.model.basic.BasicFactory; import org.gecko.emf.osgi.example.model.basic.BasicPackage; import org.gecko.emf.osgi.example.model.basic.Person; -import org.gecko.emf.osgi.example.model.basic.impl.BasicPackageImpl; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; /** * @@ -43,22 +42,20 @@ */ public class RootElementTest { - private ResourceSetImpl resourceSet; - private BasicPackage packageImpl; - private BasicFactory factoryImpl; - - @BeforeEach - public void beforeEach() { - resourceSet = new ResourceSetImpl(); - resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE); - packageImpl = BasicPackageImpl.init(); - factoryImpl = BasicFactory.eINSTANCE; - resourceSet.getPackageRegistry().put(BasicPackage.eNS_URI, packageImpl); - resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("json", new ConfigurableJsonResourceFactory()); - } - @Test - public void testSaveJson() { + public void testSaveJson(@InjectService(timeout = 2000) ServiceAware rsAware, + @InjectService(timeout = 2000) ServiceAware bfAware) { + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + assertNotNull(bfAware); + assertThat(bfAware.getServices()).hasSize(1); + BasicFactory factoryImpl = bfAware.getService(); + assertNotNull(factoryImpl); + Resource resource = resourceSet.createResource(URI.createURI("test.json")); assertNotNull(resource); assertTrue(resource instanceof ConfigurableJsonResource); @@ -78,8 +75,16 @@ public void testSaveJson() { System.out.println(result); } + + @Test - public void testLoadJsonError() { + public void testLoadJsonError(@InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + String json = "{\n" + " \"firstName\" : \"Emil\",\n" + " \"lastName\" : \"Tester\"\n" @@ -97,8 +102,21 @@ public void testLoadJsonError() { } } + @Test - public void testLoadJson() { + public void testLoadJson(@InjectService(timeout = 2000) ServiceAware rsAware, + @InjectService(timeout = 2000) ServiceAware basicPackageAware) { + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + assertNotNull(basicPackageAware); + assertThat(basicPackageAware.getServices()).hasSize(1); + BasicPackage packageImpl = basicPackageAware.getService(); + assertNotNull(packageImpl); + String json = "{\n" + " \"firstName\" : \"Emil\",\n" + " \"lastName\" : \"Tester\"\n" diff --git a/org.gecko.emf.json.tests/test.bndrun b/org.gecko.emf.json.tests/test.bndrun index 5e9e8d63..dbceee66 100644 --- a/org.gecko.emf.json.tests/test.bndrun +++ b/org.gecko.emf.json.tests/test.bndrun @@ -37,7 +37,11 @@ org.osgi.util.promise;version='[1.2.0,1.2.1)',\ org.gecko.emf.bson;version=snapshot,\ org.gecko.emf.collections;version=snapshot,\ - org.osgi.service.cm;version='[1.6.0,1.6.1)' + org.osgi.service.cm;version='[1.6.0,1.6.1)',\ + assertj-core;version='[3.22.0,3.22.1)',\ + junit-jupiter-params;version='[5.8.2,5.8.3)',\ + org.osgi.test.common;version='[1.1.0,1.1.1)',\ + org.osgi.test.junit5;version='[1.1.0,1.1.1)' diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index 3f71614a..b8a2bf9f 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -193,10 +193,13 @@ private EcoreTypeInfo getTypeInfo(Map options, String typeField, USE typeU return null; }).filter(ePackage -> ePackage != null).collect(Collectors.toList()); - reader = (value, context) -> ePackages.stream() - .map(ePackage-> { - return EMFContext.findEClassByName(value, ePackage); - }).filter(eClass -> eClass != null).findFirst().orElse(null); + if(!ePackages.isEmpty()) { + reader = (value, context) -> ePackages.stream() + .map(ePackage-> { + return EMFContext.findEClassByName(value, ePackage); + }).filter(eClass -> eClass != null).findFirst().orElse(null); + } + return new EcoreTypeInfo(typeField, reader, writer); } diff --git a/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java b/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java index 7228e0aa..53f06cea 100644 --- a/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java +++ b/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/EMFJsonAnnotationConverter.java @@ -85,7 +85,7 @@ public void convertAnnotation(Annotation annotation, boolean serialize, Map 0) { options.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, config.typePackageUris()); } } diff --git a/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/annotations/json/EMFJSONConfig.java b/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/annotations/json/EMFJSONConfig.java index b2e8a156..5a0030b7 100644 --- a/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/annotations/json/EMFJSONConfig.java +++ b/org.gecko.emf.rest/src/org/gecko/emf/jaxrs/annotations/json/EMFJSONConfig.java @@ -87,9 +87,9 @@ USE typeUSE() default USE.URI; /** - * return @see {@link EMFJs#OPTION_TYPE_PACKAGE_URIS} default is eClass + * return @see {@link EMFJs#OPTION_TYPE_PACKAGE_URIS} default is an empty array */ - String typePackageUris() default ""; + String[] typePackageUris() default {}; public enum USE { URI, From e8fb44250df6ae8608bc47e572ee4b9207072acd Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Mon, 25 Jul 2022 15:54:33 +0200 Subject: [PATCH 07/19] - removed gogo dependencies from bndrun Signed-off-by: Ilenia Salvadori --- org.gecko.emf.bson.tests/test.bndrun | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/org.gecko.emf.bson.tests/test.bndrun b/org.gecko.emf.bson.tests/test.bndrun index a90f53f8..eade835f 100644 --- a/org.gecko.emf.bson.tests/test.bndrun +++ b/org.gecko.emf.bson.tests/test.bndrun @@ -1,8 +1,4 @@ --runrequires: \ - bnd.identity;id='org.gecko.emf.bson.tests',\ - bnd.identity;id='org.apache.felix.gogo.command',\ - bnd.identity;id='org.apache.felix.gogo.runtime',\ - bnd.identity;id='org.apache.felix.gogo.shell' +-runrequires: bnd.identity;id='org.gecko.emf.bson.tests' -runfw: org.apache.felix.framework -runee: JavaSE-11 -runbundles.junit5: ${test.runbundles} @@ -36,7 +32,4 @@ org.eclipse.emfcloud.emfjson-jackson;version='[2.1.0,2.1.1)',\ org.osgi.service.cm;version='[1.6.0,1.6.1)',\ org.osgi.util.promise;version='[1.2.0,1.2.1)',\ - org.apache.felix.gogo.command;version='[1.1.2,1.1.3)',\ - org.apache.felix.gogo.runtime;version='[1.1.4,1.1.5)',\ - org.apache.felix.gogo.shell;version='[1.1.4,1.1.5)',\ assertj-core;version='[3.22.0,3.22.1)' \ No newline at end of file From b1db6772c722f8568ce1bb24d2150a8f14eedea5 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Mon, 25 Jul 2022 16:02:39 +0200 Subject: [PATCH 08/19] - fix code smells Signed-off-by: Ilenia Salvadori --- .../json/configuration/ConfigurableJsonResource.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index b8a2bf9f..744bb76b 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.TimeZone; import java.util.stream.Collectors; @@ -187,17 +188,15 @@ private EcoreTypeInfo getTypeInfo(Map options, String typeField, USE typeU List ePackages = typePackageURIS.stream().map(typePackageURI -> { if(typePackageURI != null) { - EPackage ePackage = (EPackage) getResourceSet().getPackageRegistry().getEPackage(typePackageURI); - return ePackage; + return getResourceSet().getPackageRegistry().getEPackage(typePackageURI); } return null; - }).filter(ePackage -> ePackage != null).collect(Collectors.toList()); + }).filter(Objects::nonNull).collect(Collectors.toList()); if(!ePackages.isEmpty()) { reader = (value, context) -> ePackages.stream() - .map(ePackage-> { - return EMFContext.findEClassByName(value, ePackage); - }).filter(eClass -> eClass != null).findFirst().orElse(null); + .map(ePackage-> EMFContext.findEClassByName(value, ePackage)) + .filter(Objects::nonNull).findFirst().orElse(null); } From c4458792d042dda0184966ffbf3869a94bc7df06 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Mon, 25 Jul 2022 16:10:59 +0200 Subject: [PATCH 09/19] - removed unnecessary cast Signed-off-by: Ilenia Salvadori --- .../gecko/emf/json/configuration/ConfigurableJsonResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index 744bb76b..e9d7ac03 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -222,7 +222,7 @@ private T getOrDefault(Map options, String key, T defaultvalue) { private List getOrDefaultAsList(Map options, String key, List defaultvalue) { Object value = options.get(key); if(value == null) { - return (List) defaultvalue; + return defaultvalue; } if(value instanceof List) { return (List) value; From 9d166e2514e90102a6f2942ba9b87d8a135031c9 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 06:29:46 +0200 Subject: [PATCH 10/19] added example model to test type package use options Signed-off-by: Ilenia Salvadori --- org.gecko.emf.json.tests/bnd.bnd | 9 ++- org.gecko.emf.util.example.model/.classpath | 11 +++ org.gecko.emf.util.example.model/.gitignore | 2 + org.gecko.emf.util.example.model/.project | 23 +++++++ .../org.eclipse.core.resources.prefs | 10 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++ .../.settings/org.eclipse.jdt.ui.prefs | 10 +++ org.gecko.emf.util.example.model/bnd.bnd | 20 ++++++ .../model/examplemodel.ecore | 68 +++++++++++++++++++ .../model/examplemodel.genmodel | 63 +++++++++++++++++ 10 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 org.gecko.emf.util.example.model/.classpath create mode 100644 org.gecko.emf.util.example.model/.gitignore create mode 100644 org.gecko.emf.util.example.model/.project create mode 100644 org.gecko.emf.util.example.model/.settings/org.eclipse.core.resources.prefs create mode 100644 org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.core.prefs create mode 100644 org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.ui.prefs create mode 100644 org.gecko.emf.util.example.model/bnd.bnd create mode 100644 org.gecko.emf.util.example.model/model/examplemodel.ecore create mode 100644 org.gecko.emf.util.example.model/model/examplemodel.genmodel diff --git a/org.gecko.emf.json.tests/bnd.bnd b/org.gecko.emf.json.tests/bnd.bnd index 9b5ff3ed..3b32dca1 100644 --- a/org.gecko.emf.json.tests/bnd.bnd +++ b/org.gecko.emf.json.tests/bnd.bnd @@ -10,4 +10,11 @@ Bundle-Version: 1.0.1.SNAPSHOT org.gecko.emf.osgi.component,\ org.eclipse.emf.ecore.xmi,\ org.eclipse.emf.ecore,\ - org.gecko.emf.osgi.example.model.basic \ No newline at end of file + org.gecko.emf.osgi.example.model.basic,\ + org.gecko.emf.util.example.model;version=latest + +-includeresource.resources: \ + data=data/ + +-runproperties.properties: \ + base.path=${.} \ No newline at end of file diff --git a/org.gecko.emf.util.example.model/.classpath b/org.gecko.emf.util.example.model/.classpath new file mode 100644 index 00000000..66e477cd --- /dev/null +++ b/org.gecko.emf.util.example.model/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/org.gecko.emf.util.example.model/.gitignore b/org.gecko.emf.util.example.model/.gitignore new file mode 100644 index 00000000..b1e7104c --- /dev/null +++ b/org.gecko.emf.util.example.model/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/src/ \ No newline at end of file diff --git a/org.gecko.emf.util.example.model/.project b/org.gecko.emf.util.example.model/.project new file mode 100644 index 00000000..e51d14e6 --- /dev/null +++ b/org.gecko.emf.util.example.model/.project @@ -0,0 +1,23 @@ + + + org.gecko.emf.util.example.model + + + + + + org.eclipse.jdt.core.javabuilder + + + + + bndtools.core.bndbuilder + + + + + + org.eclipse.jdt.core.javanature + bndtools.core.bndnature + + diff --git a/org.gecko.emf.util.example.model/.settings/org.eclipse.core.resources.prefs b/org.gecko.emf.util.example.model/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..a89f7959 --- /dev/null +++ b/org.gecko.emf.util.example.model/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,10 @@ +eclipse.preferences.version=1 +encoding//.settings/org.eclipse.core.resources.prefs=UTF-8 +encoding//.settings/org.eclipse.jdt.core.prefs=UTF-8 +encoding//.settings/org.eclipse.jdt.ui.prefs=UTF-8 +encoding//model/examplemodel.ecore=UTF-8 +encoding//model/examplemodel.genmodel=UTF-8 +encoding//src/test/component/Example.java=UTF-8 +encoding//test/test/component/ExampleTest.java=UTF-8 +encoding/bnd.bnd=UTF-8 +encoding/launch.bndrun=UTF-8 diff --git a/org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.core.prefs b/org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..049e42fd --- /dev/null +++ b/org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=11 diff --git a/org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.ui.prefs b/org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 00000000..25b8c0de --- /dev/null +++ b/org.gecko.emf.util.example.model/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,10 @@ +content_assist_proposals_background=255,255,255 +content_assist_proposals_foreground=0,0,0 +eclipse.preferences.version=1 +org.eclipse.jdt.ui.formatterprofiles.version=13 +org.eclipse.jdt.ui.javadoc=true +org.eclipse.jdt.ui.text.code_templates_migrated=true +org.eclipse.jdt.ui.text.custom_code_templates= +spelling_locale_initialized=true +useAnnotationsPrefPage=true +useQuickDiffPrefPage=true diff --git a/org.gecko.emf.util.example.model/bnd.bnd b/org.gecko.emf.util.example.model/bnd.bnd new file mode 100644 index 00000000..76466935 --- /dev/null +++ b/org.gecko.emf.util.example.model/bnd.bnd @@ -0,0 +1,20 @@ +# sets the usually required buildpath using the bnd library, you can extend it with the normal -buildpath to your liking +-library: enable-emf + +# The code generation takes a bit of time and makes the build a bit slower. +# It might be a good idea to put comments around it, when you don't need it +-generate:\ + model/examplemodel.genmodel;\ + generate=geckoEMF;\ + genmodel=model/examplemodel.genmodel;\ + output=src +# Add this attribute to find some logging information +# logfile=test.log;\ + +# If this is not done with a instruction namespace, bnd would think you like +# to take full control over whats exported and imported and will ignore @Export +# Bundle Annotations +-includeresource.model: model=model + +Bundle-Version: 1.0.0.SNAPSHOT + diff --git a/org.gecko.emf.util.example.model/model/examplemodel.ecore b/org.gecko.emf.util.example.model/model/examplemodel.ecore new file mode 100644 index 00000000..2d736e81 --- /dev/null +++ b/org.gecko.emf.util.example.model/model/examplemodel.ecore @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.gecko.emf.util.example.model/model/examplemodel.genmodel b/org.gecko.emf.util.example.model/model/examplemodel.genmodel new file mode 100644 index 00000000..874b7a43 --- /dev/null +++ b/org.gecko.emf.util.example.model/model/examplemodel.genmodel @@ -0,0 +1,63 @@ + + + examplemodel.ecore + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 0b09034a72f03a1de4f5e1acba904f99af030f30 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 06:30:03 +0200 Subject: [PATCH 11/19] added tests for type package use Signed-off-by: Ilenia Salvadori --- .../data/exampleTypeByClass.json | 31 +++ .../data/exampleTypeByName.json | 31 +++ .../data/exampleTypeByURI.json | 30 +++ .../org/gecko/emf/json/tests/TypeUseTest.java | 177 ++++++++++++++++++ org.gecko.emf.json.tests/test.bndrun | 7 +- 5 files changed, 273 insertions(+), 3 deletions(-) create mode 100644 org.gecko.emf.json.tests/data/exampleTypeByClass.json create mode 100644 org.gecko.emf.json.tests/data/exampleTypeByName.json create mode 100644 org.gecko.emf.json.tests/data/exampleTypeByURI.json create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java diff --git a/org.gecko.emf.json.tests/data/exampleTypeByClass.json b/org.gecko.emf.json.tests/data/exampleTypeByClass.json new file mode 100644 index 00000000..1d961b9b --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleTypeByClass.json @@ -0,0 +1,31 @@ +{ + "type": "Building", + "location": { + "type": "org.gecko.emf.util.example.model.examplemodel.Polygon", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ] + ] + } +} + diff --git a/org.gecko.emf.json.tests/data/exampleTypeByName.json b/org.gecko.emf.json.tests/data/exampleTypeByName.json new file mode 100644 index 00000000..21acd57d --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleTypeByName.json @@ -0,0 +1,31 @@ +{ + "type": "Building", + "location": { + "type": "Polygon", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ] + ] + } +} + diff --git a/org.gecko.emf.json.tests/data/exampleTypeByURI.json b/org.gecko.emf.json.tests/data/exampleTypeByURI.json new file mode 100644 index 00000000..1cdf5981 --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleTypeByURI.json @@ -0,0 +1,30 @@ +{ + "location": { + "type": "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates": [ + [ + [ + 100, + 0 + ], + [ + 101, + 0 + ], + [ + 101, + 1 + ], + [ + 100, + 1 + ], + [ + 100, + 0 + ] + ] + ] + } +} + diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java new file mode 100644 index 00000000..37148141 --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java @@ -0,0 +1,177 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emfcloud.jackson.annotations.EcoreTypeInfo; +import org.eclipse.emfcloud.jackson.databind.EMFContext; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.util.example.model.examplemodel.Building; +import org.gecko.emf.util.example.model.examplemodel.ExampleModelPackage; +import org.gecko.emf.util.example.model.examplemodel.Polygon; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; + +/** + * + * @author ilenia + * @since Jul 25, 2022 + */ +public class TypeUseTest { + + @Order(-1) + @Test + public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + } + + @Test + public void testTypeUseByName(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getLocation()).isNotNull(); + assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + } + + @Test + public void testTypeUseByClass(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByClass.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.CLASS); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getLocation()).isNotNull(); + assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + } + @Test + public void testTypeUseByURI(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByURI.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.URI); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); +// In this case we do not specify the option EMFJs.OPTION_TYPE_PACKAGE_URIS +// loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getLocation()).isNotNull(); + assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + } + +} diff --git a/org.gecko.emf.json.tests/test.bndrun b/org.gecko.emf.json.tests/test.bndrun index dbceee66..499d3422 100644 --- a/org.gecko.emf.json.tests/test.bndrun +++ b/org.gecko.emf.json.tests/test.bndrun @@ -35,15 +35,16 @@ org.apache.felix.converter;version='[1.0.18,1.0.19)',\ org.eclipse.emfcloud.emfjson-jackson;version='[2.1.0,2.1.1)',\ org.osgi.util.promise;version='[1.2.0,1.2.1)',\ - org.gecko.emf.bson;version=snapshot,\ org.gecko.emf.collections;version=snapshot,\ org.osgi.service.cm;version='[1.6.0,1.6.1)',\ assertj-core;version='[3.22.0,3.22.1)',\ junit-jupiter-params;version='[5.8.2,5.8.3)',\ org.osgi.test.common;version='[1.1.0,1.1.1)',\ - org.osgi.test.junit5;version='[1.1.0,1.1.1)' + org.osgi.test.junit5;version='[1.1.0,1.1.1)',\ + org.gecko.emf.util.example.model;version=snapshot - +-runproperties.properties: \ + base.path=${.} -runrequires: bnd.identity;id='org.gecko.emf.json.tests' -runee: JavaSE-11 \ No newline at end of file From d542bf0e8a274406d83bf4fce07894ae24106a03 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 06:31:00 +0200 Subject: [PATCH 12/19] look for package uris only in case of type use class and name Signed-off-by: Ilenia Salvadori --- .../ConfigurableJsonResource.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index e9d7ac03..0e0cb923 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -171,19 +171,37 @@ private EMFModule createInitModule(Map options, boolean isNew) { private EcoreTypeInfo getTypeInfo(Map options, String typeField, USE typeUse) { ValueReader reader; ValueWriter writer; + List ePackages; switch (typeUse) { case NAME: reader = EcoreTypeInfo.READ_BY_NAME; writer = EcoreTypeInfo.WRITE_BY_NAME; + ePackages = extractTypePackageURIs(options); + if(!ePackages.isEmpty()) { + reader = (value, context) -> ePackages.stream() + .map(ePackage-> EMFContext.findEClassByName(value, ePackage)) + .filter(Objects::nonNull).findFirst().orElse(null); + } break; case CLASS: reader = EcoreTypeInfo.READ_BY_CLASS; writer = EcoreTypeInfo.WRITE_BY_CLASS_NAME; + ePackages = extractTypePackageURIs(options); + if(!ePackages.isEmpty()) { + reader = (value, context) -> ePackages.stream() + .map(ePackage-> EMFContext.findEClassByQualifiedName(value, ePackage)) + .filter(Objects::nonNull).findFirst().orElse(null); + } break; default: reader = EcoreTypeInfo.DEFAULT_VALUE_READER; writer = EcoreTypeInfo.DEFAULT_VALUE_WRITER; - } + } + + return new EcoreTypeInfo(typeField, reader, writer); + } + + private List extractTypePackageURIs(Map options) { List typePackageURIS = getOrDefaultAsList(options, EMFJs.OPTION_TYPE_PACKAGE_URIS, Collections.emptyList()); List ePackages = typePackageURIS.stream().map(typePackageURI -> { @@ -193,14 +211,8 @@ private EcoreTypeInfo getTypeInfo(Map options, String typeField, USE typeU return null; }).filter(Objects::nonNull).collect(Collectors.toList()); - if(!ePackages.isEmpty()) { - reader = (value, context) -> ePackages.stream() - .map(ePackage-> EMFContext.findEClassByName(value, ePackage)) - .filter(Objects::nonNull).findFirst().orElse(null); - } + return ePackages; - - return new EcoreTypeInfo(typeField, reader, writer); } /** From c48608a597f42950792ed91435e805c4b7c8d15c Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 07:10:00 +0200 Subject: [PATCH 13/19] fixed array case in getOrDefaultAsList and implemented tests Signed-off-by: Ilenia Salvadori --- .../org/gecko/emf/json/tests/TypeUseTest.java | 158 ++++++++++++++++++ .../ConfigurableJsonResource.java | 14 +- 2 files changed, 164 insertions(+), 8 deletions(-) diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java index 37148141..a9c6780e 100644 --- a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java @@ -96,6 +96,84 @@ public void testTypeUseByName(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, new String[] {modelPackage.getNsURI()}); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getLocation()).isNotNull(); + assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + } + + @Test + public void testTypeUseByNameSigle(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, modelPackage.getNsURI()); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getLocation()).isNotNull(); + assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + } + @Test public void testTypeUseByClass(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { @@ -134,6 +212,86 @@ public void testTypeUseByClass(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByClass.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.CLASS); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, new String[] {modelPackage.getNsURI()}); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getLocation()).isNotNull(); + assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + } + + @Test + public void testTypeUseByClassSingle(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByClass.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.CLASS); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, modelPackage.getNsURI()); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getLocation()).isNotNull(); + assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + } + @Test public void testTypeUseByURI(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index 0e0cb923..6702997c 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -18,9 +18,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Array; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -204,15 +205,12 @@ private EcoreTypeInfo getTypeInfo(Map options, String typeField, USE typeU private List extractTypePackageURIs(Map options) { List typePackageURIS = getOrDefaultAsList(options, EMFJs.OPTION_TYPE_PACKAGE_URIS, Collections.emptyList()); - List ePackages = typePackageURIS.stream().map(typePackageURI -> { + return typePackageURIS.stream().map(typePackageURI -> { if(typePackageURI != null) { return getResourceSet().getPackageRegistry().getEPackage(typePackageURI); } return null; - }).filter(Objects::nonNull).collect(Collectors.toList()); - - return ePackages; - + }).filter(Objects::nonNull).collect(Collectors.toList()); } /** @@ -239,8 +237,8 @@ private List getOrDefaultAsList(Map options, String key, List de if(value instanceof List) { return (List) value; } - else if(value instanceof Array) { - return (List) Arrays.asList(value); + else if(value.getClass().isArray()) { + return (List) Arrays.asList((T[])value); } return List.of((T) value); } From 37d6f3df8ecd5027544bef6b19840350829ce4d9 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 07:34:23 +0200 Subject: [PATCH 14/19] added json test files to ignore list for license Signed-off-by: Ilenia Salvadori --- .licenserc.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.licenserc.yaml b/.licenserc.yaml index 3614fb22..1dcb7f0c 100755 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -38,6 +38,7 @@ header: - '**/*.basic' - '**/.classpath' - 'org.gecko.emf.osgi.codegen/src/org/gecko/emf/osgi/codegen/templates/model/*' + - 'org.gecko.emf.json.tests/data/*' - 'LICENSE' - 'Jenkinsfile' - 'NOTICE' From c2f4a6307ceed6cae6a8dfc355458dff4029de79 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 07:34:42 +0200 Subject: [PATCH 15/19] removed unused imports Signed-off-by: Ilenia Salvadori --- .../emf/json/configuration/ConfigurableJsonResource.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java index 6702997c..4576f217 100644 --- a/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java +++ b/org.gecko.emf.json/src/org/gecko/emf/json/configuration/ConfigurableJsonResource.java @@ -19,9 +19,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -238,7 +236,7 @@ private List getOrDefaultAsList(Map options, String key, List de return (List) value; } else if(value.getClass().isArray()) { - return (List) Arrays.asList((T[])value); + return Arrays.asList((T[])value); } return List.of((T) value); } From 50c14dc6ee85fd3eefe82bf72d9adbf421b233eb Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 07:35:14 +0200 Subject: [PATCH 16/19] - added test cases for null package uri -refactor tests Signed-off-by: Ilenia Salvadori --- .../org/gecko/emf/json/tests/TypeUseTest.java | 228 ++++++++++++------ 1 file changed, 148 insertions(+), 80 deletions(-) diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java index a9c6780e..990718b8 100644 --- a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/TypeUseTest.java @@ -46,28 +46,47 @@ public class TypeUseTest { public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + } + + @Test + public void testTypeUseByNameEmpty(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + checkResource(inRes); } @Test - public void testTypeUseByName(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + public void testTypeUseByNameNullURI(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); @@ -78,7 +97,7 @@ public void testTypeUseByName(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { - Building loadedObj = (Building) inRes.getContents().get(0); + checkServices(rsAware, examplePackageAware); - assertThat(loadedObj.getLocation()).isNotNull(); - assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + checkResource(inRes); } @Test public void testTypeUseByNameArray(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); @@ -125,27 +165,18 @@ public void testTypeUseByNameArray(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); @@ -164,27 +195,47 @@ public void testTypeUseByNameSigle(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { - Building loadedObj = (Building) inRes.getContents().get(0); + checkServices(rsAware, examplePackageAware); - assertThat(loadedObj.getLocation()).isNotNull(); - assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByClass.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.CLASS); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + checkResource(inRes); } @Test - public void testTypeUseByClass(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + public void testTypeUseByClassNullURI(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); @@ -195,7 +246,7 @@ public void testTypeUseByClass(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { - Building loadedObj = (Building) inRes.getContents().get(0); + checkServices(rsAware, examplePackageAware); - assertThat(loadedObj.getLocation()).isNotNull(); - assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByClass.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.CLASS); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + checkResource(inRes); } @@ -218,13 +292,11 @@ public void testTypeUseByClass(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); @@ -243,27 +315,18 @@ public void testTypeUseByClassArray(@InjectService(timeout = 2000) ServiceAware< fail("Error loading Resource! " + e); } - assertThat(inRes.getContents()).isNotEmpty(); - assertThat(inRes.getContents()).hasSize(1); - assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); - - Building loadedObj = (Building) inRes.getContents().get(0); - - assertThat(loadedObj.getLocation()).isNotNull(); - assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + checkResource(inRes); } @Test public void testTypeUseByClassSingle(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); @@ -282,27 +345,18 @@ public void testTypeUseByClassSingle(@InjectService(timeout = 2000) ServiceAware fail("Error loading Resource! " + e); } - assertThat(inRes.getContents()).isNotEmpty(); - assertThat(inRes.getContents()).hasSize(1); - assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); - - Building loadedObj = (Building) inRes.getContents().get(0); - - assertThat(loadedObj.getLocation()).isNotNull(); - assertThat(loadedObj.getLocation()).isInstanceOf(Polygon.class); + checkResource(inRes); } @Test public void testTypeUseByURI(@InjectService(timeout = 2000) ServiceAware examplePackageAware, @InjectService(timeout = 2000) ServiceAware rsAware) { - assertNotNull(examplePackageAware); - assertThat(examplePackageAware.getServices()).hasSize(1); + checkServices(rsAware, examplePackageAware); + ExampleModelPackage modelPackage = examplePackageAware.getService(); assertNotNull(modelPackage); - - assertNotNull(rsAware); - assertThat(rsAware.getServices()).hasSize(1); + ResourceSet resourceSet = rsAware.getService(); assertNotNull(resourceSet); @@ -322,6 +376,21 @@ public void testTypeUseByURI(@InjectService(timeout = 2000) ServiceAware rsAware, ServiceAware examplePackageAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + } + + private void checkResource(Resource inRes) { + assertThat(inRes.getContents()).isNotEmpty(); assertThat(inRes.getContents()).hasSize(1); assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); @@ -331,5 +400,4 @@ public void testTypeUseByURI(@InjectService(timeout = 2000) ServiceAware Date: Tue, 26 Jul 2022 10:25:00 +0200 Subject: [PATCH 17/19] - added test cases for OPTION_DATE_FORMAT - added test cases for OPTION_INDENT_OUTPUT - added test cases for OPTION_SERIALIZE_DEFAULT_VALUE - added test cases for OPTION_SERIALIZE_TYPE/OPTION_TYPE_FIELD - added test cases for OPTION_USE_ID Signed-off-by: Ilenia Salvadori --- .../data/exampleContainmentAsRefFalse.json | 7 + .../data/exampleContainmentAsRefTrue.json | 7 + .../data/exampleDateFormat.json | 5 + .../data/exampleOutputIndentFalse.json | 1 + .../data/exampleOutputIndentTrue.json | 4 + .../data/exampleSerializeDefFalse.json | 7 + .../data/exampleSerializeDefTrue.json | 8 + .../data/exampleSerializeTypeFalse.json | 5 + .../data/exampleSerializeTypeTrue.json | 7 + .../exampleSerializeTypeTrueTypeField.json | 7 + .../data/exampleUseIdFalse.json | 7 + .../data/exampleUseIdTrue.json | 9 + .../emf/json/tests/ContainmentAsRefTest.java | 175 +++++++++++++++++ .../gecko/emf/json/tests/DateFormatTest.java | 129 +++++++++++++ .../emf/json/tests/IndentOutputTest.java | 168 +++++++++++++++++ .../emf/json/tests/SerializeDefaultTest.java | 154 +++++++++++++++ .../emf/json/tests/SerializeTypeTest.java | 178 ++++++++++++++++++ .../org/gecko/emf/json/tests/UseIdTest.java | 154 +++++++++++++++ .../model/examplemodel.ecore | 30 +-- .../model/examplemodel.genmodel | 24 +-- 20 files changed, 1035 insertions(+), 51 deletions(-) create mode 100644 org.gecko.emf.json.tests/data/exampleContainmentAsRefFalse.json create mode 100644 org.gecko.emf.json.tests/data/exampleContainmentAsRefTrue.json create mode 100644 org.gecko.emf.json.tests/data/exampleDateFormat.json create mode 100644 org.gecko.emf.json.tests/data/exampleOutputIndentFalse.json create mode 100644 org.gecko.emf.json.tests/data/exampleOutputIndentTrue.json create mode 100644 org.gecko.emf.json.tests/data/exampleSerializeDefFalse.json create mode 100644 org.gecko.emf.json.tests/data/exampleSerializeDefTrue.json create mode 100644 org.gecko.emf.json.tests/data/exampleSerializeTypeFalse.json create mode 100644 org.gecko.emf.json.tests/data/exampleSerializeTypeTrue.json create mode 100644 org.gecko.emf.json.tests/data/exampleSerializeTypeTrueTypeField.json create mode 100644 org.gecko.emf.json.tests/data/exampleUseIdFalse.json create mode 100644 org.gecko.emf.json.tests/data/exampleUseIdTrue.json create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/ContainmentAsRefTest.java create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/IndentOutputTest.java create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeDefaultTest.java create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeTypeTest.java create mode 100644 org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/UseIdTest.java diff --git a/org.gecko.emf.json.tests/data/exampleContainmentAsRefFalse.json b/org.gecko.emf.json.tests/data/exampleContainmentAsRefFalse.json new file mode 100644 index 00000000..dc28695a --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleContainmentAsRefFalse.json @@ -0,0 +1,7 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "location" : { + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleContainmentAsRefTrue.json b/org.gecko.emf.json.tests/data/exampleContainmentAsRefTrue.json new file mode 100644 index 00000000..dc28695a --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleContainmentAsRefTrue.json @@ -0,0 +1,7 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "location" : { + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleDateFormat.json b/org.gecko.emf.json.tests/data/exampleDateFormat.json new file mode 100644 index 00000000..e9fcfc59 --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleDateFormat.json @@ -0,0 +1,5 @@ +{ + "type": "Building", + "startDate": "2022-07-26T08:46:53" +} + diff --git a/org.gecko.emf.json.tests/data/exampleOutputIndentFalse.json b/org.gecko.emf.json.tests/data/exampleOutputIndentFalse.json new file mode 100644 index 00000000..b744afe3 --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleOutputIndentFalse.json @@ -0,0 +1 @@ +{"eClass":"http://datainmotion.com/emf/util/examplemodel/1.0#//Building","startDate":"2022-07-26T08:46:53"} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleOutputIndentTrue.json b/org.gecko.emf.json.tests/data/exampleOutputIndentTrue.json new file mode 100644 index 00000000..1d6afbd6 --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleOutputIndentTrue.json @@ -0,0 +1,4 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "startDate" : "2022-07-26T08:46:53" +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleSerializeDefFalse.json b/org.gecko.emf.json.tests/data/exampleSerializeDefFalse.json new file mode 100644 index 00000000..dc28695a --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleSerializeDefFalse.json @@ -0,0 +1,7 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "location" : { + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleSerializeDefTrue.json b/org.gecko.emf.json.tests/data/exampleSerializeDefTrue.json new file mode 100644 index 00000000..68ead54e --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleSerializeDefTrue.json @@ -0,0 +1,8 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "type" : "Building", + "location" : { + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleSerializeTypeFalse.json b/org.gecko.emf.json.tests/data/exampleSerializeTypeFalse.json new file mode 100644 index 00000000..d18cfb9c --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleSerializeTypeFalse.json @@ -0,0 +1,5 @@ +{ + "location" : { + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleSerializeTypeTrue.json b/org.gecko.emf.json.tests/data/exampleSerializeTypeTrue.json new file mode 100644 index 00000000..dc28695a --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleSerializeTypeTrue.json @@ -0,0 +1,7 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "location" : { + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleSerializeTypeTrueTypeField.json b/org.gecko.emf.json.tests/data/exampleSerializeTypeTrueTypeField.json new file mode 100644 index 00000000..f21b8ae6 --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleSerializeTypeTrueTypeField.json @@ -0,0 +1,7 @@ +{ + "new_type_field" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "location" : { + "new_type_field" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleUseIdFalse.json b/org.gecko.emf.json.tests/data/exampleUseIdFalse.json new file mode 100644 index 00000000..dc28695a --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleUseIdFalse.json @@ -0,0 +1,7 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "location" : { + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/data/exampleUseIdTrue.json b/org.gecko.emf.json.tests/data/exampleUseIdTrue.json new file mode 100644 index 00000000..d0d359f9 --- /dev/null +++ b/org.gecko.emf.json.tests/data/exampleUseIdTrue.json @@ -0,0 +1,9 @@ +{ + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Building", + "@id" : null, + "location" : { + "eClass" : "http://datainmotion.com/emf/util/examplemodel/1.0#//Polygon", + "@id" : null, + "coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] + } +} \ No newline at end of file diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/ContainmentAsRefTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/ContainmentAsRefTest.java new file mode 100644 index 00000000..2c3d1ace --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/ContainmentAsRefTest.java @@ -0,0 +1,175 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emfcloud.jackson.annotations.EcoreTypeInfo; +import org.eclipse.emfcloud.jackson.databind.EMFContext; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.util.example.model.examplemodel.Building; +import org.gecko.emf.util.example.model.examplemodel.ExampleModelPackage; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; + +/** + * + * @author ilenia + * @since Jul 26, 2022 + */ +public class ContainmentAsRefTest { + + @Order(-1) + @Test + public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + } + + @Disabled("Setting this option has no effect currently, as all the containemnet references are output as complete objects and not as references") + @Test + public void testContainementAsRefFalse(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotNull(); + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_SERIALIZE_CONTAINMENT_AS_HREF, false); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleContainmentAsRefFalse.json"; + Resource outRes = resourceSet.createResource(URI.createFileURI(pathToJsonOutputFile)); + outRes.getContents().add(loadedObj); + + try { + outRes.save(saveOptions); + } catch (Exception e) { + fail("Error saving Resource! " + e); + } + +// try (FileInputStream fis = new FileInputStream(pathToJsonOutputFile);) { +// byte[] bytes = fis.readAllBytes(); +// String str = new String(bytes); +// assertThat(str.contains("\t")); +// } catch (IOException e) { +// fail("Error reading File! " + e); +// } + } + + @Disabled("Setting this option has no effect currently, as all the containemnet references are output as complete objects and not as references") + @Test + public void testContainementAsRefTrue(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotNull(); + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_SERIALIZE_CONTAINMENT_AS_HREF, true); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleContainmentAsRefTrue.json"; + Resource outRes = resourceSet.createResource(URI.createFileURI(pathToJsonOutputFile)); + outRes.getContents().add(loadedObj); + + try { + outRes.save(saveOptions); + } catch (Exception e) { + fail("Error saving Resource! " + e); + } + +// try (FileInputStream fis = new FileInputStream(pathToJsonOutputFile);) { +// byte[] bytes = fis.readAllBytes(); +// String str = new String(bytes); +// assertThat(str.contains("\t")); +// } catch (IOException e) { +// fail("Error reading File! " + e); +// } + } + + private void checkServices(ServiceAware rsAware, ServiceAware examplePackageAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + } + +} diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java new file mode 100644 index 00000000..e977a195 --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java @@ -0,0 +1,129 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emfcloud.jackson.databind.EMFContext; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.util.example.model.examplemodel.Building; +import org.gecko.emf.util.example.model.examplemodel.ExampleModelPackage; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; + +/** + * + * @author ilenia + * @since Jul 26, 2022 + */ +public class DateFormatTest { + + @Order(-1) + @Test + public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + } + + @Test + public void testDateFormatOnlyDate(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleDateFormat.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + String dateFormat = "yyyy-MM-dd"; + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_DATE_FORMAT, dateFormat); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + checkResource(inRes, "Tue Jul 26 00:00:00 CEST 2022"); + } + + @Test + public void testDateFormatDateTime(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleDateFormat.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + String dateFormat = "yyyy-MM-dd'T'HH:mm:ss"; + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_DATE_FORMAT, dateFormat); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + checkResource(inRes, "Tue Jul 26 08:46:53 CEST 2022"); + } + + private void checkServices(ServiceAware rsAware, ServiceAware examplePackageAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + } + + private void checkResource(Resource inRes, String expectedResult) { + + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + assertThat(loadedObj.getStartDate()).isNotNull(); + assertThat(loadedObj.getStartDate().toString()).isEqualTo(expectedResult); + } + + +} diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/IndentOutputTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/IndentOutputTest.java new file mode 100644 index 00000000..5c4d65be --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/IndentOutputTest.java @@ -0,0 +1,168 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emfcloud.jackson.databind.EMFContext; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.util.example.model.examplemodel.Building; +import org.gecko.emf.util.example.model.examplemodel.ExampleModelPackage; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; + +/** + * + * @author ilenia + * @since Jul 26, 2022 + */ +public class IndentOutputTest { + + @Order(-1) + @Test + public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + } + + @Test + public void testIndentOutputTrue(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleDateFormat.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotNull(); + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_INDENT_OUTPUT, true); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleOutputIndentTrue.json"; + Resource outRes = resourceSet.createResource(URI.createFileURI(pathToJsonOutputFile)); + outRes.getContents().add(loadedObj); + + try { + outRes.save(saveOptions); + } catch (Exception e) { + fail("Error saving Resource! " + e); + } + + try (FileInputStream fis = new FileInputStream(pathToJsonOutputFile);) { + byte[] bytes = fis.readAllBytes(); + String str = new String(bytes); + assertThat(str.contains("\t")); + } catch (IOException e) { + fail("Error reading File! " + e); + } + } + + @Test + public void testIndentOutputFalse(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleDateFormat.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotNull(); + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + Building loadedObj = (Building) inRes.getContents().get(0); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_INDENT_OUTPUT, false); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleOutputIndentFalse.json"; + Resource outRes = resourceSet.createResource(URI.createFileURI(pathToJsonOutputFile)); + outRes.getContents().add(loadedObj); + + try { + outRes.save(saveOptions); + } catch (Exception e) { + fail("Error saving Resource! " + e); + } + + try (FileInputStream fis = new FileInputStream(pathToJsonOutputFile);) { + byte[] bytes = fis.readAllBytes(); + String str = new String(bytes); + assertThat(str.contains("\t")).isFalse(); + } catch (IOException e) { + fail("Error reading File! " + e); + } + } + + private void checkServices(ServiceAware rsAware, ServiceAware examplePackageAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + + + } + +} diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeDefaultTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeDefaultTest.java new file mode 100644 index 00000000..d6225d0d --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeDefaultTest.java @@ -0,0 +1,154 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emfcloud.jackson.annotations.EcoreTypeInfo; +import org.eclipse.emfcloud.jackson.databind.EMFContext; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.util.example.model.examplemodel.Building; +import org.gecko.emf.util.example.model.examplemodel.ExampleModelPackage; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; + +/** + * + * @author ilenia + * @since Jul 26, 2022 + */ +public class SerializeDefaultTest { + + @Order(-1) + @Test + public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + } + + @Test + public void testSerializeDefaultTrue(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + Building loadedObj = getLoadedObject(resourceSet, modelPackage); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_SERIALIZE_DEFAULT_VALUE, true); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleSerializeDefTrue.json"; + save(resourceSet, saveOptions, pathToJsonOutputFile, loadedObj); + + check(pathToJsonOutputFile, str -> str.contains("\"type\": \"Building\"")); + } + + @Test + public void testSerializeDefaultFalse(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + Building loadedObj = getLoadedObject(resourceSet, modelPackage); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_SERIALIZE_DEFAULT_VALUE, false); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleSerializeDefFalse.json"; + save(resourceSet, saveOptions, pathToJsonOutputFile, loadedObj); + + check(pathToJsonOutputFile, str -> !str.contains("\"type\": \"Building\"")); + } + + private Building getLoadedObject(ResourceSet resourceSet, ExampleModelPackage modelPackage) { + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotNull(); + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + return (Building) inRes.getContents().get(0); + } + + private void save(ResourceSet resourceSet, Map saveOptions, String pathToJsonOutputFile, Building loadedObj) { + Resource outRes = resourceSet.createResource(URI.createFileURI(pathToJsonOutputFile)); + outRes.getContents().add(loadedObj); + + try { + outRes.save(saveOptions); + } catch (Exception e) { + fail("Error saving Resource! " + e); + } + } + + private void check(String pathToJsonOutputFile, Predicate predicate) { + try (FileInputStream fis = new FileInputStream(pathToJsonOutputFile);) { + byte[] bytes = fis.readAllBytes(); + String str = new String(bytes); + assertThat(predicate.test(str)); + } catch (IOException e) { + fail("Error reading File! " + e); + } + } + + private void checkServices(ServiceAware rsAware, ServiceAware examplePackageAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + } +} diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeTypeTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeTypeTest.java new file mode 100644 index 00000000..baf93bfc --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/SerializeTypeTest.java @@ -0,0 +1,178 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emfcloud.jackson.annotations.EcoreTypeInfo; +import org.eclipse.emfcloud.jackson.databind.EMFContext; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.util.example.model.examplemodel.Building; +import org.gecko.emf.util.example.model.examplemodel.ExampleModelPackage; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; + +/** + * + * @author ilenia + * @since Jul 26, 2022 + */ +public class SerializeTypeTest { + + @Order(-1) + @Test + public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + } + + @Test + public void testSerializeTypeTrue(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + Building loadedObj = getLoadedObject(resourceSet, modelPackage); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_SERIALIZE_TYPE, true); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleSerializeTypeTrue.json"; + save(resourceSet, saveOptions, pathToJsonOutputFile, loadedObj); + + check(pathToJsonOutputFile, str -> str.contains("\"eClass\":")); + } + + @Test + public void testSerializeTypeTrueTypeField(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + Building loadedObj = getLoadedObject(resourceSet, modelPackage); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_SERIALIZE_TYPE, true); + saveOptions.put(EMFJs.OPTION_TYPE_FIELD, "new_type_field"); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleSerializeTypeTrueTypeField.json"; + save(resourceSet, saveOptions, pathToJsonOutputFile, loadedObj); + + check(pathToJsonOutputFile, str -> str.contains("\"new_type_field\":")); + } + + @Test + public void testSerializeTypeFalse(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + Building loadedObj = getLoadedObject(resourceSet, modelPackage); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_SERIALIZE_TYPE, false); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleSerializeTypeFalse.json"; + save(resourceSet, saveOptions, pathToJsonOutputFile, loadedObj); + + check(pathToJsonOutputFile, str -> !str.contains("\"eClass\":")); + } + + private Building getLoadedObject(ResourceSet resourceSet, ExampleModelPackage modelPackage) { + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotNull(); + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + return (Building) inRes.getContents().get(0); + } + + private void save(ResourceSet resourceSet, Map saveOptions, String pathToJsonOutputFile, Building loadedObj) { + Resource outRes = resourceSet.createResource(URI.createFileURI(pathToJsonOutputFile)); + outRes.getContents().add(loadedObj); + + try { + outRes.save(saveOptions); + } catch (Exception e) { + fail("Error saving Resource! " + e); + } + } + + private void check(String pathToJsonOutputFile, Predicate predicate) { + try (FileInputStream fis = new FileInputStream(pathToJsonOutputFile);) { + byte[] bytes = fis.readAllBytes(); + String str = new String(bytes); + assertThat(predicate.test(str)); + } catch (IOException e) { + fail("Error reading File! " + e); + } + } + + private void checkServices(ServiceAware rsAware, ServiceAware examplePackageAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + } +} diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/UseIdTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/UseIdTest.java new file mode 100644 index 00000000..f46dcdf7 --- /dev/null +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/UseIdTest.java @@ -0,0 +1,154 @@ +/** + * Copyright (c) 2012 - 2022 Data In Motion and others. + * All rights reserved. + * + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Data In Motion - initial API and implementation + */ +package org.gecko.emf.json.tests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emfcloud.jackson.annotations.EcoreTypeInfo; +import org.eclipse.emfcloud.jackson.databind.EMFContext; +import org.gecko.emf.json.constants.EMFJs; +import org.gecko.emf.util.example.model.examplemodel.Building; +import org.gecko.emf.util.example.model.examplemodel.ExampleModelPackage; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.osgi.test.common.annotation.InjectService; +import org.osgi.test.common.service.ServiceAware; + +/** + * + * @author ilenia + * @since Jul 26, 2022 + */ +public class UseIdTest { + + @Order(-1) + @Test + public void testServices(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + } + + @Test + public void testUseIdTrue(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + Building loadedObj = getLoadedObject(resourceSet, modelPackage); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_USE_ID, true); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleUseIdTrue.json"; + save(resourceSet, saveOptions, pathToJsonOutputFile, loadedObj); + + check(pathToJsonOutputFile, str -> str.contains("\"_id\":")); + } + + @Test + public void testUseIdFalse(@InjectService(timeout = 2000) ServiceAware examplePackageAware, + @InjectService(timeout = 2000) ServiceAware rsAware) { + + checkServices(rsAware, examplePackageAware); + + ExampleModelPackage modelPackage = examplePackageAware.getService(); + assertNotNull(modelPackage); + + ResourceSet resourceSet = rsAware.getService(); + assertNotNull(resourceSet); + + Building loadedObj = getLoadedObject(resourceSet, modelPackage); + + Map saveOptions = new HashMap(); + saveOptions.put(EMFJs.OPTION_USE_ID, false); + + String pathToJsonOutputFile = System.getProperty("base.path") + "/data/exampleUseIdFalse.json"; + save(resourceSet, saveOptions, pathToJsonOutputFile, loadedObj); + + check(pathToJsonOutputFile, str -> !str.contains("\"_id\":")); + } + + private Building getLoadedObject(ResourceSet resourceSet, ExampleModelPackage modelPackage) { + + String pathToJsonInputFile = System.getProperty("base.path") + "/data/exampleTypeByName.json"; + Resource inRes = resourceSet.createResource(URI.createFileURI(pathToJsonInputFile)); + + Map loadOptions = new HashMap(); + loadOptions.put(EMFContext.Attributes.ROOT_ELEMENT, (EClass) modelPackage.getEClassifier("Building")); + loadOptions.put(EMFJs.OPTION_TYPE_USE, EcoreTypeInfo.USE.NAME); + loadOptions.put(EMFJs.OPTION_TYPE_FIELD, "type"); + loadOptions.put(EMFJs.OPTION_TYPE_PACKAGE_URIS, List.of(modelPackage.getNsURI())); + + try { + inRes.load(loadOptions); + } catch (Exception e) { + fail("Error loading Resource! " + e); + } + + assertThat(inRes.getContents()).isNotNull(); + assertThat(inRes.getContents()).isNotEmpty(); + assertThat(inRes.getContents()).hasSize(1); + assertThat(inRes.getContents().get(0)).isInstanceOf(Building.class); + + return (Building) inRes.getContents().get(0); + } + + private void save(ResourceSet resourceSet, Map saveOptions, String pathToJsonOutputFile, Building loadedObj) { + Resource outRes = resourceSet.createResource(URI.createFileURI(pathToJsonOutputFile)); + outRes.getContents().add(loadedObj); + + try { + outRes.save(saveOptions); + } catch (Exception e) { + fail("Error saving Resource! " + e); + } + } + + private void check(String pathToJsonOutputFile, Predicate predicate) { + try (FileInputStream fis = new FileInputStream(pathToJsonOutputFile);) { + byte[] bytes = fis.readAllBytes(); + String str = new String(bytes); + assertThat(predicate.test(str)); + } catch (IOException e) { + fail("Error reading File! " + e); + } + } + + private void checkServices(ServiceAware rsAware, ServiceAware examplePackageAware) { + + assertNotNull(examplePackageAware); + assertThat(examplePackageAware.getServices()).hasSize(1); + + assertNotNull(rsAware); + assertThat(rsAware.getServices()).hasSize(1); + } +} diff --git a/org.gecko.emf.util.example.model/model/examplemodel.ecore b/org.gecko.emf.util.example.model/model/examplemodel.ecore index 2d736e81..c7770254 100644 --- a/org.gecko.emf.util.example.model/model/examplemodel.ecore +++ b/org.gecko.emf.util.example.model/model/examplemodel.ecore @@ -2,30 +2,7 @@ - - - - - - - - - - - - - - - - - - - - + @@ -53,14 +30,11 @@ - - - - + diff --git a/org.gecko.emf.util.example.model/model/examplemodel.genmodel b/org.gecko.emf.util.example.model/model/examplemodel.genmodel index 874b7a43..a2bece0f 100644 --- a/org.gecko.emf.util.example.model/model/examplemodel.genmodel +++ b/org.gecko.emf.util.example.model/model/examplemodel.genmodel @@ -7,32 +7,13 @@ examplemodel.ecore - - - - - - - - - - - - - - - - - - - @@ -51,13 +32,10 @@ - - - - + From e4736bd405abe6aab31d9f1b3dad2b9ee0dea739 Mon Sep 17 00:00:00 2001 From: Ilenia Salvadori Date: Tue, 26 Jul 2022 10:48:26 +0200 Subject: [PATCH 18/19] refactor DateFormatTest to be time zone independent Signed-off-by: Ilenia Salvadori --- .../src/org/gecko/emf/json/tests/DateFormatTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java index e977a195..96c0b6bf 100644 --- a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java @@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.fail; +import java.time.Instant; import java.util.HashMap; import java.util.Map; @@ -72,7 +73,7 @@ public void testDateFormatOnlyDate(@InjectService(timeout = 2000) ServiceAware rsAware, ServiceAware examplePackageAware) { @@ -113,7 +114,7 @@ private void checkServices(ServiceAware rsAware, ServiceAware Date: Tue, 26 Jul 2022 11:00:07 +0200 Subject: [PATCH 19/19] second try to make DateFormatTest independent from time zone Signed-off-by: Ilenia Salvadori --- .../gecko/emf/json/tests/DateFormatTest.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java index 96c0b6bf..d7e0c9da 100644 --- a/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java +++ b/org.gecko.emf.json.tests/src/org/gecko/emf/json/tests/DateFormatTest.java @@ -15,7 +15,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.fail; -import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.HashMap; import java.util.Map; @@ -73,7 +74,13 @@ public void testDateFormatOnlyDate(@InjectService(timeout = 2000) ServiceAware rsAware, ServiceAware examplePackageAware) { @@ -114,16 +128,13 @@ private void checkServices(ServiceAware rsAware, ServiceAware