generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor updates to ArtifactGenerationExtension (#900)
- Loading branch information
1 parent
0e0200c
commit 07d20ce
Showing
7 changed files
with
313 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...gend/engine/language/pure/dsl/generation/extension/ArtifactGenerationExtensionLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2022 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.engine.language.pure.dsl.generation.extension; | ||
|
||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.eclipse.collections.api.factory.Lists; | ||
import org.eclipse.collections.api.factory.Sets; | ||
import org.eclipse.collections.impl.utility.ListIterate; | ||
import org.finos.legend.engine.shared.core.operational.errorManagement.EngineException; | ||
|
||
public class ArtifactGenerationExtensionLoader | ||
{ | ||
public static final String EXTENSION_KEY_REGEX = "^[a-zA-Z_\\-]+$"; | ||
|
||
public static List<ArtifactGenerationExtension> extensions() | ||
{ | ||
List<ArtifactGenerationExtension> extensions = Lists.mutable.withAll(ServiceLoader.load(ArtifactGenerationExtension.class)); | ||
Set<String> extensionKeys = Sets.mutable.empty(); | ||
for (ArtifactGenerationExtension extension : extensions) | ||
{ | ||
if (!extensionKeys.add(extension.getKey())) | ||
{ | ||
String extensionsWithSameKey = ListIterate.collect(extensions.stream().filter(e -> e.getKey().equals(extension.getKey())).collect(Collectors.toList()), e -> e.getClass().getName()) | ||
.makeString(","); | ||
throw new EngineException("Artifact extension keys must be unique. Found duplicate key: '" + extension.getKey() + "' on extensions: " + extensionsWithSameKey); | ||
} | ||
if (!extension.getKey().matches(EXTENSION_KEY_REGEX)) | ||
{ | ||
throw new EngineException("Artifact extension keys can't have spaces or special characters. Found invalid key: '" + extension.getKey() + "'."); | ||
} | ||
} | ||
return extensions; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...org/finos/legend/engine/generation/TestDataSpaceAnalyticsArtifactGenerationExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2022 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.engine.generation; | ||
|
||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.eclipse.collections.api.factory.Maps; | ||
import org.finos.legend.engine.analytics.model.DataSpaceAnalysisResult; | ||
import org.finos.legend.engine.language.pure.compiler.Compiler; | ||
import org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel; | ||
import org.finos.legend.engine.language.pure.dsl.generation.extension.Artifact; | ||
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser; | ||
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData; | ||
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.dataSpace.DataSpace; | ||
import org.finos.legend.engine.shared.core.ObjectMapperFactory; | ||
import org.finos.legend.engine.shared.core.deployment.DeploymentMode; | ||
import org.finos.legend.engine.shared.core.deployment.DeploymentStateAndVersions; | ||
import org.finos.legend.pure.generated.Root_meta_pure_metamodel_dataSpace_DataSpace; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
|
||
public class TestDataSpaceAnalyticsArtifactGenerationExtension | ||
{ | ||
private static final ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports(); | ||
|
||
private String getResourceAsString(String path) | ||
{ | ||
try | ||
{ | ||
URL infoURL = DeploymentStateAndVersions.class.getClassLoader().getResource(path); | ||
if (infoURL != null) | ||
{ | ||
java.util.Scanner scanner = new java.util.Scanner(infoURL.openStream()).useDelimiter("\\A"); | ||
return scanner.hasNext() ? scanner.next() : null; | ||
} | ||
return null; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDataSpaceAnalyticsArtifactGenerationExtension() throws Exception | ||
{ | ||
String pureModelString = getResourceAsString("models/DataspaceModel.pure"); | ||
PureModelContextData pureModelContextData = PureGrammarParser.newInstance().parseModel(pureModelString); | ||
PureModel pureModel = Compiler.compile(pureModelContextData, DeploymentMode.TEST, null); | ||
Map<DataSpace, List<Artifact>> results = Maps.mutable.empty(); | ||
DataSpaceAnalyticsArtifactGenerationExtension extension = new DataSpaceAnalyticsArtifactGenerationExtension(); | ||
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.PackageableElement packageableElement = pureModel.getPackageableElement("dataSpace::_FirmDataSpace"); | ||
Assert.assertTrue(packageableElement instanceof Root_meta_pure_metamodel_dataSpace_DataSpace); | ||
Root_meta_pure_metamodel_dataSpace_DataSpace metamodelDatasapce = (Root_meta_pure_metamodel_dataSpace_DataSpace) packageableElement; | ||
Assert.assertTrue(extension.canGenerate(metamodelDatasapce)); | ||
List<Artifact> outputs = extension.generate(packageableElement, pureModel, pureModelContextData, "vX_X_X"); | ||
Assert.assertEquals(1, outputs.size()); | ||
Artifact dataSpaceAnalyticsResult = outputs.get(0); | ||
Assert.assertEquals(dataSpaceAnalyticsResult.format, "json"); | ||
Assert.assertEquals(dataSpaceAnalyticsResult.path, "AnalyticsResult.json"); | ||
Assert.assertNotNull(dataSpaceAnalyticsResult.content); | ||
// assert the result is JSON of data space analysis result | ||
objectMapper.readValue(dataSpaceAnalyticsResult.content, DataSpaceAnalysisResult.class); | ||
} | ||
} |
Oops, something went wrong.