Search in sources :

Example 11 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class LoadCapabilityTest method shouldNotModifyPreviouslyLoadedNodesWhenDepthIsIncreased.

// GH-173
@Test
public void shouldNotModifyPreviouslyLoadedNodesWhenDepthIsIncreased() {
    Artist led = new Artist("Led Zeppelin");
    Album album = new Album("Led Zeppelin IV");
    Studio studio = new Studio("Island Studios");
    Recording recording = new Recording(album, studio, 1970);
    led.addAlbum(album);
    album.setArtist(led);
    album.setRecording(recording);
    session.save(led);
    session.clear();
    // Load the Artist to depth 0 in the first session
    Artist ledZeppelin = session.load(Artist.class, led.getId(), 0);
    assertThat(ledZeppelin).isNotNull();
    assertThat(ledZeppelin.getName()).isEqualTo(led.getName());
    assertThat(ledZeppelin.getAlbums()).isEmpty();
    // In session 2, update the name of the artist
    Session session2 = sessionFactory.openSession();
    Artist ledZepp = session2.load(Artist.class, led.getId(), 0);
    ledZepp.setName("Led Zepp");
    session2.save(ledZepp);
    // Back in the first session, load the artist to depth 1
    ledZeppelin = session.load(Artist.class, led.getId(), 1);
    assertThat(ledZeppelin).isNotNull();
    assertThat(ledZeppelin.getName()).isEqualTo(led.getName());
    assertThat(ledZeppelin.getAlbums()).hasSize(1);
    Album ledZeppelinIV = ledZeppelin.getAlbums().iterator().next();
    assertThat(ledZeppelinIV.getName()).isEqualTo(album.getName());
    assertThat(ledZeppelinIV.getArtist()).isEqualTo(ledZeppelin);
    assertThat(ledZeppelinIV.getRecording()).isNull();
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) Album(org.neo4j.ogm.domain.music.Album) Recording(org.neo4j.ogm.domain.music.Recording) Studio(org.neo4j.ogm.domain.music.Studio) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Session(org.neo4j.ogm.session.Session) Test(org.junit.Test)

Example 12 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class SessionAndMappingContextTest method disposeFromMappingContextOnDeleteWithTransientRelationshipTest.

@Test
public void disposeFromMappingContextOnDeleteWithTransientRelationshipTest() {
    MappingContext mappingContext = session.context();
    assertThat(mappingContext.getNodeEntity(artist1.getId()).getClass() == Artist.class).isTrue();
    session.delete(artist1);
    // check that the mapping context does not hold a reference to the deleted entity anymore
    Object object = mappingContext.getNodeEntity(artist1.getId());
    assertThat(object == null).isTrue();
    // check that objects with references to the deleted object have been cleared
    // check for TransientRelationship, where the object connected to the deleted object holds ref in a Set
    Album retrievedAlbum1 = (Album) mappingContext.getNodeEntity(album1.getId());
    assertThat(retrievedAlbum1.getArtist() == null).isTrue();
    Album retrievedAlbum2 = (Album) mappingContext.getNodeEntity(album2.getId());
    assertThat(retrievedAlbum2.getArtist() == null).isTrue();
    Album retrievedAlbum3 = (Album) mappingContext.getNodeEntity(album3.getId());
    assertThat(retrievedAlbum3.getArtist() == null).isTrue();
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) Album(org.neo4j.ogm.domain.music.Album) Test(org.junit.Test)

Example 13 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class TransactionTest method shouldBeAbleToRetrySaveOnTransactionRollback.

// GH-126
@Test
public void shouldBeAbleToRetrySaveOnTransactionRollback() {
    Transaction tx = session.beginTransaction();
    Studio emi = new Studio("EMI Studios, London");
    Artist theBeatles = new Artist("The Beatles");
    Album please = new Album("Please Please Me");
    Recording pleaseRecording = new Recording(please, emi, 1963);
    please.setRecording(pleaseRecording);
    theBeatles.getAlbums().add(please);
    please.setArtist(theBeatles);
    session.save(theBeatles);
    tx.rollback();
    session.save(theBeatles);
    session.clear();
    theBeatles = session.loadAll(Artist.class, -1).iterator().next();
    assertThat(theBeatles.getName()).isEqualTo("The Beatles");
    assertThat(theBeatles.getAlbums()).hasSize(1);
    assertThat(theBeatles.getAlbums().iterator().next().getName()).isEqualTo("Please Please Me");
    assertThat(theBeatles.getAlbums().iterator().next().getRecording().getStudio().getName()).isEqualTo("EMI Studios, London");
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) Transaction(org.neo4j.ogm.transaction.Transaction) Album(org.neo4j.ogm.domain.music.Album) Recording(org.neo4j.ogm.domain.music.Recording) Studio(org.neo4j.ogm.domain.music.Studio) Test(org.junit.Test)

