Search in sources :

Example 16 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class PureDomainDeserializer method deserialize.

@Override
public Entity deserialize(String content) throws IOException {
    PureModelContextData pureModelContextData;
    try {
        pureModelContextData = this.pureParser.parseModel(content);
    } catch (EngineException e) {
        throw new RuntimeException(EngineException.buildPrettyErrorMessage(e.getMessage(), e.getSourceInformation(), e.getErrorType()), e);
    }
    List<PackageableElement> elements = pureModelContextData.getElements();
    if ((elements.size() != 2) || Iterate.noneSatisfy(elements, e -> e instanceof SectionIndex)) {
        throw new RuntimeException("Unexpected parsing result (element count: " + elements.size() + ", SectionIndex present: " + Iterate.anySatisfy(elements, e -> e instanceof SectionIndex) + ")");
    }
    PackageableElement element = elements.get((elements.get(0) instanceof SectionIndex) ? 1 : 0);
    String classifierPath = getClassifierPath(element);
    String intermediateJson = this.jsonMapper.writeValueAsString(element);
    Map<String, Object> entityContent = this.jsonMapper.readValue(intermediateJson, this.jsonMapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class));
    return Entity.newEntity(element.getPath(), classifierPath, entityContent);
}
Also used : Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) PureGrammarParser(org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Association(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Association) Enumeration(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Enumeration) IOException(java.io.IOException) Reader(java.io.Reader) Iterate(org.eclipse.collections.impl.utility.Iterate) EntityTextSerializer(org.finos.legend.sdlc.serialization.EntityTextSerializer) Profile(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Profile) List(java.util.List) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Map(java.util.Map) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Writer(java.io.Writer) PureProtocolObjectMapperFactory(org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Map(java.util.Map) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)

Example 17 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class TestEntityLoader method setUp.

@Before
public void setUp() throws IOException {
    this.testEntities = getTestEntities();
    Map<String, byte[]> filesByPath = Maps.mutable.ofInitialCapacity(this.testEntities.size());
    EntitySerializer entitySerializer = EntitySerializers.getDefaultJsonSerializer();
    for (Entity entity : this.testEntities) {
        String relativeFilePath = "entities/" + entity.getPath().replaceAll("::", "/") + ".json";
        byte[] fileContent = entitySerializer.serializeToBytes(entity);
        filesByPath.put(relativeFilePath, fileContent);
    }
    this.entityLoader = createEntityLoaderFromFiles(filesByPath);
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Before(org.junit.Before)

Example 18 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class TestEntitySerializer method testSerialization.

protected static <T> void testSerialization(IOFunction<? super Entity, ? extends T> serializer, IOFunction<? super T, ? extends Entity> deserializer) throws IOException {
    for (Entity entity : getTestEntities()) {
        T serialization = serializer.apply(entity);
        Entity deserialization = deserializer.apply(serialization);
        assertEntitiesEqualButNotSame(entity.getPath(), entity, deserialization);
    }
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity)

Example 19 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class GitLabComparisonApiTestResource method runUserWorkspaceComparisonTest.

public void runUserWorkspaceComparisonTest() {
    String projectName = "ComparisonTestProjectOne";
    String description = "A test project.";
    ProjectType projectType = ProjectType.PRODUCTION;
    String groupId = "org.finos.sdlc.test";
    String artifactId = "comptestprojone";
    List<String> tags = Lists.mutable.with("doe", "moffitt", AbstractGitLabServerApiTest.INTEGRATION_TEST_PROJECT_TAG);
    String workspaceOneId = "testworkspaceone";
    Project createdProject = gitLabProjectApi.createProject(projectName, description, projectType, groupId, artifactId, tags);
    Assert.assertNotNull(createdProject);
    Assert.assertEquals(projectName, createdProject.getName());
    Assert.assertEquals(description, createdProject.getDescription());
    Assert.assertEquals(projectType, createdProject.getProjectType());
    Assert.assertEquals(Sets.mutable.withAll(tags), Sets.mutable.withAll(createdProject.getTags()));
    String projectId = createdProject.getProjectId();
    Workspace createdWorkspaceOne = gitLabWorkspaceApi.newUserWorkspace(projectId, workspaceOneId);
    Revision fromRevision = gitLabRevisionApi.getUserWorkspaceRevisionContext(projectId, workspaceOneId).getCurrentRevision();
    Assert.assertNotNull(createdWorkspaceOne);
    Assert.assertEquals(workspaceOneId, createdWorkspaceOne.getWorkspaceId());
    Assert.assertEquals(projectId, createdWorkspaceOne.getProjectId());
    Assert.assertNotNull(createdWorkspaceOne.getUserId());
    String entityPath = "test::entity";
    String classifierPath = "meta::test::mathematicsDepartment";
    Map<String, String> entityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-113", "abstract-algebra", "math-185", "complex-analysis");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceOneId).createEntity(entityPath, classifierPath, entityContentMap, "initial entity");
    List<Entity> modifiedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceOneId).getEntities(null, null, null);
    Assert.assertNotNull(modifiedWorkspaceEntities);
    Assert.assertEquals(1, modifiedWorkspaceEntities.size());
    Entity initalEntity = modifiedWorkspaceEntities.get(0);
    Assert.assertEquals(initalEntity.getPath(), entityPath);
    Assert.assertEquals(initalEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(initalEntity.getContent(), entityContentMap);
    Revision toRevision = gitLabRevisionApi.getUserWorkspaceRevisionContext(projectId, workspaceOneId).getCurrentRevision();
    List<EntityDiff> entityDiffs = gitLabComparisonApi.getUserWorkspaceCreationComparison(projectId, workspaceOneId).getEntityDiffs();
    String fromRevisionId = gitLabComparisonApi.getUserWorkspaceCreationComparison(projectId, workspaceOneId).getFromRevisionId();
    String toRevisionId = gitLabComparisonApi.getUserWorkspaceCreationComparison(projectId, workspaceOneId).getToRevisionId();
    Assert.assertNotNull(fromRevision);
    Assert.assertNotNull(toRevision);
    Assert.assertEquals(fromRevision.getId(), fromRevisionId);
    Assert.assertEquals(toRevision.getId(), toRevisionId);
    Assert.assertNotNull(entityDiffs);
    Assert.assertEquals(1, entityDiffs.size());
    Assert.assertEquals(EntityChangeType.CREATE, entityDiffs.get(0).getEntityChangeType());
    Revision projectFromRevision = gitLabRevisionApi.getProjectRevisionContext(projectId).getCurrentRevision();
    Revision projectToRevision = gitLabRevisionApi.getUserWorkspaceRevisionContext(projectId, workspaceOneId).getCurrentRevision();
    List<EntityDiff> projectEntityDiffs = gitLabComparisonApi.getUserWorkspaceProjectComparison(projectId, workspaceOneId).getEntityDiffs();
    String projectFromRevisionId = gitLabComparisonApi.getUserWorkspaceProjectComparison(projectId, workspaceOneId).getFromRevisionId();
    String projectToRevisionId = gitLabComparisonApi.getUserWorkspaceProjectComparison(projectId, workspaceOneId).getToRevisionId();
    Assert.assertNotNull(projectFromRevision);
    Assert.assertEquals(projectFromRevision.getId(), projectFromRevisionId);
    Assert.assertEquals(projectToRevision.getId(), projectToRevisionId);
    Assert.assertNotNull(projectEntityDiffs);
    Assert.assertEquals(1, projectEntityDiffs.size());
    Assert.assertEquals(EntityChangeType.CREATE, projectEntityDiffs.get(0).getEntityChangeType());
}
Also used : Project(org.finos.legend.sdlc.domain.model.project.Project) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) EntityDiff(org.finos.legend.sdlc.domain.model.comparison.EntityDiff) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace)

