Search in sources :

Example 11 with Artist

use of org.neo4j.ogm.domain.music.Artist 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 Artist

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

the class LoadCapabilityTest method shouldNotRefreshPropertiesOnEntityReload.

@Test
public void shouldNotRefreshPropertiesOnEntityReload() {
    Artist pinkFloyd = new Artist("Pink Floyd");
    session.save(pinkFloyd);
    session.clear();
    // Load Pink Floyd in a new session, session1
    Session session1 = sessionFactory.openSession();
    Artist pinkfloyd1 = session1.load(Artist.class, pinkFloyd.getId(), 1);
    assertThat(pinkfloyd1).isNotNull();
    assertThat(pinkfloyd1.getName()).isEqualTo("Pink Floyd");
    // Load Pink Floyd to in another new session, session2
    Session session2 = sessionFactory.openSession();
    Artist pinkfloyd2 = session2.load(Artist.class, pinkFloyd.getId(), -1);
    assertThat(pinkfloyd2).isNotNull();
    assertThat(pinkfloyd2.getName()).isEqualTo("Pink Floyd");
    // update the name property
    pinkfloyd2.setName("Purple Floyd");
    // and save it in session2. Now the name in the graph is Purple Floyd
    session2.save(pinkfloyd2);
    // Reload Pink Floyd in session1
    Artist pinkfloyd_1_1 = session1.load(Artist.class, pinkFloyd.getId(), -1);
    assertThat(pinkfloyd_1_1).isNotNull();
    // the name should be refreshed from the graph
    assertThat(pinkfloyd_1_1.getName()).isEqualTo("Pink Floyd");
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Session(org.neo4j.ogm.session.Session) Test(org.junit.Test)

Example 13 with Artist

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

the class LoadCapabilityTest method shouldMaintainSortOrderWhenLoadingByIds.

// GH-302
@Test
public void shouldMaintainSortOrderWhenLoadingByIds() {
    Artist led = new Artist("Led Zeppelin");
    session.save(led);
    Artist bonJovi = new Artist("Bon Jovi");
    session.save(bonJovi);
    List<Long> ids = Arrays.asList(beatlesId, led.getId(), bonJovi.getId());
    SortOrder sortOrder = new SortOrder();
    sortOrder.add(SortOrder.Direction.ASC, "name");
    Iterable<Artist> artists = session.loadAll(Artist.class, ids, sortOrder);
    assertThat(artists).isNotNull();
    List<String> artistNames = new ArrayList<>();
    for (Artist artist : artists) {
        artistNames.add(artist.getName());
    }
    assertThat(artistNames.get(0)).isEqualTo("Bon Jovi");
    assertThat(artistNames.get(1)).isEqualTo("Led Zeppelin");
    assertThat(artistNames.get(2)).isEqualTo("The Beatles");
    sortOrder = new SortOrder();
    sortOrder.add(SortOrder.Direction.DESC, "name");
    artists = session.loadAll(Artist.class, ids, sortOrder);
    assertThat(artists).isNotNull();
    artistNames = new ArrayList<>();
    for (Artist artist : artists) {
        artistNames.add(artist.getName());
    }
    assertThat(artistNames.get(0)).isEqualTo("The Beatles");
    assertThat(artistNames.get(1)).isEqualTo("Led Zeppelin");
    assertThat(artistNames.get(2)).isEqualTo("Bon Jovi");
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) ArrayList(java.util.ArrayList) SortOrder(org.neo4j.ogm.cypher.query.SortOrder) Test(org.junit.Test)

Example 14 with Artist

use of org.neo4j.ogm.domain.music.Artist 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 15 with Artist

use of org.neo4j.ogm.domain.music.Artist 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)

Aggregations

Artist (org.neo4j.ogm.domain.music.Artist)33 Test (org.junit.Test)29 Album (org.neo4j.ogm.domain.music.Album)27 Recording (org.neo4j.ogm.domain.music.Recording)19 Studio (org.neo4j.ogm.domain.music.Studio)19 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)11 Session (org.neo4j.ogm.session.Session)8 Before (org.junit.Before)4 List (java.util.List)3 Filter (org.neo4j.ogm.cypher.Filter)3 SessionFactory (org.neo4j.ogm.session.SessionFactory)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MappingContext (org.neo4j.ogm.context.MappingContext)2 SortOrder (org.neo4j.ogm.cypher.query.SortOrder)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