Example 14 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class CompilerTest method createSimpleRelationshipWithIllegalCharactersBetweenObjects.

// DATAGRAPH-589
@Test
public void createSimpleRelationshipWithIllegalCharactersBetweenObjects() {
    Artist theBeatles = new Artist("The Beatles");
    Album please = new Album("Please Please Me");
    theBeatles.getAlbums().add(please);
    please.setArtist(theBeatles);
    Compiler compiler = mapAndCompile(theBeatles, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`l'artiste`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`l'album`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`HAS-ALBUM`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) Statement(org.neo4j.ogm.request.Statement) Album(org.neo4j.ogm.domain.music.Album) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 15 with Album

use of org.neo4j.ogm.domain.music.Album in project neo4j-ogm by neo4j.

the class MusicIntegrationTest method shouldSaveAndRetrieveArtistWithTwoRelationshipTypesToAlbums.

/**
 * @see DATAGRAPH-637
 */
@Test
public void shouldSaveAndRetrieveArtistWithTwoRelationshipTypesToAlbums() {
    Studio emi = new Studio("EMI Studios, London");
    Studio olympic = new Studio("Olympic Studios, London");
    Artist theBeatles = new Artist("The Beatles");
    Artist eric = new Artist("Eric Clapton");
    Album slowhand = new Album("Slowhand");
    Recording slowRecording = new Recording(slowhand, olympic, 1977);
    slowhand.setRecording(slowRecording);
    slowhand.setArtist(eric);
    session.save(slowhand);
    session.clear();
    Album theBeatlesAlbum = new Album("The Beatles");
    Recording pleaseRecording = new Recording(theBeatlesAlbum, emi, 1968);
    theBeatlesAlbum.setRecording(pleaseRecording);
    theBeatles.getAlbums().add(theBeatlesAlbum);
    theBeatlesAlbum.setArtist(theBeatles);
    theBeatlesAlbum.setGuestArtist(eric);
    session.save(theBeatlesAlbum);
    theBeatles = session.loadAll(Artist.class, new Filters().add(new Filter("name", ComparisonOperator.EQUALS, "The Beatles"))).iterator().next();
    assertThat(theBeatles.getName()).isEqualTo("The Beatles");
    assertThat(theBeatles.getAlbums()).hasSize(1);
    assertThat(theBeatles.getAlbums().iterator().next().getName()).isEqualTo("The Beatles");
    assertThat(theBeatles.getAlbums().iterator().next().getRecording().getStudio().getName()).isEqualTo("EMI Studios, London");
    assertThat(theBeatles.getAlbums().iterator().next().getGuestArtist()).isEqualTo(eric);
    // Eric has 2 albums now
    session.clear();
    Artist loadedEric = session.loadAll(Artist.class, new Filters().add(new Filter("name", ComparisonOperator.EQUALS, "Eric Clapton"))).iterator().next();
    assertThat(loadedEric).isNotNull();
    assertThat(loadedEric.getGuestAlbums().iterator().next().getName()).isEqualTo("The Beatles");
    assertThat(loadedEric.getAlbums().iterator().next().getName()).isEqualTo("Slowhand");
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) Filters(org.neo4j.ogm.cypher.Filters) Filter(org.neo4j.ogm.cypher.Filter) Album(org.neo4j.ogm.domain.music.Album) Recording(org.neo4j.ogm.domain.music.Recording) Studio(org.neo4j.ogm.domain.music.Studio) Test(org.junit.Test)

Aggregations

Album (org.neo4j.ogm.domain.music.Album)33 Test (org.junit.Test)30 Artist (org.neo4j.ogm.domain.music.Artist)27 Recording (org.neo4j.ogm.domain.music.Recording)21 Studio (org.neo4j.ogm.domain.music.Studio)21 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)9 Session (org.neo4j.ogm.session.Session)7 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 Filter (org.neo4j.ogm.cypher.Filter)3 Map (java.util.Map)2 MappingContext (org.neo4j.ogm.context.MappingContext)2 SessionFactory (org.neo4j.ogm.session.SessionFactory)2 Transaction (org.neo4j.ogm.transaction.Transaction)2 LocalDate (java.time.LocalDate)1 ZoneId (java.time.ZoneId)1 Date (java.util.Date)1 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)1 Filters (org.neo4j.ogm.cypher.Filters)1