|
| 1 | +/* |
| 2 | + * Copyright 2021 Systems Research Group, University of St Andrews: |
| 3 | + * <https://github.com/stacs-srg> |
| 4 | + * |
| 5 | + * This file is part of the module population-records. |
| 6 | + * |
| 7 | + * population-records is free software: you can redistribute it and/or modify it under the terms of the GNU General Public |
| 8 | + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later |
| 9 | + * version. |
| 10 | + * |
| 11 | + * population-records is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 12 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along with population-records. If not, see |
| 15 | + * <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package uk.ac.standrews.cs.population_records; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.SocketAddress; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.AfterAll; |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.BeforeAll; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +import org.neo4j.driver.AuthTokens; |
| 37 | +import org.neo4j.driver.Driver; |
| 38 | +import org.neo4j.driver.GraphDatabase; |
| 39 | +import org.neo4j.driver.Session; |
| 40 | +import org.neo4j.harness.Neo4j; |
| 41 | +import org.neo4j.harness.Neo4jBuilders; |
| 42 | + |
| 43 | +import uk.ac.standrews.cs.neoStorr.impl.Store; |
| 44 | +import uk.ac.standrews.cs.neoStorr.impl.exceptions.BucketException; |
| 45 | +import uk.ac.standrews.cs.neoStorr.impl.exceptions.RepositoryException; |
| 46 | +import uk.ac.standrews.cs.neoStorr.interfaces.IBucket; |
| 47 | +import uk.ac.standrews.cs.population_records.record_types.*; |
| 48 | + |
| 49 | +public class RecordRepositoryNeoTest { |
| 50 | + static final RecordTypeTest BIRTH_TEST = new BirthTest(); |
| 51 | + static final RecordTypeTest MARRIAGE_TEST = new MarriageTest(); |
| 52 | + static final RecordTypeTest DEATH_TEST = new DeathTest(); |
| 53 | + static final String REPO_NAME_BASE = "test_"; |
| 54 | + |
| 55 | + static Neo4j neo4jDb; |
| 56 | + String repoName; |
| 57 | + RecordRepository repo; |
| 58 | + |
| 59 | + @BeforeAll |
| 60 | + static void setUpNeo() { |
| 61 | + neo4jDb = Neo4jBuilders.newInProcessBuilder() |
| 62 | + .build(); |
| 63 | + // Sets override for neo-storr.neoDbCypherBridge |
| 64 | + System.setProperty("NeoDBTestURL", neo4jDb.boltURI().toString()); |
| 65 | + } |
| 66 | + |
| 67 | + @BeforeEach |
| 68 | + void setUpRepo() { |
| 69 | + repoName = REPO_NAME_BASE + System.nanoTime(); |
| 70 | + repo = new RecordRepository(repoName); |
| 71 | + } |
| 72 | + |
| 73 | + @AfterEach |
| 74 | + void tearDown() throws RepositoryException { |
| 75 | + Store.getInstance().deleteRepository(repoName); |
| 76 | + assertFalse(Store.getInstance().repositoryExists(repoName)); |
| 77 | + repo = null; |
| 78 | + } |
| 79 | + |
| 80 | + @AfterAll |
| 81 | + static void tearDownNeo() { |
| 82 | + Store.getInstance().close(); |
| 83 | + System.clearProperty("NeoDBTestURL"); |
| 84 | + |
| 85 | + if (neo4jDb!= null) neo4jDb.close(); |
| 86 | + neo4jDb = null; |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testNeo4jConnection() { |
| 91 | + String testString = "Hello world"; |
| 92 | + Driver driver = GraphDatabase.driver(neo4jDb.boltURI(), AuthTokens.none()); |
| 93 | + |
| 94 | + try (Session session = driver.session()) { |
| 95 | + String response = session.run("RETURN '"+testString+"'").single().get(0).asString(); |
| 96 | + assertEquals(testString, response); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Test |
| 102 | + void testAddAndGetBirthRecord() throws BucketException, RepositoryException, IOException { |
| 103 | + Birth birth = new Birth(BIRTH_TEST.datasetNoRecords, BIRTH_TEST.testRecords.get(0)); |
| 104 | + repo.addBirth(birth); |
| 105 | + |
| 106 | + assertEquals(1, repo.getNumberOfBirths()); |
| 107 | + |
| 108 | + Birth retrieved = repo.getBirths().iterator().next(); |
| 109 | + assertEquals(birth, retrieved); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testAddAndGetMarriageRecord() throws BucketException, RepositoryException, IOException { |
| 114 | + Marriage marriage = new Marriage(MARRIAGE_TEST.datasetNoRecords, MARRIAGE_TEST.testRecords.get(0)); |
| 115 | + repo.addMarriage(marriage); |
| 116 | + |
| 117 | + assertEquals(1, repo.getNumberOfMarriages()); |
| 118 | + |
| 119 | + Marriage retrieved = repo.getMarriages().iterator().next(); |
| 120 | + assertEquals(marriage, retrieved); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testAddAndGetDeathRecord() throws BucketException, RepositoryException, IOException { |
| 125 | + Death death = new Death(DEATH_TEST.datasetNoRecords, DEATH_TEST.testRecords.get(0)); |
| 126 | + repo.addDeath(death); |
| 127 | + |
| 128 | + assertEquals(1, repo.getNumberOfDeaths()); |
| 129 | + |
| 130 | + Death retrieved = repo.getDeaths().iterator().next(); |
| 131 | + assertEquals(death, retrieved); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testDeleteBuckets() throws RepositoryException { |
| 136 | + repo.deleteBirthsBucket(); |
| 137 | + repo.deleteDeathsBucket(); |
| 138 | + repo.deleteMarriagesBucket(); |
| 139 | + |
| 140 | + for (String x : RecordRepository.getBucketNames()) { |
| 141 | + assertThrows(RepositoryException.class, () -> { |
| 142 | + Store.getInstance().getRepository(repoName).getBucket(x); |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void testGetBuckets() { |
| 149 | + for (String x : RecordRepository.getBucketNames()) { |
| 150 | + assertNotNull(repo.getBucket(x)); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments