Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JNG-4218 support add and remove for collections #248

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
*/

import com.google.inject.Inject;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.composition.Composition;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.composition.CompositionDao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entitya.EntityA;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entitya.EntityADao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entitya.EntityAIdentifier;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entitya.EntityAMask;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityb.EntityB;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityb.EntityBDao;
Expand All @@ -45,15 +46,8 @@
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityf4.EntityF4;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityf4.EntityF4Dao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityg.EntityG;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityg.EntityGDao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityh.EntityH;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.entityh.EntityHDao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.transfera.TransferA;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.transfera.TransferADao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.transferc.TransferC;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.transferc.TransferCDao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.transferd.TransferD;
import hu.blackbelt.judo.psm.generator.sdk.core.test.api.compositionrelationships.compositionrelationships.transferd.TransferDDao;
import hu.blackbelt.judo.psm.generator.sdk.core.test.guice.CompositionRelationshipsDaoModules;
import hu.blackbelt.judo.requirement.report.annotation.Requirement;
import hu.blackbelt.judo.runtime.core.exception.ValidationException;
Expand All @@ -65,6 +59,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -111,13 +106,7 @@ public class CompositionRelationshipsTest {
EntityHDao entityHDao;

@Inject
TransferADao transferADao;

@Inject
TransferCDao transferCDao;

@Inject
TransferDDao transferDDao;
CompositionDao compositionDao;

EntityA entityA;
EntityC singleConA;
Expand Down Expand Up @@ -501,6 +490,7 @@ void testDeepCopyCreate() {

@Test
void testDeepCopyUpdate() {

EntityA a2 = entityADao.create(EntityA.builder().withSingleRequiredConA(EntityC.builder().build()).build());
assertEquals(Optional.empty(), a2.getSingleConA());
assertEquals(0, a2.getCollectionConA().size());
Expand Down Expand Up @@ -568,4 +558,139 @@ void testDeepCopyUpdate() {
() -> entityADao.update(a5)
);
}

@Test
void testAddAndRemoveOnCollections() {
EntityC c2 = entityCDao.create(EntityC.builder().withStringC("C2").build());
EntityC c3 = entityCDao.create(EntityC.builder().withStringC("C3").build());
EntityA entityA1 = entityADao.create(EntityA.builder()
.withSingleRequiredConA(EntityC.builder().withStringC("C1").build())
.withCollectionConA(List.of(c2, c3))
.build());

assertEquals("C1", entityA1.getSingleRequiredConA().getStringC().orElseThrow());
assertEquals(2, entityA1.getCollectionConA().size());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C2")).count());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C3")).count());

EntityC c4 = EntityC.builder().withStringC("C4").build();
EntityC c5 = EntityC.builder().withStringC("C5").build();
EntityC c6 = EntityC.builder().withStringC("C6").build();
entityA1.addToCollectionConA(c4);
entityA1.addToCollectionConA(c5, c6);

assertEquals("C1", entityA1.getSingleRequiredConA().getStringC().orElseThrow());
assertEquals(5, entityA1.getCollectionConA().size());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C2")).count());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C3")).count());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C4")).count());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C5")).count());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C6")).count());

entityA1.addToCollectionConA(null);
assertEquals(6, entityA1.getCollectionConA().size());
EntityC c7 = EntityC.builder().withStringC("C7").build();
entityA1.addToCollectionConA(null, c7);
Madmat8 marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c != null && c.getStringC().orElseThrow().equals("C6")).count());
assertEquals(1, entityA1.getCollectionConA().stream()
.filter(c -> c != null && c.getStringC().orElseThrow().equals("C7")).count());
assertEquals(8, entityA1.getCollectionConA().size());

// The ID of c3 was changed after entity A1 was created
entityA1.removeFromCollectionConA(c3,c4);
assertEquals(7, entityA1.getCollectionConA().size());
assertEquals(1, entityA1.getCollectionConA().stream().filter(c -> c != null && c.getStringC().orElseThrow().equals("C3")).count());
assertEquals(0, entityA1.getCollectionConA().stream().filter(c -> c != null && c.getStringC().orElseThrow().equals("C4")).count());

entityA1.removeFromCollectionConA(c6);
assertEquals(6, entityA1.getCollectionConA().size());
assertEquals(0, entityA1.getCollectionConA().stream().filter(c -> c != null && c.getStringC().orElseThrow().equals("C6")).count());

c5.setStringC("C5Updated");
entityA1.removeFromCollectionConA(c5);

assertEquals(5, entityA1.getCollectionConA().size());
assertEquals(0, entityA1.getCollectionConA().stream().filter(c -> c != null && c.getStringC().orElseThrow().equals("C5")).count());
assertEquals(0, entityA1.getCollectionConA().stream().filter(c -> c != null && c.getStringC().orElseThrow().equals("C5Updated")).count());

