From 69dff7eef60ea4aa91b06592b1f9aea5ab6105b6 Mon Sep 17 00:00:00 2001 From: Tsvetozar Bonev Date: Thu, 27 Sep 2018 15:14:13 +0300 Subject: [PATCH] v0.1: final clean up --- build.gradle | 3 +- nharker-core/build.gradle | 2 - .../nitrite/NitriteArticleSynonymProvider.kt | 10 +- .../adapter/nitrite/NitriteArticles.kt | 57 +++++++----- .../adapter/nitrite/NitriteCatalogues.kt | 93 +++++++++++-------- .../nitrite/NitriteEntityTrashCollector.kt | 7 +- .../nharker/adapter/nitrite/NitriteEntries.kt | 12 +-- .../com/tsbonev/nharker/core/Article.kt | 17 ++-- .../tsbonev/nharker/core/ArticleProperties.kt | 2 +- .../tsbonev/nharker/core/ArticleRequest.kt | 2 +- .../nharker/core/ArticleSynonymProvider.kt | 9 +- .../com/tsbonev/nharker/core/Articles.kt | 21 +++-- .../com/tsbonev/nharker/core/Catalogue.kt | 22 ++--- .../com/tsbonev/nharker/core/Catalogues.kt | 20 ++-- .../com/tsbonev/nharker/core/Entries.kt | 21 +++-- .../kotlin/com/tsbonev/nharker/core/Entry.kt | 15 +-- .../tsbonev/nharker/core/SimpleEntryLinker.kt | 9 +- .../tsbonev/nharker/core/TrashCollector.kt | 4 +- .../nharker/core/helpers/EntityConverters.kt | 4 +- .../nharker/core/helpers/OrderedMap.kt | 14 +-- .../tsbonev/nharker/core/helpers/StubClock.kt | 1 + .../NitriteArticleSynonymProviderTest.kt | 14 +-- .../adapter/nitrite/NitriteArticlesTest.kt | 25 ++++- .../adapter/nitrite/NitriteCataloguesTest.kt | 78 ++++++++-------- .../NitriteEntityTrashCollectorTest.kt | 22 ++--- .../adapter/nitrite/NitriteEntriesTest.kt | 24 ++--- .../nharker/core/SimpleEntryLinkerTest.kt | 8 +- .../core/helpers/OrderedMapHelpersTest.kt | 12 +-- .../nharker/core/helpers/StubClockTest.kt | 8 +- 29 files changed, 290 insertions(+), 246 deletions(-) diff --git a/build.gradle b/build.gradle index e7a75fb..8e75a32 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ repositories { } buildscript { - ext.kotlinVersion = "1.2.70" + ext.kotlinVersion = "1.2.71" ext.nitriteVersion = "3.1.0" repositories { @@ -19,7 +19,6 @@ buildscript { } } - allprojects { apply plugin: "jacoco" diff --git a/nharker-core/build.gradle b/nharker-core/build.gradle index 1fea7bb..7fc7022 100644 --- a/nharker-core/build.gradle +++ b/nharker-core/build.gradle @@ -1,5 +1,3 @@ -group 'com.tsbonev.nharker' - repositories { mavenCentral() mavenLocal() diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProvider.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProvider.kt index 444d501..1b59ad1 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProvider.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProvider.kt @@ -16,9 +16,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite, private val collectionName: String = "Article_synonyms", private val globalMapId: String = "Global_synonym_map") : ArticleSynonymProvider { - /** - * Retrieve the repository on every request. - */ + private val coll: NitriteCollection get() = nitriteDb.getCollection(collectionName) @@ -33,7 +31,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite, override fun addSynonym(articleSynonym: String, article: Article): String { val map = getSynonymMap().toMutableMap() - if(map[articleSynonym] != null) throw SynonymAlreadyTakenException() + if (map[articleSynonym] != null) throw SynonymAlreadyTakenException() map[articleSynonym] = article.linkTitle @@ -44,7 +42,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite, override fun removeSynonym(articleSynonym: String): String { val map = getSynonymMap().toMutableMap() - if(map[articleSynonym] == null) throw SynonymNotFoundException() + if (map[articleSynonym] == null) throw SynonymNotFoundException() map.remove(articleSynonym) @@ -59,7 +57,7 @@ class NitriteArticleSynonymProvider(private val nitriteDb: Nitrite, * @param synonymMap The map whose values to save. * @return A Document of the map. */ - private fun updateOrCreateMap(synonymMap: Map): Document{ + private fun updateOrCreateMap(synonymMap: Map): Document { val mapDocument = coll.find("globalId" eq globalMapId).firstOrNull() ?: Document.createDocument("globalId", globalMapId) diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticles.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticles.kt index f65c9db..1642dc6 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticles.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticles.kt @@ -16,24 +16,21 @@ import java.util.Optional class NitriteArticles(private val nitriteDb: Nitrite, private val collectionName: String = "Articles", private val entryLinker: EntryLinker, - private val clock: Clock = Clock.systemUTC()) : Articles { + private val clock: Clock = Clock.systemUTC()) + : Articles { - /** - * Retrieve the repository on every request. - */ private val coll: ObjectRepository
get() = nitriteDb.getRepository(collectionName, Article::class.java) - @Throws(ArticleTitleTakenException::class) override fun create(articleRequest: ArticleRequest): Article { val article = Article( NitriteId.newId().toString(), - articleRequest.title.toLinkTitle(), - articleRequest.title, + articleRequest.fullTitle.toLinkTitle(), + articleRequest.fullTitle, LocalDateTime.now(clock) ) - if(coll.find(Article::linkTitle eq article.linkTitle).firstOrNull() != null) + if (coll.find(Article::linkTitle eq article.linkTitle).firstOrNull() != null) throw ArticleTitleTakenException() coll.insert(article) @@ -46,35 +43,46 @@ class NitriteArticles(private val nitriteDb: Nitrite, } override fun getById(articleId: String): Optional
{ - val article = coll.find(Article::id eq articleId).firstOrNull() ?: return Optional.empty() + val article = coll.find(Article::id eq articleId).firstOrNull() + ?: return Optional.empty() + return Optional.of(article) } + override fun delete(articleId: String): Article { + val article = findByIdOrThrow(articleId) + + coll.remove(article) + return article + } + override fun appendEntry(articleId: String, entry: Entry): Entry { val article = findByIdOrThrow(articleId) - if(article.entries.containsKey(entry.id)) throw EntryAlreadyInArticleException() + if (article.entries.containsKey(entry.id)) throw EntryAlreadyInArticleException() handleArticleLinks(article, entry, true) val updatedArticle = article - .copy(entries = article.entries.append(entry.id)) + .copy(entries = article.entries + .append(entry.id)) coll.update(updatedArticle) - return entry } override fun removeEntry(articleId: String, entry: Entry): Entry { val article = findByIdOrThrow(articleId) - if(!article.entries.contains(entry.id)) throw EntryNotInArticleException() + if (!article.entries.contains(entry.id)) throw EntryNotInArticleException() handleArticleLinks(article, entry, false) - val updatedArticle = article.copy(entries = article.entries.subtract(entry.id)) - coll.update(updatedArticle) + val updatedArticle = article + .copy(entries = article.entries + .subtract(entry.id)) + coll.update(updatedArticle) return entry } @@ -82,10 +90,13 @@ class NitriteArticles(private val nitriteDb: Nitrite, val article = findByIdOrThrow(articleId) return try { - val updatedArticle = article.copy(entries = article.entries.switch(first.id, second.id)) + val updatedArticle = article + .copy(entries = article.entries + .switch(first.id, second.id)) + coll.update(updatedArticle) updatedArticle - }catch (ex: ElementNotInMapException){ + } catch (ex: ElementNotInMapException) { throw EntryNotInArticleException() } } @@ -111,7 +122,7 @@ class NitriteArticles(private val nitriteDb: Nitrite, override fun getArticleTitles(linkTitleList: Set): List { val fullTitleList = mutableListOf() - linkTitleList.forEach{ + linkTitleList.forEach { val article = coll.find(Article::linkTitle eq it) .project(ArticleFullTitle::class.java).firstOrNull() ?: return@forEach fullTitleList.add(article.fullTitle) @@ -123,7 +134,7 @@ class NitriteArticles(private val nitriteDb: Nitrite, /** * Finds an article by id or throws an exception. */ - private fun findByIdOrThrow(articleId: String): Article{ + private fun findByIdOrThrow(articleId: String): Article { return coll.find(Article::id eq articleId).firstOrNull() ?: throw ArticleNotFoundException() } @@ -132,7 +143,7 @@ class NitriteArticles(private val nitriteDb: Nitrite, * * @return List of article link titles. */ - private fun getArticleLinkTitles(): List{ + private fun getArticleLinkTitles(): List { val projectedArticleTitles = coll .find() .project(ArticleLinkTitle::class.java) @@ -153,17 +164,17 @@ class NitriteArticles(private val nitriteDb: Nitrite, * @param entry The entry whose links are up for modification. * @param adding Whether or not the links should be added or removed. */ - private fun handleArticleLinks(article: Article, entry: Entry, adding: Boolean){ + private fun handleArticleLinks(article: Article, entry: Entry, adding: Boolean) { val entryLinks = entryLinker.findArticleLinks( entry, getArticleLinkTitles() ) - if(adding){ + if (adding) { entryLinks.forEach { article.links.addLink(it) } - }else{ + } else { entryLinks.forEach { article.links.removeLink(it) } diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCatalogues.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCatalogues.kt index 1d7ef83..7d51222 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCatalogues.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCatalogues.kt @@ -15,15 +15,14 @@ import java.util.Optional */ class NitriteCatalogues(private val nitriteDb: Nitrite, private val collectionName: String = "Catalogues", - private val clock: Clock = Clock.systemUTC()) : Catalogues { - /** - * Retrieve the repository on every request. - */ + private val clock: Clock = Clock.systemUTC()) + : Catalogues { + private val coll: ObjectRepository get() = nitriteDb.getRepository(collectionName, Catalogue::class.java) override fun create(catalogueRequest: CatalogueRequest): Catalogue { - if(coll.find(Catalogue::title eq catalogueRequest.title).firstOrNull() != null) + if (coll.find(Catalogue::title eq catalogueRequest.title).firstOrNull() != null) throw CatalogueTitleTakenException() val catalogue = Catalogue( @@ -43,14 +42,17 @@ class NitriteCatalogues(private val nitriteDb: Nitrite, } override fun getById(catalogueId: String): Optional { - val catalogue = coll.find(Catalogue::id eq catalogueId).firstOrNull() ?: return Optional.empty() + val catalogue = coll.find(Catalogue::id eq catalogueId).firstOrNull() + ?: return Optional.empty() + return Optional.of(catalogue) } override fun changeTitle(catalogueId: String, newTitle: String): Catalogue { val catalogue = findOrThrow(catalogueId) - if(coll.find(Catalogue::title eq newTitle).firstOrNull() != null) throw CatalogueTitleTakenException() + if (coll.find(Catalogue::title eq newTitle).firstOrNull() != null) + throw CatalogueTitleTakenException() val updatedCatalogue = catalogue.copy(title = newTitle) @@ -61,19 +63,20 @@ class NitriteCatalogues(private val nitriteDb: Nitrite, override fun changeParentCatalogue(childCatalogueId: String, parentCatalogue: Catalogue): Catalogue { val childCatalogue = findOrThrow(childCatalogueId) - if(childCatalogue.parentCatalogue == parentCatalogue.id) throw CatalogueAlreadyAChildException() + if (childCatalogue.parentCatalogue == parentCatalogue.id) throw CatalogueAlreadyAChildException() - if(childCatalogue.id == parentCatalogue.id) throw SelfContainedCatalogueException() + if (childCatalogue.id == parentCatalogue.id) throw SelfContainedCatalogueException() - if(parentCatalogue.parentCatalogue == childCatalogue.id) throw CatalogueCircularInheritanceException() + if (parentCatalogue.parentCatalogue == childCatalogue.id) throw CatalogueCircularInheritanceException() - val updatedChild = childCatalogue.copy(parentCatalogue = parentCatalogue.id) - val updatedParent = parentCatalogue.copy(subCatalogues = parentCatalogue.subCatalogues - .append(childCatalogueId)) + val updatedChild = childCatalogue + .copy(parentCatalogue = parentCatalogue.id) + val updatedParent = parentCatalogue + .copy(subCatalogues = parentCatalogue.subCatalogues + .append(childCatalogueId)) coll.update(updatedChild) coll.update(updatedParent) - return updatedChild } @@ -81,64 +84,69 @@ class NitriteCatalogues(private val nitriteDb: Nitrite, val catalogue = findOrThrow(catalogueId) coll.remove(Catalogue::id eq catalogueId) - return catalogue } override fun appendSubCatalogue(parentCatalogueId: String, childCatalogue: Catalogue): Catalogue { - if(childCatalogue.parentCatalogue == parentCatalogueId) throw CatalogueAlreadyAChildException() + if (childCatalogue.parentCatalogue == parentCatalogueId) throw CatalogueAlreadyAChildException() - if(childCatalogue.id == parentCatalogueId) throw SelfContainedCatalogueException() + if (childCatalogue.id == parentCatalogueId) throw SelfContainedCatalogueException() val parentCatalogue = findOrThrow(parentCatalogueId) - if(parentCatalogue.parentCatalogue == childCatalogue.id) throw CatalogueCircularInheritanceException() + if (parentCatalogue.parentCatalogue == childCatalogue.id) throw CatalogueCircularInheritanceException() - val updatedChild = childCatalogue.copy(parentCatalogue = parentCatalogueId) - val updatedParent = parentCatalogue.copy(subCatalogues = parentCatalogue.subCatalogues.append(childCatalogue.id)) + val updatedChild = childCatalogue + .copy(parentCatalogue = parentCatalogueId) + val updatedParent = parentCatalogue + .copy(subCatalogues = parentCatalogue.subCatalogues + .append(childCatalogue.id)) coll.update(updatedChild) coll.update(updatedParent) - return updatedChild } override fun removeSubCatalogue(parentCatalogueId: String, childCatalogue: Catalogue): Catalogue { - if(childCatalogue.parentCatalogue != parentCatalogueId) throw CatalogueNotAChildException() + if (childCatalogue.parentCatalogue != parentCatalogueId) throw CatalogueNotAChildException() val parentCatalogue = findOrThrow(parentCatalogueId) - val updatedChild = childCatalogue.copy(parentCatalogue = null) + val updatedChild = childCatalogue + .copy(parentCatalogue = null) - val updatedParent = parentCatalogue.copy(subCatalogues = parentCatalogue.subCatalogues.subtract(childCatalogue.id)) + val updatedParent = parentCatalogue + .copy(subCatalogues = parentCatalogue.subCatalogues + .subtract(childCatalogue.id)) coll.update(updatedChild) coll.update(updatedParent) - return updatedChild } override fun appendArticle(parentCatalogueId: String, article: Article): Article { val catalogue = findOrThrow(parentCatalogueId) - if(catalogue.articles.containsKey(article.id)) throw ArticleAlreadyInCatalogueException() + if (catalogue.articles.containsKey(article.id)) throw ArticleAlreadyInCatalogueException() - val updatedCatalogue = catalogue.copy(articles = catalogue.articles.append(article.id)) + val updatedCatalogue = catalogue + .copy(articles = catalogue.articles + .append(article.id)) coll.update(updatedCatalogue) - return article } override fun removeArticle(parentCatalogueId: String, article: Article): Article { val catalogue = findOrThrow(parentCatalogueId) - if(!catalogue.articles.containsKey(article.id)) throw ArticleNotInCatalogueException() + if (!catalogue.articles.containsKey(article.id)) throw ArticleNotInCatalogueException() - val updatedCatalogue = catalogue.copy(articles = catalogue.articles.subtract(article.id)) + val updatedCatalogue = catalogue + .copy(articles = catalogue.articles + .subtract(article.id)) coll.update(updatedCatalogue) - return article } @@ -146,22 +154,30 @@ class NitriteCatalogues(private val nitriteDb: Nitrite, val catalogue = findOrThrow(catalogueId) return try { - val updatedCatalogue = catalogue.copy(articles = catalogue.articles.switch(first.id, second.id)) + val updatedCatalogue = catalogue + .copy(articles = catalogue.articles + .switch(first.id, second.id)) + coll.update(updatedCatalogue) updatedCatalogue - }catch (ex: ElementNotInMapException){ + } catch (ex: ElementNotInMapException) { throw ArticleNotInCatalogueException() } } - override fun switchSubCatalogues(parentCatalogueId: String, firstChild: Catalogue, secondChild: Catalogue): Catalogue { + override fun switchSubCatalogues(parentCatalogueId: String, + firstChild: Catalogue, + secondChild: Catalogue): Catalogue { val catalogue = findOrThrow(parentCatalogueId) return try { - val updatedCatalogue = catalogue.copy(subCatalogues = catalogue.subCatalogues.switch(firstChild.id, secondChild.id)) + val updatedCatalogue = catalogue + .copy(subCatalogues = catalogue.subCatalogues + .switch(firstChild.id, secondChild.id)) + coll.update(updatedCatalogue) updatedCatalogue - }catch (ex: ElementNotInMapException){ + } catch (ex: ElementNotInMapException) { throw CatalogueNotAChildException() } } @@ -169,7 +185,8 @@ class NitriteCatalogues(private val nitriteDb: Nitrite, /** * Finds a catalogue by id or throws an exception. */ - private fun findOrThrow(catalogueId: String): Catalogue{ - return coll.find(Catalogue::id eq catalogueId).firstOrNull() ?: throw CatalogueNotFoundException() + private fun findOrThrow(catalogueId: String): Catalogue { + return coll.find(Catalogue::id eq catalogueId).firstOrNull() + ?: throw CatalogueNotFoundException() } } \ No newline at end of file diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollector.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollector.kt index 05097a7..6e6ae68 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollector.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollector.kt @@ -12,10 +12,9 @@ import org.dizitart.no2.NitriteCollection * @author Tsvetozar Bonev (tsbonev@gmail.com) */ class NitriteEntityTrashCollector(private val nitriteDb: Nitrite, - private val collectionName: String = "Entity_trash") : TrashCollector { - /** - * Retrieve the repository on every request. - */ + private val collectionName: String = "Entity_trash") + : TrashCollector { + private val coll: NitriteCollection get() = nitriteDb.getCollection(collectionName) diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntries.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntries.kt index 61f677e..d162818 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntries.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntries.kt @@ -15,11 +15,9 @@ import java.util.Optional */ class NitriteEntries(private val nitriteDb: Nitrite, private val collectionName: String = "Entries", - private val clock: Clock = Clock.systemUTC()) : Entries { + private val clock: Clock = Clock.systemUTC()) + : Entries { - /** - * Retrieve the repository on every request. - */ private val coll: ObjectRepository get() = nitriteDb.getRepository(collectionName, Entry::class.java) @@ -41,7 +39,9 @@ class NitriteEntries(private val nitriteDb: Nitrite, } override fun getById(entryId: String): Optional { - val entry = coll.find(Entry::id eq entryId).firstOrNull() ?: return Optional.empty() + val entry = coll.find(Entry::id eq entryId).firstOrNull() + ?: return Optional.empty() + return Optional.of(entry) } @@ -74,7 +74,7 @@ class NitriteEntries(private val nitriteDb: Nitrite, /** * Finds an entry by id or throws an exception. */ - private fun findByIdOrThrow(entryId: String): Entry{ + private fun findByIdOrThrow(entryId: String): Entry { return coll.find(Entry::id eq entryId).firstOrNull() ?: throw EntryNotFoundException() } } \ No newline at end of file diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Article.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Article.kt index 550e751..e19eb62 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Article.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Article.kt @@ -8,10 +8,9 @@ import java.time.LocalDateTime /** * Articles are the main building block of - * NHarker's organization scheme, they group - * entries together into different information - * categories and consolidate their links into - * one accessible map. + * NHarker's organization scheme, they keep track + * of entries and handle automatically linking + * to each other based on each entry's content. * * @author Tsvetozar Bonev (tsbonev@gmail.com) */ @@ -30,7 +29,7 @@ data class Article(@Id val id: String, */ fun String.toLinkTitle(): String { return this.toLowerCase() - .replace(' ', '-') + .replace("\\s+".toRegex(), "-") .replace("\'", "") .replace(",", "") .replace(".", "") @@ -46,8 +45,8 @@ data class ArticleLinkTitle(val linkTitle: String) */ data class ArticleFullTitle(val fullTitle: String) -class ArticleNotFoundException : Exception() -class ArticleTitleTakenException : Exception() +class ArticleNotFoundException : Throwable() +class ArticleTitleTakenException : Throwable() -class EntryAlreadyInArticleException : Exception() -class EntryNotInArticleException : Exception() \ No newline at end of file +class EntryAlreadyInArticleException : Throwable() +class EntryNotInArticleException : Throwable() \ No newline at end of file diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleProperties.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleProperties.kt index 9613308..2bc5f8b 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleProperties.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleProperties.kt @@ -20,4 +20,4 @@ data class ArticleProperties (private val map: MutableMap = mutab } } -class PropertyNotFoundException : Exception() \ No newline at end of file +class PropertyNotFoundException : Throwable() \ No newline at end of file diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleRequest.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleRequest.kt index be6dfa7..f589d64 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleRequest.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleRequest.kt @@ -3,4 +3,4 @@ package com.tsbonev.nharker.core /** * @author Tsvetozar Bonev (tsbonev@gmail.com) */ -data class ArticleRequest (val title: String) \ No newline at end of file +data class ArticleRequest (val fullTitle: String) \ No newline at end of file diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleSynonymProvider.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleSynonymProvider.kt index de99a15..08bf662 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleSynonymProvider.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/ArticleSynonymProvider.kt @@ -1,8 +1,7 @@ package com.tsbonev.nharker.core /** - * Provides the methods to persist a map of synonyms that ease - * article linking. + * Provides the methods to persist a map of synonyms that ease article linking. * * @author Tsvetozar Bonev (tsbonev@gmail.com) */ @@ -10,7 +9,7 @@ interface ArticleSynonymProvider { /** * Returns a map of article link synonyms. * - * @return A map of the synonym link and the real link. + * @return A map of synonym links to real links. */ fun getSynonymMap(): Map @@ -34,5 +33,5 @@ interface ArticleSynonymProvider { fun removeSynonym(articleSynonym: String): String } -class SynonymNotFoundException: Exception() -class SynonymAlreadyTakenException: Exception() \ No newline at end of file +class SynonymNotFoundException : Throwable() +class SynonymAlreadyTakenException : Throwable() \ No newline at end of file diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Articles.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Articles.kt index 67caea1..59693b2 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Articles.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Articles.kt @@ -3,25 +3,34 @@ package com.tsbonev.nharker.core import java.util.Optional /** + * Provides the methods to handle articles in persistence. + * * @author Tsvetozar Bonev (tsbonev@gmail.com) */ interface Articles { - /** - * Creates an article from a request and saves it into persistence. + * Creates an article from a request and saves it. * * @param articleRequest The article to be created. * @return The created article. */ @Throws(ArticleTitleTakenException::class) - fun create(articleRequest: ArticleRequest) : Article + fun create(articleRequest: ArticleRequest): Article /** - * Saves an article into persistence, overwriting the previous one - * if it exists. + * Saves an article, overwriting the previous one if it exists. */ fun save(article: Article): Article + /** + * Deletes an article. + * + * @param articleId The id of the article to delete. + * @return The deleted article. + */ + @Throws(ArticleNotFoundException::class) + fun delete(articleId: String): Article + /** * Retrieves an optional article by value. * @@ -53,7 +62,7 @@ interface Articles { fun removeEntry(articleId: String, entry: Entry): Entry /** - * Switches two entries in the article. + * Switches two entries in an article. * * @param articleId The value of the article. * @param first The first entry. diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogue.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogue.kt index 175f8ff..a7a00ca 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogue.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogue.kt @@ -21,19 +21,15 @@ data class Catalogue(@Id val id: String, val creationDate: LocalDateTime, val articles: Map = emptyMap(), val subCatalogues: Map = emptyMap(), - val parentCatalogue: String? = null){ - fun hasParent(): Boolean { - return this.parentCatalogue != null - } -} + val parentCatalogue: String? = null) -class CatalogueNotFoundException : Exception() -class CatalogueTitleTakenException : Exception() +class CatalogueNotFoundException : Throwable() +class CatalogueTitleTakenException : Throwable() -class CatalogueAlreadyAChildException : Exception() -class CatalogueNotAChildException : Exception() -class SelfContainedCatalogueException : Exception() -class CatalogueCircularInheritanceException : Exception() +class CatalogueAlreadyAChildException : Throwable() +class CatalogueNotAChildException : Throwable() +class SelfContainedCatalogueException : Throwable() +class CatalogueCircularInheritanceException : Throwable() -class ArticleAlreadyInCatalogueException : Exception() -class ArticleNotInCatalogueException : Exception() +class ArticleAlreadyInCatalogueException : Throwable() +class ArticleNotInCatalogueException : Throwable() diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogues.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogues.kt index 04878f7..7b4cc36 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogues.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Catalogues.kt @@ -3,6 +3,8 @@ package com.tsbonev.nharker.core import java.util.Optional /** + * Provides the methods to handle catalogues in persistence. + * * @author Tsvetozar Bonev (tsbonev@gmail.com) */ interface Catalogues { @@ -29,6 +31,15 @@ interface Catalogues { */ fun getById(catalogueId: String): Optional + /** + * Deletes a catalogue by value. + * + * @param catalogueId The value of the catalogue targeted. + * @return The deleted catalogue. + */ + @Throws(CatalogueNotFoundException::class) + fun delete(catalogueId: String): Catalogue + /** * Changes the title of a catalogue. * @@ -51,15 +62,6 @@ interface Catalogues { CatalogueAlreadyAChildException::class) fun changeParentCatalogue(childCatalogueId: String, parentCatalogue: Catalogue): Catalogue - /** - * Deletes a catalogue by value. - * - * @param catalogueId The value of the catalogue targeted. - * @return The deleted catalogue. - */ - @Throws(CatalogueNotFoundException::class) - fun delete(catalogueId: String): Catalogue - /** * Appends a catalogue to the targeted catalogue's children list. * diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entries.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entries.kt index d49fdde..45cca98 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entries.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entries.kt @@ -3,10 +3,11 @@ package com.tsbonev.nharker.core import java.util.Optional /** + * Provides the methods to handle entries in persistence. + * * @author Tsvetozar Bonev (tsbonev@gmail.com) */ interface Entries { - /** * Creates an Entry from an EntryRequest and saves it into persistence. * @@ -21,6 +22,15 @@ interface Entries { */ fun save(entry: Entry): Entry + /** + * Deletes an entry. + * + * @param entryId The Id of the entry targeted. + * @return The deleted entry. + */ + @Throws(EntryNotFoundException::class) + fun delete(entryId: String): Entry + /** * Updates the content of an entry. * @@ -41,15 +51,6 @@ interface Entries { @Throws(EntryNotFoundException::class) fun updateLinks(entryId: String, links: Map): Entry - /** - * Deletes an entry. - * - * @param entryId The Id of the entry targeted. - * @return The deleted entry. - */ - @Throws(EntryNotFoundException::class) - fun delete(entryId: String): Entry - /** * Returns an optional entry by value. * diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entry.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entry.kt index e746cfe..5f62539 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entry.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/Entry.kt @@ -8,15 +8,16 @@ import java.time.LocalDateTime /** * Entries are the main wrapper of information, - * all entries are owned by an article and contain - * a set of phrases that should point to other articles. + * they may contain links that are explicit which + * will be used by their parent article alongside their + * content for linking purposes. * * @author Tsvetozar Bonev (tsbonev@gmail.com) */ @Indices(Index(value = "content", type = IndexType.Fulltext)) -data class Entry (@Id val id: String, - val creationDate: LocalDateTime, - val content: String, - val links: Map = emptyMap()) +data class Entry(@Id val id: String, + val creationDate: LocalDateTime, + val content: String, + val links: Map = emptyMap()) -class EntryNotFoundException : Exception() +class EntryNotFoundException : Throwable() diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/SimpleEntryLinker.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/SimpleEntryLinker.kt index 6f92790..9cdc0d4 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/SimpleEntryLinker.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/SimpleEntryLinker.kt @@ -3,7 +3,8 @@ package com.tsbonev.nharker.core /** * @author Tsvetozar Bonev (tsbonev@gmail.com) */ -class SimpleEntryLinker(private val articleSynonymProvider: ArticleSynonymProvider) : EntryLinker { +class SimpleEntryLinker(private val articleSynonymProvider: ArticleSynonymProvider) + : EntryLinker { private val punctuations = listOf( "!", "?", ".", ",", ":", ";", @@ -37,10 +38,10 @@ class SimpleEntryLinker(private val articleSynonymProvider: ArticleSynonymProvid * @param linkSet The collection to add the found links to. * @param linkMap The map of links to match. */ - private fun String.mapLinks(linkSet: MutableSet, linkMap: Map): String{ + private fun String.mapLinks(linkSet: MutableSet, linkMap: Map): String { var contentHolder = this - linkMap.forEach{ - if(contentHolder.contains("-${it.key}-")){ + linkMap.forEach { + if (contentHolder.contains("-${it.key}-")) { contentHolder = contentHolder.replace("-${it.key}-", "-#-") linkSet.add(it.value) } diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/TrashCollector.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/TrashCollector.kt index 3f864b5..3444190 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/TrashCollector.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/TrashCollector.kt @@ -1,7 +1,5 @@ package com.tsbonev.nharker.core -import java.lang.Exception - /** * Provides the methods to store a deleted entity in a * collection and be retrieved on a later basis. @@ -41,4 +39,4 @@ interface TrashCollector { fun clear() } -class EntityNotInTrashException: Exception() \ No newline at end of file +class EntityNotInTrashException : Throwable() \ No newline at end of file diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/EntityConverters.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/EntityConverters.kt index 485b9c6..df3f4db 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/EntityConverters.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/EntityConverters.kt @@ -5,13 +5,13 @@ import org.dizitart.no2.Document import org.dizitart.no2.NitriteId /** - * Converters used by the trash store utilizing Jackson. + * Converters used by the trash store. * * @author Tsvetozar Bonev (tsbonev@gmail.com) */ /** - * Converts object to a document with three fields, + * Converts object to a document with three fields: * a class, an id and a json body. */ fun Any.toDocument(): Document { diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/OrderedMap.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/OrderedMap.kt index 546cd76..f7651ca 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/OrderedMap.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/OrderedMap.kt @@ -4,9 +4,7 @@ package com.tsbonev.nharker.core.helpers * @author Tsvetozar Bonev (tsbonev@gmail.com) */ -class ElementNotInMapException : Exception() - -fun Map.switch(first: T, second: T): Map{ +fun Map.switch(first: T, second: T): Map { val mutableMap = this.toMutableMap() val firstVal = mutableMap[first] ?: throw ElementNotInMapException() val secondVal = mutableMap[second] ?: throw ElementNotInMapException() @@ -17,21 +15,23 @@ fun Map.switch(first: T, second: T): Map{ return mutableMap } -fun Map.append(value: T): Map{ +fun Map.append(value: T): Map { val mutableMap = this.toMutableMap() mutableMap[value] = mutableMap.size return mutableMap } -fun Map.subtract(value: T): Map{ +fun Map.subtract(value: T): Map { val mutableMap = this.toMutableMap() val removedSpace = mutableMap[value] ?: throw ElementNotInMapException() mutableMap.remove(value) mutableMap.forEach { - if(it.value > removedSpace) mutableMap[it.key] = it.value - 1 + if (it.value > removedSpace) mutableMap[it.key] = it.value - 1 } return mutableMap -} \ No newline at end of file +} + +class ElementNotInMapException : Throwable() diff --git a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/StubClock.kt b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/StubClock.kt index 7d84eea..be693d3 100644 --- a/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/StubClock.kt +++ b/nharker-core/src/main/kotlin/com/tsbonev/nharker/core/helpers/StubClock.kt @@ -7,6 +7,7 @@ import java.time.* */ class StubClock(val instant: Instant = Instant.ofEpochSecond(1)) : Clock() { + override fun withZone(p0: ZoneId?): Clock { return this } diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProviderTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProviderTest.kt index 40bc69e..5c0aed8 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProviderTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticleSynonymProviderTest.kt @@ -45,14 +45,14 @@ class NitriteArticleSynonymProviderTest { .first()["synonymMap"] as Map @Before - fun setUp(){ + fun setUp() { val mapDocument = Document.createDocument("globalId", globalMapId) mapDocument["synonymMap"] = synonymMap db.getCollection(mapCollectionName).insert(mapDocument) } @Test - fun `Getting synonym map creates it`(){ + fun `Getting synonym map creates it`() { db.getCollection(mapCollectionName).remove("globalId" eq globalMapId) val synonymMap = synonymMapProvider.getSynonymMap() @@ -61,14 +61,14 @@ class NitriteArticleSynonymProviderTest { } @Test - fun `Get synonym map`(){ + fun `Get synonym map`() { val synonymMap = synonymMapProvider.getSynonymMap() assertThat(synonymMap, Is(synonymMap)) } @Test - fun `Add synonym to map`(){ + fun `Add synonym to map`() { val synonym = synonymMapProvider.addSynonym("::synonym::", article) assertThat(presavedMap, Is(synonymMap.plus( @@ -77,12 +77,12 @@ class NitriteArticleSynonymProviderTest { } @Test(expected = SynonymAlreadyTakenException::class) - fun `Adding existing synonym throws exception`(){ + fun `Adding existing synonym throws exception`() { synonymMapProvider.addSynonym("::presavedSynonym::", article) } @Test - fun `Remove synonym from map`(){ + fun `Remove synonym from map`() { synonymMapProvider.removeSynonym("::presavedSynonym::") @@ -90,7 +90,7 @@ class NitriteArticleSynonymProviderTest { } @Test(expected = SynonymNotFoundException::class) - fun `Removing non-existent synonym throws exception`(){ + fun `Removing non-existent synonym throws exception`() { synonymMapProvider.removeSynonym("::non-existent-synonym::") } } \ No newline at end of file diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticlesTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticlesTest.kt index 843f8a0..1735b28 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticlesTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteArticlesTest.kt @@ -4,6 +4,7 @@ import com.tsbonev.nharker.core.* import com.tsbonev.nharker.core.helpers.StubClock import org.dizitart.kno2.filters.eq import org.dizitart.kno2.nitrite +import org.hamcrest.CoreMatchers.nullValue import org.jmock.AbstractExpectations.returnValue import org.jmock.Expectations import org.jmock.Mockery @@ -125,6 +126,20 @@ class NitriteArticlesTest { assertThat(articles.getById("::fake-article-value::").isPresent, Is(false)) } + @Test + fun `Delete and return article`() { + val deletedArticle = articles.delete(article.id) + + assertThat(deletedArticle, Is(article)) + assertThat(db.getRepository(collectionName, Article::class.java) + .find(Article::id eq article.id).firstOrNull(), Is(nullValue())) + } + + @Test(expected = ArticleNotFoundException::class) + fun `Deleting non-existing article throws exception`() { + articles.delete("::fake-article-id::") + } + @Test fun `Append entry to article`() { context.expecting { @@ -153,7 +168,7 @@ class NitriteArticlesTest { } @Test - fun `Linking increases count when already linked`(){ + fun `Linking increases count when already linked`() { context.expecting { oneOf(entryLinker).findArticleLinks(entry, listOf("article-title")) will(returnValue(setOf("article-title-2"))) @@ -197,7 +212,7 @@ class NitriteArticlesTest { } @Test - fun `Decrease number of links when more than one is present`(){ + fun `Decrease number of links when more than one is present`() { context.expecting { oneOf(entryLinker).findArticleLinks(firstPresavedEntry, listOf("article-title")) will(returnValue(setOf("article-title-1"))) @@ -286,14 +301,14 @@ class NitriteArticlesTest { } @Test - fun `Get article titles by links`(){ + fun `Get article titles by links`() { val articleTitles = articles.getArticleTitles(setOf("article-title")) assertThat(articleTitles, Is(listOf("Article title"))) } @Test - fun `Skip non-existing links when gathering full titles`(){ + fun `Skip non-existing links when gathering full titles`() { val articleTitles = articles.getArticleTitles(setOf( "not-found-link-1", "article-title", "not-found-link-2")) @@ -301,7 +316,7 @@ class NitriteArticlesTest { } @Test - fun `Return empty list when link titles don't point to existing articles`(){ + fun `Return empty list when link titles don't point to existing articles`() { val articleTitles = articles.getArticleTitles(setOf("non-existent-link-1", "non-existent-link-2")) diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCataloguesTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCataloguesTest.kt index 0a13629..55e365c 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCataloguesTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteCataloguesTest.kt @@ -91,7 +91,7 @@ class NitriteCataloguesTest { ) @Before - fun setUp(){ + fun setUp() { db.getRepository(collectionName, Catalogue::class.java).insert(catalogue) db.getRepository(collectionName, Catalogue::class.java).insert(subCatalogue) db.getRepository(collectionName, Catalogue::class.java).insert(firstPresavedSubcatalogue) @@ -99,7 +99,7 @@ class NitriteCataloguesTest { } @Test - fun `Create and return catalogue`(){ + fun `Create and return catalogue`() { db.getRepository(collectionName, Catalogue::class.java).remove(Catalogue::id eq catalogue.id) assertThat(catalogues.create(catalogueRequest).copy(id = catalogue.id), @@ -108,30 +108,30 @@ class NitriteCataloguesTest { } @Test(expected = CatalogueTitleTakenException::class) - fun `Creating a catalogue with a taken title throws exception`(){ + fun `Creating a catalogue with a taken title throws exception`() { catalogues.create(catalogueRequest) } @Test - fun `Save and return catalogue`(){ + fun `Save and return catalogue`() { db.getRepository(collectionName, Catalogue::class.java).remove(Catalogue::id eq catalogue.id) - + assertThat(catalogues.save(catalogue), Is(catalogue)) } @Test - fun `Retrieve catalogue by id`(){ + fun `Retrieve catalogue by id`() { assertThat(catalogues.getById(catalogue.id).isPresent, Is(true)) assertThat(catalogues.getById(catalogue.id).get(), Is(catalogue)) } @Test - fun `Retrieving non-existent catalogue returns empty`(){ + fun `Retrieving non-existent catalogue returns empty`() { assertThat(catalogues.getById("fake-catalogue-value::").isPresent, Is(false)) } @Test - fun `Change catalogue title`(){ + fun `Change catalogue title`() { val updatedCatalogue = catalogues.changeTitle(catalogue.id, "::new-title::") assertThat(updatedCatalogue, Is(catalogue.copy(title = updatedCatalogue.title))) @@ -139,23 +139,23 @@ class NitriteCataloguesTest { } @Test(expected = CatalogueTitleTakenException::class) - fun `Changing catalogue title to taken one throws exception`(){ + fun `Changing catalogue title to taken one throws exception`() { catalogues.changeTitle(catalogue.id, catalogue.title) } @Test(expected = CatalogueNotFoundException::class) - fun `Changing title of non-existent catalogue throws exception`(){ + fun `Changing title of non-existent catalogue throws exception`() { catalogues.changeTitle("::fake-value::", catalogue.title) } @Test - fun `Change parent of catalogue`(){ + fun `Change parent of catalogue`() { val updatedChild = catalogues.changeParentCatalogue(subCatalogue.id, catalogue) assertThat(updatedChild, Is(subCatalogue.copy( parentCatalogue = catalogue.id))) assertThat(presavedCatalogue, Is(catalogue.copy( - subCatalogues= catalogue.subCatalogues.append(subCatalogue.id)))) + subCatalogues = catalogue.subCatalogues.append(subCatalogue.id)))) } @Test(expected = CatalogueAlreadyAChildException::class) @@ -164,22 +164,22 @@ class NitriteCataloguesTest { } @Test(expected = CatalogueCircularInheritanceException::class) - fun `Changing parent of a parent to its child throws exception`(){ + fun `Changing parent of a parent to its child throws exception`() { catalogues.changeParentCatalogue(catalogue.id, firstPresavedSubcatalogue) } @Test(expected = CatalogueNotFoundException::class) - fun `Changing parent of non-existent catalogue throws exception`(){ + fun `Changing parent of non-existent catalogue throws exception`() { catalogues.changeParentCatalogue("::fake-parent-id::", subCatalogue) } @Test(expected = SelfContainedCatalogueException::class) - fun `Changing parent of catalogue to the same value throws exception`(){ + fun `Changing parent of catalogue to the same value throws exception`() { catalogues.changeParentCatalogue(catalogue.id, catalogue) } @Test - fun `Append catalogue to catalogue subcatalogues`(){ + fun `Append catalogue to catalogue subcatalogues`() { val appendedChild = catalogues.appendSubCatalogue(catalogue.id, subCatalogue) assertThat(appendedChild, Is(subCatalogue.copy(parentCatalogue = catalogue.id))) @@ -189,27 +189,27 @@ class NitriteCataloguesTest { } @Test(expected = CatalogueCircularInheritanceException::class) - fun `Appending parent to its own child throws exception`(){ + fun `Appending parent to its own child throws exception`() { catalogues.appendSubCatalogue(firstPresavedSubcatalogue.id, catalogue) } @Test(expected = CatalogueAlreadyAChildException::class) - fun `Appending catalogue that is already a subcatalogue throws exception`(){ + fun `Appending catalogue that is already a subcatalogue throws exception`() { catalogues.appendSubCatalogue(catalogue.id, firstPresavedSubcatalogue) } @Test(expected = SelfContainedCatalogueException::class) - fun `Appending catalogue to itself throws exception`(){ + fun `Appending catalogue to itself throws exception`() { catalogues.appendSubCatalogue(catalogue.id, catalogue) } @Test(expected = CatalogueNotFoundException::class) - fun `Appending subcatalogue to non-existent catalogue throws exception`(){ + fun `Appending subcatalogue to non-existent catalogue throws exception`() { catalogues.appendSubCatalogue("::fake-catalogue-id::", catalogue) } @Test - fun `Remove subcatalogue from catalogue`(){ + fun `Remove subcatalogue from catalogue`() { val removedCatalogue = catalogues.removeSubCatalogue(catalogue.id, secondPresavedSubcatalogue) assertThat(removedCatalogue, Is(secondPresavedSubcatalogue.copy(parentCatalogue = null))) @@ -217,25 +217,25 @@ class NitriteCataloguesTest { } @Test - fun `Reorder subcatalogues after deletion`(){ + fun `Reorder subcatalogues after deletion`() { catalogues.removeSubCatalogue(catalogue.id, firstPresavedSubcatalogue) assertThat(presavedCatalogue.subCatalogues, Is(mapOf(secondPresavedSubcatalogue.id to 0))) } @Test(expected = CatalogueNotAChildException::class) - fun `Removing subcatalogue from non-parent throws exception`(){ + fun `Removing subcatalogue from non-parent throws exception`() { catalogues.removeSubCatalogue("::fake-parent-catalogue-id::", catalogue) } @Test(expected = CatalogueNotFoundException::class) - fun `Removing subcatalogue from non-existent parent throws exception`(){ + fun `Removing subcatalogue from non-existent parent throws exception`() { db.getRepository(collectionName, Catalogue::class.java).remove(Catalogue::id eq catalogue.id) catalogues.removeSubCatalogue(catalogue.id, firstPresavedSubcatalogue) } @Test - fun `Append article to catalogue`(){ + fun `Append article to catalogue`() { val appendedChild = catalogues.appendArticle(catalogue.id, article) assertThat(appendedChild, Is(article)) @@ -245,17 +245,17 @@ class NitriteCataloguesTest { } @Test(expected = ArticleAlreadyInCatalogueException::class) - fun `Appending article that is already a in a catalogue throws exception`(){ + fun `Appending article that is already a in a catalogue throws exception`() { catalogues.appendArticle(catalogue.id, firstPresavedArticle) } @Test(expected = CatalogueNotFoundException::class) - fun `Appending article to non-existent catalogue throws exception`(){ + fun `Appending article to non-existent catalogue throws exception`() { catalogues.appendArticle("::fake-catalogue-value::", article) } @Test - fun `Remove article from catalogue`(){ + fun `Remove article from catalogue`() { val removedArticle = catalogues.removeArticle(catalogue.id, secondPresavedArticle) assertThat(removedArticle, Is(secondPresavedArticle)) @@ -263,25 +263,25 @@ class NitriteCataloguesTest { } @Test - fun `Reorder articles after deletion`(){ + fun `Reorder articles after deletion`() { catalogues.removeArticle(catalogue.id, firstPresavedArticle) assertThat(presavedCatalogue, Is(catalogue.copy(articles = mapOf(secondPresavedArticle.id to 0)))) } @Test(expected = ArticleNotInCatalogueException::class) - fun `Removing article from non-parent throws exception`(){ + fun `Removing article from non-parent throws exception`() { catalogues.removeArticle(catalogue.id, article) } @Test(expected = CatalogueNotFoundException::class) - fun `Removing article from non-existent catalogue throws exception`(){ + fun `Removing article from non-existent catalogue throws exception`() { db.getRepository(collectionName, Catalogue::class.java).remove(Catalogue::id eq catalogue.id) catalogues.removeArticle(catalogue.id, firstPresavedArticle) } @Test - fun `Switch articles in catalogue`(){ + fun `Switch articles in catalogue`() { val updatedCatalogue = catalogues.switchArticles(catalogue.id, firstPresavedArticle, secondPresavedArticle) @@ -292,18 +292,18 @@ class NitriteCataloguesTest { } @Test(expected = CatalogueNotFoundException::class) - fun `Switching articles in non-existent catalogue throws exception`(){ + fun `Switching articles in non-existent catalogue throws exception`() { catalogues.switchArticles("::fake-catalogue-value::", firstPresavedArticle, secondPresavedArticle) } @Test(expected = ArticleNotInCatalogueException::class) - fun `Switching articles in non-containing catalogue throws exception`(){ + fun `Switching articles in non-containing catalogue throws exception`() { catalogues.switchArticles(catalogue.id, article, secondPresavedArticle) } @Test - fun `Switch subcatalogues in catalogue`(){ + fun `Switch subcatalogues in catalogue`() { val updatedCatalogue = catalogues.switchSubCatalogues(catalogue.id, firstPresavedSubcatalogue, secondPresavedSubcatalogue) @@ -314,18 +314,18 @@ class NitriteCataloguesTest { } @Test(expected = CatalogueNotAChildException::class) - fun `Switching subcatalogues in non-containing catalogue throws exception`(){ + fun `Switching subcatalogues in non-containing catalogue throws exception`() { catalogues.switchSubCatalogues(catalogue.id, subCatalogue, secondPresavedSubcatalogue) } @Test(expected = CatalogueNotFoundException::class) - fun `Switching subcatalogues in non-existing catalogue throws exception`(){ + fun `Switching subcatalogues in non-existing catalogue throws exception`() { catalogues.switchSubCatalogues("::fake-catalogue-value::", firstPresavedSubcatalogue, secondPresavedSubcatalogue) } @Test - fun `Delete and return catalogue`(){ + fun `Delete and return catalogue`() { val savedCatalogue = presavedCatalogue val deletedCatalogue = catalogues.delete(savedCatalogue.id) @@ -338,7 +338,7 @@ class NitriteCataloguesTest { } @Test(expected = CatalogueNotFoundException::class) - fun `Deleting non-existent catalogue throws exception`(){ + fun `Deleting non-existent catalogue throws exception`() { catalogues.delete("::fake-catalogue-value::") } } diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollectorTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollectorTest.kt index 593bc04..7bc552a 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollectorTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntityTrashCollectorTest.kt @@ -52,24 +52,24 @@ class NitriteEntityTrashCollectorTest { private val trashCollector = NitriteEntityTrashCollector(db, entryTrashCollectionName) private val coll = db.getCollection(entryTrashCollectionName) - + @Before - fun setUp(){ + fun setUp() { val trashedDoc = trashedEntry.toDocument() trashedId = trashedDoc.get("entityId", String::class.java) coll.insert(trashedDoc) } - + @Test - fun `Trash entity`(){ + fun `Trash entity`() { val trashedID = trashCollector.trash(entry) assertThat(findEntity(trashedID) as Entry, Is(entry)) } @Test - fun `Trash entities of differing types`(){ + fun `Trash entities of differing types`() { val entryTrashedId = trashCollector.trash(entry) val articleTrashedId = trashCollector.trash(article) @@ -78,33 +78,33 @@ class NitriteEntityTrashCollectorTest { } @Test - fun `Restore entity from trash`(){ + fun `Restore entity from trash`() { val restoredEntry = trashCollector.restore(trashedId) as Entry assertThat(restoredEntry, Is(trashedEntry)) } @Test - fun `Restoring entity removes it from trash`(){ + fun `Restoring entity removes it from trash`() { trashCollector.restore(trashedId) assertThat(coll.find("entityId" eq trashedId).firstOrNull(), Is(nullValue())) } @Test(expected = EntityNotInTrashException::class) - fun `Restoring non-trashed entity throws exception`(){ + fun `Restoring non-trashed entity throws exception`() { trashCollector.restore(entry.id) } @Test - fun `View trashed entities`(){ + fun `View trashed entities`() { val trashed = trashCollector.view() - + assertThat(trashed, Is(listOf(trashedEntry))) } @Test - fun `Clear trashed`(){ + fun `Clear trashed`() { trashCollector.clear() assertThat(coll.find().toList(), Is(emptyList())) diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntriesTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntriesTest.kt index 211d6de..1240c64 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntriesTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/adapter/nitrite/NitriteEntriesTest.kt @@ -40,22 +40,22 @@ class NitriteEntriesTest { private val entries = NitriteEntries(db, collectionName, stubClock) @Before - fun setUp(){ + fun setUp() { db.getRepository(collectionName, Entry::class.java).insert(entry) } @Test - fun `Create and return entry`(){ + fun `Create and return entry`() { assertThat(entries.create(entryRequest).copy(id = "::entryId::"), Is(entry)) } @Test - fun `Save and return entry`(){ + fun `Save and return entry`() { assertThat(entries.save(entry), Is(entry)) } @Test - fun `Retrieve entry by id`(){ + fun `Retrieve entry by id`() { val retrievedEntry = entries.getById("::entryId::") assertThat(retrievedEntry.isPresent, Is(true)) @@ -63,14 +63,14 @@ class NitriteEntriesTest { } @Test - fun `Return empty when an entry is not found by id`(){ + fun `Return empty when an entry is not found by id`() { val retrievedEntry = entries.getById("::fake-entry-value::") assertThat(retrievedEntry.isPresent, Is(false)) } @Test - fun `Update entry content`(){ + fun `Update entry content`() { val content = "::new-content::" entries.updateContent(entry.id, content) @@ -81,12 +81,12 @@ class NitriteEntriesTest { } @Test(expected = EntryNotFoundException::class) - fun `Updating a non-existent entry's content throws exception`(){ + fun `Updating a non-existent entry's content throws exception`() { entries.updateContent("::fake-entry-value::", "::content::") } @Test - fun `Update entry links`(){ + fun `Update entry links`() { val links = mapOf("::new-link::" to "::new-article::") entries.updateLinks(entry.id, links) @@ -97,12 +97,12 @@ class NitriteEntriesTest { } @Test(expected = EntryNotFoundException::class) - fun `Updating a non-existent entry's links throws exception`(){ + fun `Updating a non-existent entry's links throws exception`() { entries.updateLinks("::fake-entry-value::", mapOf("::new-link::" to "::new-article::")) } @Test - fun `Query entries by content`(){ + fun `Query entries by content`() { val firstEntry = entries.create(entryRequest.copy(content = "apples")) val secondEntry = entries.create(entryRequest.copy(content = "apples and oranges")) val thirdEntry = entries.create(entryRequest.copy(content = "only oranges")) @@ -121,7 +121,7 @@ class NitriteEntriesTest { } @Test - fun `Delete and return entry`(){ + fun `Delete and return entry`() { val deletedEntry = entries.delete(entry.id) assertThat(deletedEntry, Is(entry)) @@ -129,7 +129,7 @@ class NitriteEntriesTest { } @Test(expected = EntryNotFoundException::class) - fun `Deleting non-existent entry throws exception`(){ + fun `Deleting non-existent entry throws exception`() { entries.delete("::fake-entry-value::") } } \ No newline at end of file diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/SimpleEntryLinkerTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/SimpleEntryLinkerTest.kt index ffdee09..d26e06e 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/SimpleEntryLinkerTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/SimpleEntryLinkerTest.kt @@ -76,7 +76,7 @@ class SimpleEntryLinkerTest { private val entryLinker = SimpleEntryLinker(synonymProvider) @Test - fun `Link entry content to articles`(){ + fun `Link entry content to articles`() { context.expecting { oneOf(synonymProvider).getSynonymMap() will(returnValue(synonymMap)) @@ -88,7 +88,7 @@ class SimpleEntryLinkerTest { } @Test - fun `Find no implicit links`(){ + fun `Find no implicit links`() { context.expecting { oneOf(synonymProvider).getSynonymMap() will(returnValue(emptyMap())) @@ -105,7 +105,7 @@ class SimpleEntryLinkerTest { assertThat(articleLinks, Is(emptySet())) } - private fun Mockery.expecting(block: Expectations.() -> Unit){ - checking(Expectations().apply(block)) + private fun Mockery.expecting(block: Expectations.() -> Unit) { + checking(Expectations().apply(block)) } } \ No newline at end of file diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/OrderedMapHelpersTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/OrderedMapHelpersTest.kt index fec621e..980961e 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/OrderedMapHelpersTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/OrderedMapHelpersTest.kt @@ -9,11 +9,11 @@ import org.junit.Assert.assertThat */ class OrderedMapHelpersTest { - private lateinit var holderMap : Map + private lateinit var holderMap: Map private val mutableMap = mutableMapOf() @Test - fun `Appending to map follows order`(){ + fun `Appending to map follows order`() { holderMap = mutableMap.append("One") holderMap = holderMap.append("Two") @@ -23,7 +23,7 @@ class OrderedMapHelpersTest { } @Test - fun `Subtracting from map reorders`(){ + fun `Subtracting from map reorders`() { holderMap = mutableMap.append("One") holderMap = holderMap.append("Two") holderMap = holderMap.append("Three") @@ -35,12 +35,12 @@ class OrderedMapHelpersTest { } @Test(expected = ElementNotInMapException::class) - fun `Subtracting non-existent value throws exception`(){ + fun `Subtracting non-existent value throws exception`() { mutableMap.subtract("::non-existent-value::") } @Test - fun `Switching list values reorders`(){ + fun `Switching list values reorders`() { holderMap = mutableMap.append("One") holderMap = holderMap.append("Two") holderMap = holderMap.append("Three") @@ -53,7 +53,7 @@ class OrderedMapHelpersTest { } @Test(expected = ElementNotInMapException::class) - fun `Switching non-existent value throws exception`(){ + fun `Switching non-existent value throws exception`() { mutableMap.switch("::non-existent-value::", "::non-existent-value::") } } \ No newline at end of file diff --git a/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/StubClockTest.kt b/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/StubClockTest.kt index d26afae..c441d3d 100644 --- a/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/StubClockTest.kt +++ b/nharker-core/src/test/kotlin/com/tsbonev/nharker/core/helpers/StubClockTest.kt @@ -14,22 +14,22 @@ class StubClockTest { private val stubClock = StubClock(instant) @Test - fun `Instant of clock is stubbed`(){ + fun `Instant of clock is stubbed`() { assertThat(stubClock.instant, Is(instant)) } @Test - fun `Zone of clock is UTC`(){ + fun `Zone of clock is UTC`() { assertThat(stubClock.zone, Is(ZoneId.of("Z"))) } @Test - fun `Zone of clock cannot change`(){ + fun `Zone of clock cannot change`() { assertThat(stubClock.withZone(ZoneId.of("GMT")).zone, Is(ZoneId.of("Z"))) } @Test - fun `Clock time is fixed`(){ + fun `Clock time is fixed`() { assertThat(LocalDateTime.ofInstant(instant, ZoneOffset.UTC), Is(LocalDateTime.now(stubClock))) }