{"id":20533823,"url":"https://github.com/anicolaspp/ojai-testing","last_synced_at":"2025-09-12T06:40:00.157Z","repository":{"id":53095926,"uuid":"177884559","full_name":"anicolaspp/ojai-testing","owner":"anicolaspp","description":"Embeded In-Memory Implementation of OJAI Driver to be used in testing","archived":false,"fork":false,"pushed_at":"2021-04-07T02:29:03.000Z","size":135,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T20:40:44.376Z","etag":null,"topics":["json","mapr","mapr-db","mapr-json","mapr-ojai","maprdb","ojai","scala","testing"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anicolaspp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-26T23:35:34.000Z","updated_at":"2021-04-07T02:29:05.000Z","dependencies_parsed_at":"2022-08-24T14:51:50.625Z","dependency_job_id":null,"html_url":"https://github.com/anicolaspp/ojai-testing","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicolaspp%2Fojai-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicolaspp%2Fojai-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicolaspp%2Fojai-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicolaspp%2Fojai-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anicolaspp","download_url":"https://codeload.github.com/anicolaspp/ojai-testing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837275,"owners_count":21169373,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","mapr","mapr-db","mapr-json","mapr-ojai","maprdb","ojai","scala","testing"],"created_at":"2024-11-16T00:23:56.450Z","updated_at":"2025-04-14T06:52:41.985Z","avatar_url":"https://github.com/anicolaspp.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ojai-testing\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.anicolaspp/ojai-testing_2.11/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.anicolaspp/ojai-testing_2.11)\n[![Build Status](https://travis-ci.org/anicolaspp/ojai-testing.svg?branch=master)](https://travis-ci.org/anicolaspp/ojai-testing)\n[![codecov](https://codecov.io/gh/anicolaspp/ojai-testing/branch/master/graph/badge.svg)](https://codecov.io/gh/anicolaspp/ojai-testing)\n\nEmbeded In-Memory Implementation of OJAI Driver to be used in testing\n\nThe aim of this project is to provide to a way of running an OJAI Document Store in memory that can be used for mocking \nMapR Database when running test suits.\n\nIn order to use our in memory store we should first register the correct driver as follow.\n\n```scala\n DriverManager.registerDriver(InMemoryDriver)\n```\n\nAlternatively, you can mix in the `OjaiTesting` trait and it will automatically register our driver.\n\n```scala\nclass AutoRegisterForTest extends FlatSpec with ScalaOjaiTesting with Matchers {\n\n  it should \"be ready\" in {\n\n    getConnection().isInstanceOf[InMemoryConnection] should be (true)\n\n    getConnection().getStore(\"anicolaspp/mem\") should be (documentStore(\"anicolaspp/mem\"))\n  }\n\n}\n```\n\nThen we can get a `Connection` and a `DocumentStore` as we normally do when connecting to a MapR Database table.\n\n```scala\nval connection = DriverManager.getConnection(\"ojai:anicolaspp:mem\")\n\nval store = connection.getStore(\"anicolaspp/mem\")\n```\nThe `storeName` can be any name you want. \n\nThere will be only a single instance per store name so the following holds.\n\n```scala\nclass SomeTests extends FlatSpec with ScalaOjaiTesting with Matchers {\n\n  it should \"keep track of stores\" in {\n    val store = getConnection().getStore(\"anicolaspp/my_store\")\n\n    store should be (getConnection().getStore(\"anicolaspp/my_store\"))\n    \n    documentStore(\"anicolaspp/a_store\") should be (getConnection().getStore(\"anicolaspp/a_store\"))\n    \n    store should not be (documentStore(\"anicolaspp/a_store\"))\n  }\n}\n```\nWhen running multiple tests, you can share the same store instance across all of them. \n\n```scala\nclass ShareStoreTests extends FlatSpec with ScalaOjaiTesting with Matchers {\n\n  it should \"keep values on store\" in {\n    val store = getConnection().getStore(\"my_store\")\n \n    store.insert(getConnection().newDocument().setId(\"5\"))\n    \n    store.find().iterator.hasNext() should be (true)\n  }\n  \n   it should \"has values on it\" in {\n    val store = getConnection().getStore(\"my_store\")\n\n    val it = store.find().iterator\n    \n    it.hasNext should be (true)\n    it.next.getId().toString() should be (\"5\")\n  }\n\n}\n```\n\nHowever, this behavior might not desirable under certain conditions. In those case, you can clean the stores by calling `.close()` on the store.\n\n```scala\nclass ConnectionResetTest extends FlatSpec\n  with ScalaOjaiTesting\n  with Matchers\n  with BeforeAndAfterEach {\n\n  it should \"insert clean up on reset\" in {\n    val store = documentStore(\"someStore\")\n    store.insert(getConnection().newDocument().setId(\"hello\"))\n\n    store.find().iterator().hasNext should be (true)\n\n    val sameStore = documentStore(\"someStore\")\n    sameStore.find().iterator().hasNext should be (true)\n\n    sameStore.close()\n\n    val emptyStore = documentStore(\"someStore\")\n    emptyStore.find().iterator().hasNext should be (false)\n  }\n\n}\n```\n\nIn order to clean after each test, you can use `override def beforeEach(): Unit = getConnection().close()`\n\n\nNotice we are mixing in the `ScalaOjaiTesting` trait to auto register the correct driver and to have access to \n`getConnection` and `documentStore`.\n\nAfter we have gained access to the `DocumentStore` we should be able to run OJAI queries on it. \n\n```scala\nobject Run extends OjaiTesting {\n\n def run() = {\n  store = documentStore(\"someStore\")\n\n  val doc = getConnection().newDocument().set(\"name\", \"nico\").set(\"age\", 30).set(\"_id\", \"1\")\n\n  store.insert(doc)\n  assert(store.find().asScala.toList.length == 1)\n\n  store.insert(getConnection().newDocument().set(\"name\", \"nico\").set(\"age\", 30).set(\"_id\", \"2\"))\n  assert(store.find().asScala.toList.length == 2)\n\n  store.delete(\"1\")\n  assert(store.find().asScala.toList.length == 1)\n\n  store.increment(\"2\", \"age\", 2)\n  store.increment(\"a\", \"asd\", 2)\n\n  store\n   .find()\n   .asScala\n   .foreach(println)\n } \n \n}\n```\n\n## Using Java\n\nIf you want to use this library from Java, that can be done without drawbacks since we have the same testing facilities ready to be used. \n\n```java\npublic class JavaTesting extends JavaOjaiTesting {\n    \n    @Test\n    public void testGetConnection() {\n        assert getConnection() instanceof InMemoryConnection;\n    }\n    \n    @Test\n    public void testGetStore() {\n        assert documentStore(\"anicolaspp/java_store\") instanceof InMemoryStore;\n    }\n}\n```\n\nNotice that by implementing `JavaOjaiTesting` we gain access to the same constructs, `getConnection()` and \n`documentStore()` and from here we just write our normal Java tests using the testing framework of your choice.\n\nIf extending the test class with `JavaOjaiTesting` is not possible, it is also possible to use a composition.\n```java\npublic class JavaTesting extends SomeOtherBaseClass {\n    private JavaOjaiTesting testObject = new JavaOjaiTesting();\n    \n    @Test\n    public void testGetConnection() {\n        assert testObject.getConnection() instanceof InMemoryConnection;\n    }\n    \n    @Test\n    public void testGetStore() {\n        assert testObject.documentStore(\"anicolaspp/java_store\") instanceof InMemoryStore;\n    }\n}\n```\n\n## Connection options\nIn some scenarios you can specify further options for the in memory ojai connection. These options are available:\n\n| Option | Description | Default |\n| ------ | ----------- | ------- |\n| `ojai.in-memory.store.clear-store-on-close` | As default the data in an in memory store will be cleared on closing the store. Set this option to `false` to preserve the state of the store after closing. | `true` |\n\nThe options can be passed to `DriverManager.getConnection(url, options)`.\n\n`com.github.anicolaspp.ojai.ConnectionOptions` defines a constant for each option.\n\n```scala\nDriverManager.registerDriver(InMemoryDriver)\nval options = Json.newDocument().set(ConnectionOptions.clearStoreOnCloseOption, false)\nval connection = DriverManager.getConnection(\"ojai:anicolaspp:mem\", options)\n```\n\nIn Java `JavaOjaiTesting` also provides a easy access for creating connections with options.\n```Java\npublic class Test extends JavaOjaiTesting {\n    public Test() {\n        super(false);\n    }\n\n    @Test\n    public void testGetConnection() {\n        Connection connection = getConnection()\n        // ...\n    }\n}\n```\n\n## Cross Build\n\nThis project is cross built for Scala `2.11.8` and `2.12.8`. Make sure you select the right version for your Scala.\n\n## Linking\n\n### Maven\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.github.anicolaspp\u003c/groupId\u003e\n  \u003cartifactId\u003eojai-testing_2.X\u003c/artifactId\u003e\n  \u003cversion\u003e1.0.12\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### SBT\n\n```sbt\nlibraryDependencies += \"com.github.anicolaspp\" % \"ojai-testing_2.X\" % \"1.0.12\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanicolaspp%2Fojai-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanicolaspp%2Fojai-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanicolaspp%2Fojai-testing/lists"}