-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to download backup using command line tool
- Loading branch information
Showing
4 changed files
with
176 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/groovy/org/modelcatalogue/discourse/util/DelegatingMainClass.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.modelcatalogue.discourse.util | ||
|
||
import org.modelcatalogue.discourse.api.Backups | ||
|
||
class DelegatingMainClass { | ||
|
||
private static Map<String, Class> delegates = [backup: Backups] | ||
|
||
static void main(String... args) { | ||
if (!args) { | ||
println """ | ||
Usage: | ||
discourse <command> [args] | ||
Available commands: ${delegates.keySet().sort().join(', ')} | ||
""".stripIndent() | ||
return | ||
} | ||
|
||
Class clazz = delegates[args[0]] | ||
|
||
if (!clazz) { | ||
println """ | ||
No such command '${args[0]}' | ||
Available commands: ${delegates.keySet().sort().join(', ')} | ||
""".stripIndent() | ||
return | ||
} | ||
|
||
if (args.size() == 1) { | ||
clazz.main([] as String[]) | ||
} else { | ||
clazz.main(args[1..-1] as String[]) | ||
} | ||
|
||
} | ||
|
||
} |
88 changes: 59 additions & 29 deletions
88
src/test/groovy/org/modelcatalogue/discourse/Sandbox.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,87 @@ | ||
package org.modelcatalogue.discourse | ||
|
||
import groovy.json.JsonOutput | ||
import org.junit.Rule | ||
import org.junit.rules.TemporaryFolder | ||
import org.modelcatalogue.discourse.api.Backups | ||
import org.modelcatalogue.discourse.util.DelegatingMainClass | ||
import spock.lang.Ignore | ||
import spock.lang.Specification | ||
|
||
@Ignore | ||
class Sandbox extends Specification { | ||
|
||
Discourse discourse = Discourse.create 'http://192.168.1.114', 'af9402ba45b8f4aff5a84bcdf6da85fc7548db746026c5095ed652d0f83fcd8b', 'discourse' | ||
@Rule TemporaryFolder temporaryFolder = new TemporaryFolder() | ||
|
||
@Ignore | ||
def "get some posts"() { | ||
def topic = discourse.posts.createPost(14, """ | ||
one two three four five six seven eight nine ten ${System.currentTimeMillis()} | ||
""".stripIndent().trim()) | ||
Discourse discourse = Discourse.create 'http://discourse.metadataregistry.org.uk/', '7b6d51fd026e18e3d96a868501f4890d4cff87a6de5f4cfb26778685de534622', 'admin' | ||
|
||
println JsonOutput.prettyPrint(JsonOutput.toJson(topic.data)) | ||
|
||
def "download latest backup"() { | ||
File testFolder = temporaryFolder.newFolder('backup-tests') | ||
|
||
File propFile = new File(testFolder, 'discourse.properties') | ||
Properties props = new Properties() | ||
props.setProperty('discourse.url', 'http://discourse.metadataregistry.org.uk/') | ||
props.setProperty('discourse.key', 'xxx') | ||
props.setProperty('discourse.user', 'admin') | ||
propFile.withWriter { | ||
props.store(it, 'Test properties file') | ||
} | ||
|
||
File bkpDir = new File(testFolder, 'backups') | ||
bkpDir.mkdirs() | ||
|
||
Backups.main(propFile.absolutePath, bkpDir.absolutePath) | ||
|
||
expect: | ||
topic instanceof Reader | ||
topic | ||
topic.data instanceof Map | ||
topic.status == 200 | ||
bkpDir.listFiles().size() == 1 | ||
bkpDir.listFiles()[0].name.endsWith('tar.gz') | ||
bkpDir.listFiles()[0].size() > 100 | ||
|
||
} | ||
|
||
def "test searching"() { | ||
def result = discourse.categories.getCategory('NHIC') | ||
def "download latest backup (using delegating class)"() { | ||
File testFolder = temporaryFolder.newFolder('backup-tests') | ||
println JsonOutput.prettyPrint(JsonOutput.toJson(result.data)) | ||
File propFile = new File(testFolder, 'discourse.properties') | ||
Properties props = new Properties() | ||
props.setProperty('discourse.url', 'http://discourse.metadataregistry.org.uk/') | ||
props.setProperty('discourse.key', 'xxx') | ||
props.setProperty('discourse.user', 'admin') | ||
propFile.withWriter { | ||
props.store(it, 'Test properties file') | ||
} | ||
File bkpDir = new File(testFolder, 'backups') | ||
bkpDir.mkdirs() | ||
DelegatingMainClass.main('backup', propFile.absolutePath, bkpDir.absolutePath) | ||
expect: | ||
result | ||
bkpDir.listFiles().size() == 1 | ||
bkpDir.listFiles()[0].name.endsWith('tar.gz') | ||
} | ||
@Ignore | ||
def "create topic with category"() { | ||
def result = discourse.topics.createTopic( | ||
"this is a cool title ${System.currentTimeMillis()}", | ||
"this is ultracool post about nothing because I don't know what to write for ${System.currentTimeMillis()}", | ||
"Catalogue Elements" | ||
) | ||
def "download backup"() { | ||
def result = discourse.backups.backups | ||
println JsonOutput.prettyPrint(JsonOutput.toJson(result.data)) | ||
expect: | ||
result | ||
} | ||
when: | ||
@Ignore | ||
def "verify user does not exist"() { | ||
def result = discourse.users.getUser('otto_von_bahnhof') | ||
File backupsDir = temporaryFolder.newFolder('discourse-backups') | ||
backupsDir.mkdirs() | ||
println JsonOutput.prettyPrint(JsonOutput.toJson(result.data)) | ||
for (backup in result.data) { | ||
def backupFile = discourse.backups.downloadBackup(backup.filename) | ||
new File(backupsDir, backup.filename) << backupFile.data | ||
} | ||
expect: | ||
result.status == 404 | ||
then: | ||
true | ||
} | ||
} |