Skip to content

Commit

Permalink
DATAGRAPH-1275 - Verify update of relationship entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-simons committed Dec 10, 2019
1 parent a529a08 commit bbf7ec1
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public class WorldRepositoryTests {
@Autowired
WorldRepository worldRepository;

@Autowired
TransactionTemplate transactionTemplate;

boolean failed = false;

@Test // DATAGRAPH-951
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2011-2019 the original author or authors.
*
* 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
*
* https://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.springframework.data.neo4j.repository.relcentric;

import static org.assertj.core.api.Assertions.*;

import java.util.Collections;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.relcentric.app.Actor;
import org.springframework.data.neo4j.repository.relcentric.app.Movie;
import org.springframework.data.neo4j.repository.relcentric.app.Role;
import org.springframework.data.neo4j.repository.relcentric.app.RoleRepository;
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.support.TransactionTemplate;

/**
* @author Michael J. Simons
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MinimalRelationshipEntityMappingTest.Config.class)
public class MinimalRelationshipEntityMappingTest {

@Autowired RoleRepository repository;

@Autowired private TransactionTemplate transactionTemplate;

@Autowired SessionFactory sessionFactory;

@Test // DATAGRAPH-1275, OGM-GH-607
public void verifyUpdateOfRelationshipEntity() {

long id = transactionTemplate.execute(status -> {
Actor actor = new Actor("A1");
Movie movie = new Movie("M1");
Role role = new Role("R1", actor, movie);
return repository.save(role).getId();
});

Iterable<Map<String, Object>> results = sessionFactory.openSession()
.query("MATCH (m:Movie) <- [:ACTS_IN] - (:Actor {name: 'A1'}) RETURN collect(m.name) as titles",
Collections.emptyMap()).queryResults();
assertThat(results).hasSize(1);
assertThat(results).first()
.satisfies(m -> assertThat((String[]) m.get("titles")).containsOnly("M1"));

transactionTemplate.execute(status -> {
Role role = repository.findById(id).get();
role.setMovie(new Movie("M2"));
return repository.save(role).getId();
});

results = sessionFactory.openSession()
.query("MATCH (m:Movie) <- [:ACTS_IN] - (:Actor {name: 'A1'}) RETURN collect(m.name) as titles",
Collections.emptyMap()).queryResults();
assertThat(results).hasSize(1);
assertThat(results).first()
.satisfies(m -> assertThat((String[]) m.get("titles")).containsOnly("M2"));
}

@Configuration
@Neo4jIntegrationTest(domainPackages = "org.springframework.data.neo4j.repository.relcentric.app")
static class Config {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2011-2019 the original author or authors.
*
* 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
*
* https://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.springframework.data.neo4j.repository.relcentric.app;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.ogm.annotation.Relationship;

/**
* @author Michael J. Simons
*/
public class Actor {

public Long id;
String name;

@Relationship(type = "ACTS_IN")
private List<Role> roles = new ArrayList<>();

public Actor() {
}

public Actor(String name) {
this.name = name;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2011-2019 the original author or authors.
*
* 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
*
* https://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.springframework.data.neo4j.repository.relcentric.app;


/**
* @author Michael J. Simons
*/
public class Movie {

private Long id;
private String name;

public Movie() {
}

public Movie(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2011-2019 the original author or authors.
*
* 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
*
* https://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.springframework.data.neo4j.repository.relcentric.app;

import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;

/**
* @author Michael J. Simons
*/
@RelationshipEntity(type = "ACTS_IN")
public class Role {

private Long id;
private String played;

@StartNode
private Actor actor;

@EndNode
private Movie movie;

public Role() {
}

public Role(String character, Actor actor, Movie movie) {
played = character;
this.actor = actor;
this.movie = movie;
}

public Long getId() {
return id;
}

public Movie getMovie() {
return movie;
}

public void setMovie(Movie movie) {
this.movie = movie;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2011-2019 the original author or authors.
*
* 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
*
* https://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.springframework.data.neo4j.repository.relcentric.app;

import org.springframework.data.neo4j.repository.Neo4jRepository;

/**
* @author Michael J. Simons
*/
public interface RoleRepository extends Neo4jRepository<Role, Long> {
}

0 comments on commit bbf7ec1

Please sign in to comment.