Example 20 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class GitLabEntityApiTestResource method runEntitiesInNormalUserWorkspaceWorkflowTest.

public void runEntitiesInNormalUserWorkspaceWorkflowTest() throws GitLabApiException {
    String projectName = "CommitFlowTestProject";
    String description = "A test project.";
    ProjectType projectType = ProjectType.PRODUCTION;
    String groupId = "org.finos.sdlc.test";
    String artifactId = "entitytestproj";
    List<String> tags = Lists.mutable.with("doe", "moffitt", AbstractGitLabServerApiTest.INTEGRATION_TEST_PROJECT_TAG);
    String workspaceName = "entitytestworkspace";
    Project createdProject = gitLabProjectApi.createProject(projectName, description, projectType, groupId, artifactId, tags);
    String projectId = createdProject.getProjectId();
    Workspace createdWorkspace = gitLabWorkspaceApi.newUserWorkspace(projectId, workspaceName);
    String workspaceId = createdWorkspace.getWorkspaceId();
    List<Entity> initialWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    List<Entity> initialProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertEquals(Collections.emptyList(), initialWorkspaceEntities);
    Assert.assertEquals(Collections.emptyList(), initialProjectEntities);
    String entityPath = "test::entity";
    String classifierPath = "meta::test::mathematicsDepartment";
    Map<String, String> entityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-113", "abstract-algebra", "math-185", "complex-analysis");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPath, classifierPath, entityContentMap, "initial entity");
    List<Entity> modifiedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    List<Entity> modifiedProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertNotNull(modifiedWorkspaceEntities);
    Assert.assertEquals(Collections.emptyList(), modifiedProjectEntities);
    Assert.assertEquals(1, modifiedWorkspaceEntities.size());
    Entity initalEntity = modifiedWorkspaceEntities.get(0);
    Assert.assertEquals(initalEntity.getPath(), entityPath);
    Assert.assertEquals(initalEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(initalEntity.getContent(), entityContentMap);
    Map<String, String> newEntityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-128", "numerical-analysis", "math-110", "linear-algebra");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).updateEntity(entityPath, classifierPath, newEntityContentMap, "update entity");
    List<Entity> updatedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(updatedWorkspaceEntities);
    Assert.assertEquals(1, updatedWorkspaceEntities.size());
    Entity updatedEntity = updatedWorkspaceEntities.get(0);
    Assert.assertEquals(updatedEntity.getPath(), entityPath);
    Assert.assertEquals(updatedEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(updatedEntity.getContent(), newEntityContentMap);
    String entityPathTwo = "testtwo::entitytwo";
    String classifierPathTwo = "meta::test::csDepartment";
    Map<String, String> newEntityContentMapTwo = Maps.mutable.with("package", "testtwo", "name", "entitytwo", "cs-194", "computational-imaging", "cs-189", "machine-learning");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPathTwo, classifierPathTwo, newEntityContentMapTwo, "second entity");
    List<Entity> postAddWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(postAddWorkspaceEntities);
    Assert.assertEquals(2, postAddWorkspaceEntities.size());
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).deleteEntity(entityPath, classifierPath);
    List<Entity> postDeleteWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(postDeleteWorkspaceEntities);
    Assert.assertEquals(1, postDeleteWorkspaceEntities.size());
    Entity remainedEntity = postDeleteWorkspaceEntities.get(0);
    Assert.assertEquals(remainedEntity.getPath(), entityPathTwo);
    Assert.assertEquals(remainedEntity.getClassifierPath(), classifierPathTwo);
    Assert.assertEquals(remainedEntity.getContent(), newEntityContentMapTwo);
    List<String> paths = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntityPaths(null, null, null);
    Assert.assertNotNull(paths);
    Assert.assertEquals(1, paths.size());
    Assert.assertEquals(entityPathTwo, paths.get(0));
    List<String> labels = Collections.singletonList("default");
    Review testReview = gitLabCommitterReviewApi.createReview(projectId, workspaceId, WorkspaceType.USER, "Add Courses.", "add two courses", labels);
    String reviewId = testReview.getId();
    Review approvedReview = gitLabApproverReviewApi.approveReview(projectId, reviewId);
    Assert.assertNotNull(approvedReview);
    Assert.assertEquals(reviewId, approvedReview.getId());
    Assert.assertEquals(ReviewState.OPEN, approvedReview.getState());
    Assert.assertEquals(labels, approvedReview.getLabels());
    GitLabProjectId sdlcGitLabProjectId = GitLabProjectId.parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getMergeRequestApi();
    int parsedMergeRequestId = Integer.parseInt(reviewId);
    int gitlabProjectId = sdlcGitLabProjectId.getGitLabId();
    String requiredStatus = "can_be_merged";
    CallUntil<MergeRequest, GitLabApiException> callUntil = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredStatus.equals(mr.getMergeStatus()), 20, 1000);
    if (!callUntil.succeeded()) {
        throw new RuntimeException("Merge request " + approvedReview.getId() + " still does not have status \"" + requiredStatus + "\" after " + callUntil.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge to have status \"{}\"", callUntil.getTryCount(), requiredStatus);
    gitLabCommitterReviewApi.commitReview(projectId, reviewId, "add two math courses");
    String requiredMergedStatus = "merged";
    CallUntil<MergeRequest, GitLabApiException> callUntilMerged = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredMergedStatus.equals(mr.getState()), 10, 500);
    if (!callUntilMerged.succeeded()) {
        throw new RuntimeException("Merge request " + reviewId + " still does not have state \"" + requiredMergedStatus + "\" after " + callUntilMerged.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge request to have state \"{}\"", callUntilMerged.getTryCount(), requiredMergedStatus);
    RepositoryApi repositoryApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getRepositoryApi();
    CallUntil<List<Branch>, GitLabApiException> callUntilBranchDeleted = CallUntil.callUntil(() -> repositoryApi.getBranches(sdlcGitLabProjectId.getGitLabId()), GitLabEntityApiTestResource::hasOnlyMasterBranch, 15, 1000);
    if (!callUntilBranchDeleted.succeeded()) {
        // Warn instead of throwing exception since we cannot manage time expectation on GitLab to reflect branch deletion.
        LOGGER.warn("Branch is still not deleted post merge after {} tries", callUntilBranchDeleted.getTryCount());
    }
    LOGGER.info("Waited {} times for branch to be deleted post merge", callUntilBranchDeleted.getTryCount());
    List<Entity> postCommitProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertNotNull(postCommitProjectEntities);
    Assert.assertEquals(1, postCommitProjectEntities.size());
    Entity projectEntity = postCommitProjectEntities.get(0);
    Assert.assertEquals(projectEntity.getPath(), entityPathTwo);
    Assert.assertEquals(projectEntity.getClassifierPath(), classifierPathTwo);
    Assert.assertEquals(projectEntity.getContent(), newEntityContentMapTwo);
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) GitLabApiException(org.gitlab4j.api.GitLabApiException) Review(org.finos.legend.sdlc.domain.model.review.Review) Project(org.finos.legend.sdlc.domain.model.project.Project) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) RepositoryApi(org.gitlab4j.api.RepositoryApi) List(java.util.List) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace)

Aggregations

Entity (org.finos.legend.sdlc.domain.model.entity.Entity)81 Test (org.junit.Test)41 File (java.io.File)17 List (java.util.List)17 Path (java.nio.file.Path)15 Collections (java.util.Collections)15 MavenProject (org.apache.maven.project.MavenProject)15 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)15 IOException (java.io.IOException)14 Map (java.util.Map)13 Assert (org.junit.Assert)12 Iterate (org.eclipse.collections.impl.utility.Iterate)11 Sets (org.eclipse.collections.api.factory.Sets)10 ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)10 ProjectConfiguration (org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)10 EntityLoader (org.finos.legend.sdlc.serialization.EntityLoader)10 Files (java.nio.file.Files)9 Class (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class)9 Workspace (org.finos.legend.sdlc.domain.model.project.workspace.Workspace)9 Paths (java.nio.file.Paths)8