EntityC c8 = EntityC.builder().withStringC("C8").build();
EntityC c9 = EntityC.builder().withStringC("C9").build();
EntityC c10 = EntityC.builder().withStringC("C10").build();
EntityA entityA2 = EntityA.builder().withStringA("A2").withSingleRequiredConA(EntityC.builder().build()).build();
EntityA entityA3 = EntityA.builder().withStringA("A3").withSingleRequiredConA(EntityC.builder().build()).build();
EntityA entityA4 = EntityA.builder().withStringA("A4").withSingleRequiredConA(EntityC.builder().build()).build();
Composition composition = Composition.builder().build();
entityA2.addToCollectionConA(c9, c10);
entityA3.addToCollectionConA(c3, c4);
entityA4.addToCollectionConA(c7, c8);
composition.setEntityA(entityA2);
composition.addToEntityAs(entityA3, entityA4);

assertEquals(2, composition.getEntityAs().size());

entityA2 = composition.getEntityA().orElseThrow();
entityA3 = composition.getEntityAs().stream().filter(a -> a.getStringA().orElseThrow().equals("A3")).findFirst().orElseThrow();
entityA4 = composition.getEntityAs().stream().filter(a -> a.getStringA().orElseThrow().equals("A4")).findFirst().orElseThrow();
assertEquals("A2", entityA2.getStringA().orElseThrow());
assertEquals("A3", entityA3.getStringA().orElseThrow());
assertEquals("A4", entityA4.getStringA().orElseThrow());
assertEquals(1, entityA3.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C3")).count());
assertEquals(1, entityA3.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C4")).count());
assertEquals(1, entityA4.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C7")).count());
assertEquals(1, entityA4.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C8")).count());
assertEquals(1, entityA2.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C9")).count());
assertEquals(1, entityA2.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C10")).count());

entityA3.removeFromCollectionConA(c3);
composition.getEntityA().orElseThrow().removeFromCollectionConA(c9);

assertEquals(0, entityA2.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C9")).count());
assertEquals(0, entityA3.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C3")).count());

composition = compositionDao.create(composition);

assertEquals(2, composition.getEntityAs().size());

entityA2 = composition.getEntityA().orElseThrow();
entityA3 = composition.getEntityAs().stream().filter(a -> a.getStringA().orElseThrow().equals("A3")).findFirst().orElseThrow();
entityA4 = composition.getEntityAs().stream().filter(a -> a.getStringA().orElseThrow().equals("A4")).findFirst().orElseThrow();
assertEquals("A2", entityA2.getStringA().orElseThrow());
assertEquals("A3", entityA3.getStringA().orElseThrow());
assertEquals("A4", entityA4.getStringA().orElseThrow());
assertEquals(1, entityA3.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C4")).count());
assertEquals(1, entityA4.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C7")).count());
assertEquals(1, entityA4.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C8")).count());
assertEquals(1, entityA2.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C10")).count());

EntityA entityA5 = EntityA.builder().withStringA("AA").withSingleRequiredConA(EntityC.builder().withStringC("CC").build()).build();
entityA5.addToCollectionConA(EntityC.builder().withStringC("C12").build());
composition.addToEntityAs(entityA5);
EntityC c11 = EntityC.builder().withStringC("C11").build();
entityA2.addToCollectionConA(c11);
c7 = entityA3.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C4")).findFirst().orElseThrow();
entityA4.removeFromCollectionConA(c7);

composition = compositionDao.update(composition);

assertEquals(3, composition.getEntityAs().size());

entityA2 = composition.getEntityA().orElseThrow();
entityA3 = composition.getEntityAs().stream().filter(a -> a.getStringA().orElseThrow().equals("A3")).findFirst().orElseThrow();
entityA4 = composition.getEntityAs().stream().filter(a -> a.getStringA().orElseThrow().equals("A4")).findFirst().orElseThrow();
entityA5 = composition.getEntityAs().stream().filter(a -> a.getStringA().orElseThrow().equals("AA")).findFirst().orElseThrow();
assertEquals("A2", entityA2.getStringA().orElseThrow());
assertEquals("A3", entityA3.getStringA().orElseThrow());
assertEquals("A4", entityA4.getStringA().orElseThrow());
assertEquals("A4", entityA4.getStringA().orElseThrow());
assertEquals("AA", entityA5.getStringA().orElseThrow());
assertEquals(1, entityA3.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C4")).count());
assertEquals(1, entityA4.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C8")).count());
assertEquals(1, entityA2.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C10")).count());
assertEquals("CC", entityA5.getSingleRequiredConA().getStringC().orElseThrow());
assertEquals(1, entityA5.getCollectionConA().stream().filter(c -> c.getStringC().orElseThrow().equals("C12")).count());

EntityA entityA = entityADao.create(EntityA.builder().withSingleRequiredConA(EntityC.builder().build()).build());

entityA.setCollectionConA(List.of(EntityC.builder().withStringC("C1").build(), EntityC.builder().withStringC("C2").build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ model CompositionRelationships;

type string String min-size:0 max-size:128;

entity Composition {
field EntityA entityA;
field EntityA[] entityAs;
}

entity EntityA {
field String stringA;
field EntityC singleConA;
Expand Down
Loading
Loading