diff --git a/.ci/doc/builder/Dockerfile b/.ci/doc/builder/Dockerfile new file mode 100644 index 00000000..51d14788 --- /dev/null +++ b/.ci/doc/builder/Dockerfile @@ -0,0 +1,8 @@ +FROM node:14.18.2 + +COPY . /var/app +WORKDIR /var/app + +RUN npm install + +CMD [ "node /var/app/builder.js" ] \ No newline at end of file diff --git a/.ci/doc/builder/builder.js b/.ci/doc/builder/builder.js new file mode 100644 index 00000000..e79b5246 --- /dev/null +++ b/.ci/doc/builder/builder.js @@ -0,0 +1,90 @@ +const yaml = require('js-yaml'); +const fs = require('fs'); +const Path = require('path'); +const glob = require('glob'); + +const javaSnippetPath = '/mnt/doc/**/snippets/*-java.test.yml'; +const kotlinSnippetPath = '/mnt/doc/**/snippets/*-kotlin.test.yml'; +const snippetTemplatesPath = '/mnt/snippet-templates/*'; +const buildDir = '/mnt/build'; + +/** + * Extract code from balises @start and @stop + * If one of the balise is missing, the whole file is returned + * + * @param {String} code + * @returns + */ +function extractCode(code) { + const lines = code.split('\n'); + const start = lines.findIndex(line => line.includes('// @start')); + const end = lines.findIndex(line => line.includes('// @end')); + + if (start === -1 || end === -1) { + return code; + } + + return lines.slice(start, end).join('\n'); +} + +/** + * Go over each file and build the snippet with the right template + * then build the method that encapsulate the snippet + * + * @param {String[]} paths + * @param {String} ext + * @param {*} templates + * @returns + */ +function buildSnippetMethods(paths, ext, templates) { + const methodTemplate = fs.readFileSync(`./templates/method.${ext}`, 'utf-8'); + + const methods = {}; + for (const path of paths) { + const testConfig = yaml.load(fs.readFileSync(path, 'utf8')); + + const template = templates[`${testConfig.template}.tpl.${ext}`]; + + const snippetName = testConfig.name.toLowerCase().replace(/ /g, '_').replace(/-/g, '_').replace(/#/g,''); + + const snippetCode = extractCode(fs.readFileSync(path.replace('.test.yml', `.${ext}`), 'utf8')); + + const snippet = template.replace('[snippet-code]', snippetCode); + methods[snippetName] = methodTemplate.replace('[snippet-name]', snippetName).replace('[snippet-code]', snippet); + } + + return methods; +} + +/** + * Build each snippet in a static method and bundle every method in a single file + * ready to be compiled + * + * @param {*} paths + * @param {*} ext + * @param {*} templates + */ +function bundleAndBuildTests(paths, ext, templates) { + const methods = buildSnippetMethods(paths, ext, templates); + + const mainTemplate = fs.readFileSync(`./templates/main.${ext}`, 'utf8'); + + const buildedSnippets = Object.keys(methods).map(key => methods[key]).join('\n'); + + + const program = mainTemplate.replace('[builded-snippets]', buildedSnippets); + fs.writeFileSync(`${buildDir}/SnippetTest.${ext}`, program); +} + +async function main() { + const templatesPath = glob.sync(snippetTemplatesPath); + const templates = {} + for (const path of templatesPath) { + templates[Path.basename(path)] = fs.readFileSync(path, 'utf8'); + } + + bundleAndBuildTests(glob.sync(javaSnippetPath), 'java', templates); + bundleAndBuildTests(glob.sync(kotlinSnippetPath), 'kt', templates); +} + +main(); \ No newline at end of file diff --git a/.ci/doc/builder/package-lock.json b/.ci/doc/builder/package-lock.json new file mode 100644 index 00000000..6e369b1b --- /dev/null +++ b/.ci/doc/builder/package-lock.json @@ -0,0 +1,98 @@ +{ + "name": "builder", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + }, + "balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "glob": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "requires": { + "argparse": "^2.0.1" + } + }, + "minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + } + } +} diff --git a/.ci/doc/builder/package.json b/.ci/doc/builder/package.json new file mode 100644 index 00000000..0d8127e0 --- /dev/null +++ b/.ci/doc/builder/package.json @@ -0,0 +1,15 @@ +{ + "name": "builder", + "version": "1.0.0", + "description": "", + "main": "builder.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "glob": "^7.2.0", + "js-yaml": "^4.1.0" + } +} diff --git a/.ci/doc/builder/templates/main.java b/.ci/doc/builder/templates/main.java new file mode 100644 index 00000000..69a4e9cc --- /dev/null +++ b/.ci/doc/builder/templates/main.java @@ -0,0 +1,39 @@ +import io.kuzzle.sdk.Kuzzle; +import io.kuzzle.sdk.protocol.*; +import io.kuzzle.sdk.coreClasses.responses.Response; +import io.kuzzle.sdk.coreClasses.SearchResult; +import io.kuzzle.sdk.coreClasses.lang.Lang; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + +public class SnippetTest { + + [builded-snippets] + + public static void main(String[] args) { + if (args.length < 1) { + System.err.println("Missing snippet name to execute"); + System.exit(1); + } + + // Format the snippet name into the method name + // Convert "java-searchresultscroll_3e8f25b" into "java_searchresultscroll" + String methodName = args[0].toLowerCase().replace("-","_").substring(0, args[0].length() - 8); + + try { + // Find the static method that has the given name using reflection + Method method = SnippetTest.class.getMethod(methodName); + // Execute the static method + method.invoke(null); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/.ci/doc/builder/templates/main.kt b/.ci/doc/builder/templates/main.kt new file mode 100644 index 00000000..72670b30 --- /dev/null +++ b/.ci/doc/builder/templates/main.kt @@ -0,0 +1,30 @@ +import io.kuzzle.sdk.Kuzzle +import io.kuzzle.sdk.protocol.* +import io.kuzzle.sdk.coreClasses.responses.Response +import io.kuzzle.sdk.coreClasses.SearchResult +import io.kuzzle.sdk.coreClasses.lang.Lang +import io.kuzzle.sdk.coreClasses.maps.KuzzleMap +import java.lang.reflect.Method +import java.util.*; +import kotlin.system.exitProcess + +public class Snippets { + [builded-snippets] +} + +fun main(args: Array) { + if (args.isEmpty()) { + println("Missing snippet name to execute") + exitProcess(1) + } + + // Format the snippet name into the method name + // Convert "kotlin-searchresultscroll_3e8f25b" into "kotlin_searchresultscroll" + val methodName: String = args[0].lowercase().replace('-', '_').substring(0, args[0].length - 8) + + var snippets: Snippets = Snippets() + // Find the method that has the given name using reflection + var method: Method = snippets.javaClass.getMethod(methodName) + // execute method + method.invoke(snippets) +} \ No newline at end of file diff --git a/.ci/doc/builder/templates/method.java b/.ci/doc/builder/templates/method.java new file mode 100644 index 00000000..d38ac97d --- /dev/null +++ b/.ci/doc/builder/templates/method.java @@ -0,0 +1,4 @@ +public static void [snippet-name]() { + Kuzzle kuzzle = null; + [snippet-code] +} \ No newline at end of file diff --git a/.ci/doc/builder/templates/method.kt b/.ci/doc/builder/templates/method.kt new file mode 100644 index 00000000..514120c8 --- /dev/null +++ b/.ci/doc/builder/templates/method.kt @@ -0,0 +1,3 @@ +fun [snippet-name]() { + [snippet-code] +} \ No newline at end of file diff --git a/.ci/doc/config.yml b/.ci/doc/config.yml index f2e6b217..c24feb6a 100644 --- a/.ci/doc/config.yml +++ b/.ci/doc/config.yml @@ -11,10 +11,10 @@ runners: default: java java: + debugStdout: false + debugStderr: false service: doc-runner-java path: /var/snippets/java - build: - cmd: cd /mnt && ./gradlew jar + afterInit: /mnt/.ci/doc/snippet-build.sh run: - before: timeout 600 bash -c 'until stat /tmp/runner_is_ready && curl -f -s -o /dev/null http://kuzzle:7512/_now; do sleep 1; done' - cmd: SNIPPET_PROTOCOL="{{ snippet.protocol }}" /mnt/.ci/doc/test-snippet.sh {{ snippet.source }} + cmd: SNIPPET_PROTOCOL="{{ snippet.protocol }}" /mnt/.ci/doc/test-snippet.sh "{{ snippet.source }}" "{{ snippet.name }}" diff --git a/.ci/doc/docker-compose.yml b/.ci/doc/docker-compose.yml index c99cabfb..4d47b8a5 100644 --- a/.ci/doc/docker-compose.yml +++ b/.ci/doc/docker-compose.yml @@ -25,8 +25,21 @@ services: ulimits: nofile: 65536 + snippet-builder: + build: ./builder + command: > + sh -c ' + rm /mnt/build/SnippetTest.java; + rm /mnt/build/SnippetTest.kt; + node /var/app/builder.js + ' + volumes: + - ../..:/mnt + - build:/mnt/build + - ./templates:/mnt/snippet-templates + doc-tests: - image: kuzzleio/snippets-tests + image: kuzzleio/snippets-tests:latest privileged: true ports: - '9229:9229' @@ -48,11 +61,15 @@ services: touch /tmp/runner_is_ready; tail -f /dev/null ' + depends_on: + - snippet-builder links: - kuzzle volumes: - ../..:/mnt + - build:/mnt/build - snippets:/var/snippets volumes: snippets: + build: diff --git a/.ci/doc/snippet-build.sh b/.ci/doc/snippet-build.sh new file mode 100755 index 00000000..13bdafcf --- /dev/null +++ b/.ci/doc/snippet-build.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -e + +echo "Wait for Snippets to be built and bundled" +timeout 600 bash -c 'until stat /mnt/build/SnippetTest.java > /dev/null && stat /mnt/build/SnippetTest.kt > /dev/null; do sleep 1; done' +echo "Building SDK" +cd /mnt && ./gradlew jar +echo "Copying SDK" +cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/java-project/libs/sdk-jvm-without-dependencies.jar +cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/kotlin-project/libs/sdk-jvm-without-dependencies.jar +echo "Copying Java Snippets" +cp /mnt/build/SnippetTest.java /mnt/.ci/doc/java-project/src/main/java/SnippetTest.java +echo "Copying Kotlin Snippets" +cp /mnt/build/SnippetTest.kt /mnt/.ci/doc/kotlin-project/src/main/java/SnippetTest.kt +echo "Building Java executable" +cd /mnt/.ci/doc/java-project/ && ./gradlew build +echo "Building Kotlin executable" +cd /mnt/.ci/doc/kotlin-project/ && ./gradlew build +echo "Wait for Kuzzle to be ready" +timeout 600 bash -c 'until stat /tmp/runner_is_ready && curl -f -s -o /dev/null http://kuzzle:7512/_now; do sleep 1; done' \ No newline at end of file diff --git a/.ci/doc/templates/catch.tpl.java b/.ci/doc/templates/catch.tpl.java index 756bdc46..4e7dd5ad 100644 --- a/.ci/doc/templates/catch.tpl.java +++ b/.ci/doc/templates/catch.tpl.java @@ -1,30 +1,18 @@ -import io.kuzzle.sdk.Kuzzle; -import java.util.ArrayList; -import io.kuzzle.sdk.protocol.*; -import io.kuzzle.sdk.coreClasses.responses.Response; -import java.util.*; - -public class SnippetTest { - private static Kuzzle kuzzle; - - public static void main(String[] argv) { - try { - AbstractProtocol protocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { - protocol = new Http("kuzzle"); - } else { - protocol = new WebSocket("kuzzle"); - } - kuzzle = new Kuzzle(protocol); - kuzzle.connect(); - [snippet-code] - System.out.println("Error"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (kuzzle != null) { - kuzzle.disconnect(); - } - } +try { + AbstractProtocol protocol; + if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { + protocol = new Http("kuzzle"); + } else { + protocol = new WebSocket("kuzzle"); + } + kuzzle = new Kuzzle(protocol); + kuzzle.connect(); + [snippet-code] + System.out.println("Error"); +} catch (Exception e) { + e.printStackTrace(); +} finally { + if (kuzzle != null) { + kuzzle.disconnect(); } } \ No newline at end of file diff --git a/.ci/doc/templates/catch.tpl.kt b/.ci/doc/templates/catch.tpl.kt index dc00b915..1dc9bb97 100644 --- a/.ci/doc/templates/catch.tpl.kt +++ b/.ci/doc/templates/catch.tpl.kt @@ -1,25 +1,17 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.* - -import java.util.concurrent.ExecutionException -import io.kuzzle.sdk.coreClasses.responses.Response - -fun main() { - val protocol: AbstractProtocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { - protocol = Http("kuzzle") - } else { - protocol = WebSocket("kuzzle") - } - val kuzzle = Kuzzle(protocol).apply { - connect() - } - try { - [snippet-code] - println("Error") - } catch (e: Exception) { - e.printStackTrace() - } finally { - kuzzle.disconnect() - } +val protocol: AbstractProtocol; +if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { + protocol = Http("kuzzle") +} else { + protocol = WebSocket("kuzzle") +} +val kuzzle = Kuzzle(protocol).apply { + connect() +} +try { + [snippet-code] + println("Error") +} catch (e: Exception) { + e.printStackTrace() +} finally { + kuzzle.disconnect() } diff --git a/.ci/doc/templates/default.tpl.java b/.ci/doc/templates/default.tpl.java index 47405418..e86109ba 100644 --- a/.ci/doc/templates/default.tpl.java +++ b/.ci/doc/templates/default.tpl.java @@ -1,31 +1,18 @@ -import io.kuzzle.sdk.Kuzzle; -import io.kuzzle.sdk.protocol.*; -import io.kuzzle.sdk.coreClasses.responses.Response; -import io.kuzzle.sdk.coreClasses.SearchResult; -import io.kuzzle.sdk.coreClasses.lang.Lang; -import java.util.*; - -public class SnippetTest { - private static Kuzzle kuzzle; - - public static void main(String[] argv) { - try { - AbstractProtocol protocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { - protocol = new Http("kuzzle"); - } else { - protocol = new WebSocket("kuzzle"); - } - kuzzle = new Kuzzle(protocol); - kuzzle.connect(); - [snippet-code] - System.out.println("Success"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (kuzzle != null) { - kuzzle.disconnect(); - } - } +try { + AbstractProtocol protocol; + if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { + protocol = new Http("kuzzle"); + } else { + protocol = new WebSocket("kuzzle"); + } + kuzzle = new Kuzzle(protocol); + kuzzle.connect(); + [snippet-code] + System.out.println("Success"); +} catch (Exception e) { + e.printStackTrace(); +} finally { + if (kuzzle != null) { + kuzzle.disconnect(); } } \ No newline at end of file diff --git a/.ci/doc/templates/default.tpl.kt b/.ci/doc/templates/default.tpl.kt index 0f1f33f1..22d2545c 100644 --- a/.ci/doc/templates/default.tpl.kt +++ b/.ci/doc/templates/default.tpl.kt @@ -1,28 +1,19 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.* -import java.util.concurrent.ExecutionException -import io.kuzzle.sdk.coreClasses.responses.Response -import io.kuzzle.sdk.coreClasses.SearchResult -import io.kuzzle.sdk.coreClasses.lang.Lang -import java.util.*; - -fun main() { - val protocol: AbstractProtocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { - protocol = Http("kuzzle") - } else { - protocol = WebSocket("kuzzle") - } - val kuzzle = Kuzzle(protocol).apply { - connect() - } - try { - [snippet-code] - println("Success") - } catch (e: Exception) { - e.printStackTrace() - } finally { - kuzzle.disconnect() - } +val protocol: AbstractProtocol; +if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { + protocol = Http("kuzzle") +} else { + protocol = WebSocket("kuzzle") +} +val kuzzle = Kuzzle(protocol).apply { + connect() } +try { + [snippet-code] + println("Success") +} catch (e: Exception) { + e.printStackTrace() +} finally { + kuzzle.disconnect() +} + diff --git a/.ci/doc/templates/print-result-array.tpl.java b/.ci/doc/templates/print-result-array.tpl.java index c9fcf7d8..0d1d69b4 100644 --- a/.ci/doc/templates/print-result-array.tpl.java +++ b/.ci/doc/templates/print-result-array.tpl.java @@ -1,32 +1,20 @@ -import io.kuzzle.sdk.Kuzzle; -import io.kuzzle.sdk.protocol.*; -import io.kuzzle.sdk.coreClasses.responses.Response; -import io.kuzzle.sdk.coreClasses.lang.Lang; -import java.util.*; - -public class SnippetTest { - private static Kuzzle kuzzle; - - public static void main(String[] argv) { - try { - AbstractProtocol protocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { - protocol = new Http("kuzzle"); - } else { - protocol = new WebSocket("kuzzle"); - } - kuzzle = new Kuzzle(protocol); - kuzzle.connect(); - [snippet-code] - for (Object o : result.get("successes")) { - System.out.println(o); - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (kuzzle != null) { - kuzzle.disconnect(); - } - } +try { + AbstractProtocol protocol; + if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { + protocol = new Http("kuzzle"); + } else { + protocol = new WebSocket("kuzzle"); + } + kuzzle = new Kuzzle(protocol); + kuzzle.connect(); + [snippet-code] + for (Object o : result.get("successes")) { + System.out.println(o); + } +} catch (Exception e) { + e.printStackTrace(); +} finally { + if (kuzzle != null) { + kuzzle.disconnect(); } } \ No newline at end of file diff --git a/.ci/doc/templates/print-result-array.tpl.kt b/.ci/doc/templates/print-result-array.tpl.kt index adb739eb..e315dc71 100644 --- a/.ci/doc/templates/print-result-array.tpl.kt +++ b/.ci/doc/templates/print-result-array.tpl.kt @@ -1,32 +1,22 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.* - -import java.util.concurrent.ExecutionException -import io.kuzzle.sdk.coreClasses.responses.Response -import io.kuzzle.sdk.coreClasses.maps.KuzzleMap -import io.kuzzle.sdk.coreClasses.lang.Lang - -fun main() { - val protocol: AbstractProtocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { - protocol = Http("kuzzle") - } else { - protocol = WebSocket("kuzzle") - } - val kuzzle = Kuzzle(protocol).apply { - connect() - } - try { - [snippet-code] - result["successes"]?.map { - for (item: Any? in it as KuzzleMap) { - println(item.toString()) - } +val protocol: AbstractProtocol; +if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { + protocol = Http("kuzzle") +} else { + protocol = WebSocket("kuzzle") +} +val kuzzle = Kuzzle(protocol).apply { + connect() +} +try { + [snippet-code] + result["successes"]?.map { + for (item: Any? in it as KuzzleMap) { + println(item.toString()) } - println(result.toString()) - } catch (e: Exception) { - e.printStackTrace() - } finally { - kuzzle.disconnect() } -} + println(result.toString()) +} catch (e: Exception) { + e.printStackTrace() +} finally { + kuzzle.disconnect() +} \ No newline at end of file diff --git a/.ci/doc/templates/print-result-arraylist.tpl.java b/.ci/doc/templates/print-result-arraylist.tpl.java index 07dc3497..440fb135 100644 --- a/.ci/doc/templates/print-result-arraylist.tpl.java +++ b/.ci/doc/templates/print-result-arraylist.tpl.java @@ -1,32 +1,20 @@ -import io.kuzzle.sdk.Kuzzle; -import io.kuzzle.sdk.protocol.*; -import io.kuzzle.sdk.coreClasses.responses.Response; -import io.kuzzle.sdk.coreClasses.lang.Lang; -import java.util.*; - -public class SnippetTest { - private static Kuzzle kuzzle; - - public static void main(String[] argv) { - try { - AbstractProtocol protocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { - protocol = new Http("kuzzle"); - } else { - protocol = new WebSocket("kuzzle"); - } - kuzzle = new Kuzzle(protocol); - kuzzle.connect(); - [snippet-code] - for (Object o : result) { - System.out.println(o); - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (kuzzle != null) { - kuzzle.disconnect(); - } - } +try { + AbstractProtocol protocol; + if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { + protocol = new Http("kuzzle"); + } else { + protocol = new WebSocket("kuzzle"); } -} \ No newline at end of file + kuzzle = new Kuzzle(protocol); + kuzzle.connect(); + [snippet-code] + for (Object o : result) { + System.out.println(o); + } +} catch (Exception e) { + e.printStackTrace(); +} finally { + if (kuzzle != null) { + kuzzle.disconnect(); + } +} diff --git a/.ci/doc/templates/print-result-arraylist.tpl.kt b/.ci/doc/templates/print-result-arraylist.tpl.kt index 8c78301c..658dc562 100644 --- a/.ci/doc/templates/print-result-arraylist.tpl.kt +++ b/.ci/doc/templates/print-result-arraylist.tpl.kt @@ -1,29 +1,20 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.* - -import java.util.concurrent.ExecutionException -import io.kuzzle.sdk.coreClasses.responses.Response -import io.kuzzle.sdk.coreClasses.lang.Lang - -fun main() { - val protocol: AbstractProtocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { - protocol = Http("kuzzle") - } else { - protocol = WebSocket("kuzzle") - } - val kuzzle = Kuzzle(protocol).apply { - connect() - } - try { - [snippet-code] - for (item: Any? in result) { - println(item.toString()) - } - println(result.toString()) - } catch (e: Exception) { - e.printStackTrace() - } finally { - kuzzle.disconnect() - } +val protocol: AbstractProtocol; +if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { + protocol = Http("kuzzle") +} else { + protocol = WebSocket("kuzzle") } +val kuzzle = Kuzzle(protocol).apply { + connect() +} +try { + [snippet-code] + for (item: Any? in result) { + println(item.toString()) + } + println(result.toString()) +} catch (e: Exception) { + e.printStackTrace() +} finally { + kuzzle.disconnect() +} \ No newline at end of file diff --git a/.ci/doc/templates/print-result.tpl.java b/.ci/doc/templates/print-result.tpl.java index 8bb01b19..ec4889e5 100644 --- a/.ci/doc/templates/print-result.tpl.java +++ b/.ci/doc/templates/print-result.tpl.java @@ -1,30 +1,18 @@ -import io.kuzzle.sdk.Kuzzle; -import io.kuzzle.sdk.protocol.*; -import io.kuzzle.sdk.coreClasses.responses.Response; -import io.kuzzle.sdk.coreClasses.lang.Lang; -import java.util.*; - -public class SnippetTest { - private static Kuzzle kuzzle; - - public static void main(String[] argv) { - try { - AbstractProtocol protocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { - protocol = new Http("kuzzle"); - } else { - protocol = new WebSocket("kuzzle"); - } - kuzzle = new Kuzzle(protocol); - kuzzle.connect(); - [snippet-code] - System.out.println(result.toString()); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (kuzzle != null) { - kuzzle.disconnect(); - } - } +try { + AbstractProtocol protocol; + if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { + protocol = new Http("kuzzle"); + } else { + protocol = new WebSocket("kuzzle"); } -} \ No newline at end of file + kuzzle = new Kuzzle(protocol); + kuzzle.connect(); + [snippet-code] + System.out.println(result.toString()); +} catch (Exception e) { + e.printStackTrace(); +} finally { + if (kuzzle != null) { + kuzzle.disconnect(); + } +} diff --git a/.ci/doc/templates/print-result.tpl.kt b/.ci/doc/templates/print-result.tpl.kt index 185d698e..e69b92fb 100644 --- a/.ci/doc/templates/print-result.tpl.kt +++ b/.ci/doc/templates/print-result.tpl.kt @@ -1,27 +1,17 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.* -import java.util.Date - -import java.util.concurrent.ExecutionException -import io.kuzzle.sdk.coreClasses.lang.Lang -import io.kuzzle.sdk.coreClasses.responses.Response - -fun main() { - val protocol: AbstractProtocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { - protocol = Http("kuzzle") - } else { - protocol = WebSocket("kuzzle") - } - val kuzzle = Kuzzle(protocol).apply { - connect() - } - try { - [snippet-code] - println(result.toString()) - } catch (e: Exception) { - e.printStackTrace() - } finally { - kuzzle.disconnect() - } +val protocol: AbstractProtocol; +if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { + protocol = Http("kuzzle") +} else { + protocol = WebSocket("kuzzle") } +val kuzzle = Kuzzle(protocol).apply { + connect() +} +try { + [snippet-code] + println(result.toString()) +} catch (e: Exception) { + e.printStackTrace() +} finally { + kuzzle.disconnect() +} \ No newline at end of file diff --git a/.ci/doc/templates/print-search-result.tpl.java b/.ci/doc/templates/print-search-result.tpl.java index d15c814a..e51853c7 100644 --- a/.ci/doc/templates/print-search-result.tpl.java +++ b/.ci/doc/templates/print-search-result.tpl.java @@ -1,31 +1,18 @@ -import io.kuzzle.sdk.Kuzzle; -import java.util.*; -import io.kuzzle.sdk.protocol.*; -import io.kuzzle.sdk.coreClasses.responses.Response; -import io.kuzzle.sdk.coreClasses.SearchResult; -import io.kuzzle.sdk.coreClasses.lang.Lang; - -public class SnippetTest { - private static Kuzzle kuzzle; - - public static void main(String[] argv) { - try { - AbstractProtocol protocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { - protocol = new Http("kuzzle"); - } else { - protocol = new WebSocket("kuzzle"); - } - kuzzle = new Kuzzle(protocol); - kuzzle.connect(); - [snippet-code] - System.out.println("Successfully retrieved " + matched.size() + " documents"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (kuzzle != null) { - kuzzle.disconnect(); - } - } +try { + AbstractProtocol protocol; + if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { + protocol = new Http("kuzzle"); + } else { + protocol = new WebSocket("kuzzle"); + } + kuzzle = new Kuzzle(protocol); + kuzzle.connect(); + [snippet-code] + System.out.println("Successfully retrieved " + matched.size() + " documents"); +} catch (Exception e) { + e.printStackTrace(); +} finally { + if (kuzzle != null) { + kuzzle.disconnect(); } } \ No newline at end of file diff --git a/.ci/doc/templates/print-search-result.tpl.kt b/.ci/doc/templates/print-search-result.tpl.kt index 8168b781..726ec6e5 100644 --- a/.ci/doc/templates/print-search-result.tpl.kt +++ b/.ci/doc/templates/print-search-result.tpl.kt @@ -1,26 +1,16 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.* - -import java.util.concurrent.ExecutionException -import io.kuzzle.sdk.coreClasses.responses.Response -import io.kuzzle.sdk.coreClasses.SearchResult -import io.kuzzle.sdk.coreClasses.lang.Lang - -fun main() { - val protocol: AbstractProtocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { - protocol = Http("kuzzle") - } else { - protocol = WebSocket("kuzzle") - } - val kuzzle = Kuzzle(protocol).apply { - connect() - } - try { - [snippet-code] - println("Successfully retrieved " + matched.size + " documents"); } catch (e: Exception) { - e.printStackTrace() - } finally { - kuzzle.disconnect() - } +val protocol: AbstractProtocol; +if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { + protocol = Http("kuzzle") +} else { + protocol = WebSocket("kuzzle") } +val kuzzle = Kuzzle(protocol).apply { + connect() +} +try { + [snippet-code] + println("Successfully retrieved " + matched.size + " documents"); } catch (e: Exception) { + e.printStackTrace() +} finally { + kuzzle.disconnect() +} \ No newline at end of file diff --git a/.ci/doc/templates/without-connect.tpl.java b/.ci/doc/templates/without-connect.tpl.java index d299edd3..8b9cc132 100644 --- a/.ci/doc/templates/without-connect.tpl.java +++ b/.ci/doc/templates/without-connect.tpl.java @@ -1,27 +1,17 @@ -import io.kuzzle.sdk.Kuzzle; -import io.kuzzle.sdk.protocol.*; -import java.util.*; - -public class SnippetTest { - private static Kuzzle kuzzle; - - public static void main(String[] argv) { - try { - AbstractProtocol protocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { - protocol = new Http("kuzzle"); - } else { - protocol = new WebSocket("kuzzle"); - } - kuzzle = new Kuzzle(protocol); - [snippet-code] - System.out.println("Success"); - } catch (Exception e) { - System.err.println(e.getMessage()); - } finally { - if (kuzzle != null) { - kuzzle.disconnect(); - } - } +try { + AbstractProtocol protocol; + if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL").equals("http")) { + protocol = new Http("kuzzle"); + } else { + protocol = new WebSocket("kuzzle"); + } + kuzzle = new Kuzzle(protocol); + [snippet-code] + System.out.println("Success"); +} catch (Exception e) { + System.err.println(e.getMessage()); +} finally { + if (kuzzle != null) { + kuzzle.disconnect(); } } diff --git a/.ci/doc/templates/without-connect.tpl.kt b/.ci/doc/templates/without-connect.tpl.kt index ad5f5515..ee9eba3c 100644 --- a/.ci/doc/templates/without-connect.tpl.kt +++ b/.ci/doc/templates/without-connect.tpl.kt @@ -1,24 +1,17 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.* - -import java.util.concurrent.ExecutionException - -fun main() { - val protocol: AbstractProtocol; - if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { - protocol = Http("kuzzle") - } else { - protocol = WebSocket("kuzzle") - } - val kuzzle = Kuzzle(protocol).apply { - connect() - } - try { - [snippet-code] - println("Success") - } catch (e: Exception) { - e.printStackTrace() - } finally { - kuzzle.disconnect() - } +val protocol: AbstractProtocol; +if (System.getenv("SNIPPET_PROTOCOL") != null && System.getenv("SNIPPET_PROTOCOL") == "http") { + protocol = Http("kuzzle") +} else { + protocol = WebSocket("kuzzle") +} +val kuzzle = Kuzzle(protocol).apply { + connect() +} +try { + [snippet-code] + println("Success") +} catch (e: Exception) { + e.printStackTrace() +} finally { + kuzzle.disconnect() } \ No newline at end of file diff --git a/.ci/doc/templates/without-ctor.tpl.java b/.ci/doc/templates/without-ctor.tpl.java index 8ae03372..a3b31bce 100644 --- a/.ci/doc/templates/without-ctor.tpl.java +++ b/.ci/doc/templates/without-ctor.tpl.java @@ -1,14 +1,6 @@ -import io.kuzzle.sdk.Kuzzle; -import io.kuzzle.sdk.protocol.WebSocket; -import java.util.*; - -public class SnippetTest { - public static void main(String[] argv) { - try { - [snippet-code] - System.out.println("Success"); - } catch (Exception e) { - System.err.println((e.getMessage())); - } - } +try { + [snippet-code] + System.out.println("Success"); +} catch (Exception e) { + System.err.println((e.getMessage())); } \ No newline at end of file diff --git a/.ci/doc/templates/without-ctor.tpl.kt b/.ci/doc/templates/without-ctor.tpl.kt index 5f546733..0f3fff27 100644 --- a/.ci/doc/templates/without-ctor.tpl.kt +++ b/.ci/doc/templates/without-ctor.tpl.kt @@ -1,13 +1,6 @@ -import io.kuzzle.sdk.Kuzzle -import io.kuzzle.sdk.protocol.WebSocket - -import java.util.concurrent.ExecutionException - -fun main() { - try { - [snippet-code] - println("Success") - } catch (e: Exception) { - e.printStackTrace() - } -} +try { + [snippet-code] + println("Success") +} catch (e: Exception) { + e.printStackTrace() +} \ No newline at end of file diff --git a/.ci/doc/test-snippet.sh b/.ci/doc/test-snippet.sh index e1cb1353..65a651e8 100755 --- a/.ci/doc/test-snippet.sh +++ b/.ci/doc/test-snippet.sh @@ -4,7 +4,7 @@ set -e if [ ${1: -3} == ".kt" ] then - cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/kotlin-project/libs/sdk-jvm-without-dependencies.jar && cp $1 /mnt/.ci/doc/kotlin-project/src/main/java/SnippetTest.kt && cd /mnt/.ci/doc/kotlin-project/ && ./gradlew build && java -classpath 'libs/sdk-jvm-without-dependencies.jar:' -jar build/libs/project-1.jar + cd /mnt/.ci/doc/kotlin-project/ && java -classpath 'libs/sdk-jvm-without-dependencies.jar:' -jar build/libs/project-1.jar $2 else - cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/java-project/libs/sdk-jvm-without-dependencies.jar && cp $1 /mnt/.ci/doc/java-project/src/main/java/SnippetTest.java && cd /mnt/.ci/doc/java-project/ && ./gradlew build && java -classpath 'libs/sdk-jvm-without-dependencies.jar:' -jar build/libs/project-1.jar + cd /mnt/.ci/doc/java-project/ && java -classpath 'libs/sdk-jvm-without-dependencies.jar:' -jar build/libs/project-1.jar $2 fi diff --git a/.github/actions/snippets-tests/action.yml b/.github/actions/snippets-tests/action.yml index 95ce4828..415b3a77 100644 --- a/.github/actions/snippets-tests/action.yml +++ b/.github/actions/snippets-tests/action.yml @@ -8,7 +8,7 @@ runs: java-version: "11" architecture: x64 - name: Build - run: ./gradlew build fatJar + run: ./gradlew build shadowJar shell: bash - name: Launch Tests run: docker-compose -f .ci/doc/docker-compose.yml run doc-tests index diff --git a/build.gradle.kts b/build.gradle.kts index adb30932..73ccc07d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,9 +1,5 @@ import org.gradle.jvm.tasks.Jar -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile -import java.util.Date -import org.gradle.api.publish.maven.MavenPom -import org.jetbrains.kotlin.gradle.dsl.Coroutines -import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar plugins { application @@ -12,6 +8,8 @@ plugins { signing jacoco kotlin("jvm") version "1.6.10" + id("com.github.johnrengelman.shadow") version "7.1.2" + id("org.jetbrains.dokka") version "1.7.10" } val artifactName = "sdk-jvm" @@ -35,7 +33,7 @@ val pomDeveloperId = "kuzzleio" val pomDeveloperName = "kuzzle" group = "io.kuzzle.sdk" -version = "1.3.2" +version = "1.3.3" val ktorVersion = "1.6.8" repositories { @@ -60,50 +58,50 @@ dependencies { implementation("com.google.code.gson:gson:2.9.0") testImplementation("org.jetbrains.kotlin:kotlin-test-junit") - testImplementation("io.mockk:mockk:1.8.13") - testImplementation("io.ktor:ktor-client-mock:1.3.2") + testImplementation("io.mockk:mockk:1.12.4") + testImplementation("io.ktor:ktor-client-mock:$ktorVersion") testImplementation("io.ktor:ktor-client-mock-jvm:$ktorVersion") testImplementation("io.ktor:ktor-client-json-jvm:$ktorVersion") - testImplementation("io.ktor:ktor-client-mock-js:1.3.2") - testImplementation("io.ktor:ktor-client-mock-native:1.3.2") + testImplementation("io.ktor:ktor-client-mock-js:1.3.1") + testImplementation("io.ktor:ktor-client-mock-native:1.3.1") testImplementation("org.mock-server:mockserver-netty:5.3.0") - testImplementation("io.cucumber:cucumber-java8:7.0.0") - testImplementation("io.cucumber:cucumber-junit:7.0.0") + testImplementation("io.cucumber:cucumber-java8:7.3.3") + testImplementation("io.cucumber:cucumber-junit:7.3.3") +} + +application { + mainClass.set("io.kuzzle.sdk.protocol") } java { + withJavadocJar() withSourcesJar() } -application { - mainClassName = "io.kuzzle.sdk.protocol" +tasks.withType { + this.testLogging { + this.showStandardStreams = true + } } -tasks.withType { - archiveFileName.set("${artifactName}-${artifactVersion}-without-dependencies.jar") +val javadocJar = tasks.named("javadocJar") { + from(tasks.named("dokkaJavadoc")) +} + +tasks.named("jar") { + archiveClassifier.set("without-dependencies") } tasks { - register("fatJar") { - archiveFileName.set("${artifactName}-${artifactVersion}.jar") - duplicatesStrategy = DuplicatesStrategy.EXCLUDE + named("shadowJar") { + archiveClassifier.set("") + archiveBaseName.set(artifactName) manifest { - attributes("Main-Class" to application.mainClassName) + attributes(mapOf("Main-Class" to application.mainClass.get())) } - from(configurations.runtimeClasspath.get() - .onEach { println("Add from dependencies: ${it.name}") } - .map { if (it.isDirectory) it else zipTree(it) }) - val sourcesMain = sourceSets.main.get() - sourcesMain.allSource.forEach { println("Add from sources: ${it.name}") } - from(sourcesMain.output) } } -tasks.register("javadocJar") { - from(tasks.javadoc) - archiveClassifier.set("javadoc") -} - publishing { repositories { maven { @@ -115,41 +113,13 @@ publishing { } } publications { - create("kuzzle-sdk-jvm-thin") { - groupId = artifactGroup - artifactId = artifactName - version = "${artifactVersion}-without-dependencies" - artifact(tasks["sourcesJar"]) - artifact(tasks["javadocJar"]) - - from(components["java"]) - pom.withXml { - asNode().apply { - appendNode("description", pomDesc) - appendNode("name", rootProject.name) - appendNode("url", pomUrl) - appendNode("licenses").appendNode("license").apply { - appendNode("name", pomLicenseName) - appendNode("url", pomLicenseUrl) - appendNode("distribution", pomLicenseDist) - } - appendNode("developers").appendNode("developer").apply { - appendNode("id", pomDeveloperId) - appendNode("name", pomDeveloperName) - } - appendNode("scm").apply { - appendNode("url", pomScmUrl) - appendNode("connection", pomScmConnection) - } - } - } - } - create("kuzzle-sdk-jvm-fat") { + create("kuzzle-sdk-jvm") { groupId = artifactGroup artifactId = artifactName version = artifactVersion - artifact(tasks["fatJar"]) + artifact(tasks["jar"]) artifact(tasks["sourcesJar"]) + artifact(tasks["shadowJar"]) artifact(tasks["javadocJar"]) pom.withXml { @@ -178,6 +148,5 @@ publishing { signing { useInMemoryPgpKeys(System.getenv("MAVEN_CENTRAL_GPG"), System.getenv("MAVEN_CENTRAL_GPG_PASSWORD")) - sign(publishing.publications["kuzzle-sdk-jvm-thin"]) - sign(publishing.publications["kuzzle-sdk-jvm-fat"]) + sign(publishing.publications["kuzzle-sdk-jvm"]) } \ No newline at end of file diff --git a/doc/1/core-classes/kuzzle/constructor/snippets/constructor-java.java b/doc/1/core-classes/kuzzle/constructor/snippets/constructor-java.java index 4b03b113..c475ca32 100644 --- a/doc/1/core-classes/kuzzle/constructor/snippets/constructor-java.java +++ b/doc/1/core-classes/kuzzle/constructor/snippets/constructor-java.java @@ -1 +1 @@ -Kuzzle kuzzle = new Kuzzle(new WebSocket("kuzzle")); +Kuzzle sdk = new Kuzzle(new WebSocket("kuzzle")); diff --git a/doc/1/core-classes/kuzzle/constructor/snippets/constructor-kotlin.kt b/doc/1/core-classes/kuzzle/constructor/snippets/constructor-kotlin.kt index 705f5df9..2c05dac2 100644 --- a/doc/1/core-classes/kuzzle/constructor/snippets/constructor-kotlin.kt +++ b/doc/1/core-classes/kuzzle/constructor/snippets/constructor-kotlin.kt @@ -1 +1 @@ -val kuzzle: Kuzzle = Kuzzle(WebSocket("kuzzle")) +val sdk: Kuzzle = Kuzzle(WebSocket("kuzzle")) diff --git a/doc/1/getting-started/java/snippets/document-java.java b/doc/1/getting-started/java/snippets/document-java.java index c69a12d3..37911aa9 100644 --- a/doc/1/getting-started/java/snippets/document-java.java +++ b/doc/1/getting-started/java/snippets/document-java.java @@ -6,7 +6,7 @@ public class SnippetTest { private static Kuzzle kuzzle; public static void main(String[] args) { - + // @start try { // Creates a WebSocket connection. // Replace "kuzzle" with @@ -40,5 +40,6 @@ public static void main(String[] args) { // Disconnects the SDK. kuzzle.disconnect(); + // @end } } diff --git a/doc/1/getting-started/java/snippets/firstconnection-java.java b/doc/1/getting-started/java/snippets/firstconnection-java.java index 4eb47c83..6534ef59 100644 --- a/doc/1/getting-started/java/snippets/firstconnection-java.java +++ b/doc/1/getting-started/java/snippets/firstconnection-java.java @@ -6,7 +6,7 @@ public class SnippetTest { private static Kuzzle kuzzle; public static void main(String[] args) { - + // @start try { // Creates a WebSocket connection. // Replace "kuzzle" with @@ -42,5 +42,6 @@ public static void main(String[] args) { // Disconnects the SDK kuzzle.disconnect(); + // @end } } diff --git a/doc/1/getting-started/java/snippets/realtime-java.java b/doc/1/getting-started/java/snippets/realtime-java.java index 275e3ad7..55c5c6dd 100644 --- a/doc/1/getting-started/java/snippets/realtime-java.java +++ b/doc/1/getting-started/java/snippets/realtime-java.java @@ -7,6 +7,7 @@ public class SnippetTest { public static void main(String[] args) { + // @start try { // Creates a WebSocket connection. // Replace "kuzzle" with @@ -72,5 +73,6 @@ public static void main(String[] args) { // Disconnects the SDK. kuzzle.disconnect(); + // @end } } diff --git a/doc/1/getting-started/kotlin/snippets/document-kotlin.kt b/doc/1/getting-started/kotlin/snippets/document-kotlin.kt index 4e246cd8..89c33fb7 100644 --- a/doc/1/getting-started/kotlin/snippets/document-kotlin.kt +++ b/doc/1/getting-started/kotlin/snippets/document-kotlin.kt @@ -2,6 +2,7 @@ import io.kuzzle.sdk.* import io.kuzzle.sdk.protocol.WebSocket fun main() { + // @start // Creates a WebSocket connection. // Replace "kuzzle" with // your Kuzzle host name (e.g. "localhost") @@ -32,4 +33,5 @@ fun main() { // Disconnects the SDK. kuzzle.disconnect() } + // @end } \ No newline at end of file diff --git a/doc/1/getting-started/kotlin/snippets/firstconnection-kotlin.kt b/doc/1/getting-started/kotlin/snippets/firstconnection-kotlin.kt index 75c1e210..5557fd57 100644 --- a/doc/1/getting-started/kotlin/snippets/firstconnection-kotlin.kt +++ b/doc/1/getting-started/kotlin/snippets/firstconnection-kotlin.kt @@ -2,6 +2,7 @@ import io.kuzzle.sdk.*; import io.kuzzle.sdk.protocol.WebSocket; fun main() { + // @start // Creates a WebSocket connection. // Replace "kuzzle" with // your Kuzzle hostname like "localhost" @@ -33,4 +34,5 @@ fun main() { // Disconnects the SDK kuzzle.disconnect() } + // @end } diff --git a/doc/1/getting-started/kotlin/snippets/realtime-kotlin.kt b/doc/1/getting-started/kotlin/snippets/realtime-kotlin.kt index 47eafe07..8d3408c6 100644 --- a/doc/1/getting-started/kotlin/snippets/realtime-kotlin.kt +++ b/doc/1/getting-started/kotlin/snippets/realtime-kotlin.kt @@ -2,6 +2,7 @@ import io.kuzzle.sdk.* import io.kuzzle.sdk.protocol.WebSocket fun main() { + // @start // Creates a WebSocket connection. // Replace "kuzzle" with // your Kuzzle hostname like "localhost" @@ -63,4 +64,5 @@ fun main() { // Disconnects the SDK. kuzzle.disconnect() } + // @end } diff --git a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt index fb795709..c45d4e3e 100644 --- a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt +++ b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt @@ -68,33 +68,50 @@ open class Kuzzle { private fun onMessageReceived(event: MessageReceivedEvent) { val message = event.message var jsonObject: Map + val eventRequestId = event.payload["requestId"] as String? try { jsonObject = JsonSerializer.deserialize(message) as Map } catch (e: Exception) { - if (event.requestId != null) { - queries[event.requestId]?.completeExceptionally(InvalidJSON(event.message ?: "null")) - queries.remove(event.requestId) + if (eventRequestId != null) { + queries[eventRequestId]?.completeExceptionally(InvalidJSON(event.message ?: "null")) + queries.remove(eventRequestId) } else { protocol.trigger(UnhandledExceptionEvent(InvalidJSON(event.message ?: "null"))) } return } - - if (! jsonObject.containsKey("headers") && event.headers != null) { - jsonObject = jsonObject.plus("headers" to event.headers) - } + val response = Response() // If the message is empty, we take the requestId of the event, // to avoid error in fromMap function. - if (! jsonObject.containsKey("requestId") && event.requestId != null) { - jsonObject = jsonObject.plus("requestId" to event.requestId) - } + if (! jsonObject.containsKey("requestId") && eventRequestId != null) { + response.result = jsonObject + response.requestId = eventRequestId + response.status = event.status + if (event.headers != null) { + response.headers = event.headers as Map + if (response.headers!!.containsKey("X-Kuzzle-Volatile")) { + response.Volatile = response.headers!!["X-Kuzzle-Volatile"] as Map? + } + } - val response = Response().apply { - fromMap(jsonObject) + if (response.Volatile == null) { + response.Volatile = event.payload["volatile"] as Map? + } + response.controller = event.payload["controller"] as String? + response.action = event.payload["action"] as String? + response.index = event.payload["index"] as String? + response.collection = event.payload["collection"] as String? + } else { + if (! jsonObject.containsKey("headers") && event.headers != null) { + jsonObject = jsonObject.plus("headers" to event.headers) + } + response.apply { + fromMap(jsonObject) + } } - val requestId = event.requestId ?: response.room ?: response.requestId + val requestId = eventRequestId ?: response.room ?: response.requestId if (response.error?.id == "security.token.expired") { protocol.trigger(TokenExpiredEvent()) diff --git a/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt b/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt index 3cdb1731..c602291f 100644 --- a/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt +++ b/src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt @@ -1,3 +1,8 @@ package io.kuzzle.sdk.events -data class MessageReceivedEvent(var message: String?, var requestId: String? = null, var headers: Map>? = null) +data class MessageReceivedEvent( + var message: String?, + var payload: Map = emptyMap(), + var status: Int = 200, + var headers: Map>? = null +) diff --git a/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt b/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt index 2bc4ffb7..8ca6c0d5 100644 --- a/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt +++ b/src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt @@ -223,7 +223,14 @@ open class Http : AbstractProtocol { this.body = JsonSerializer.serialize(payload) } // trigger messageReceived - super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?, response.headers.toMap())) + super.trigger( + MessageReceivedEvent( + response.receive(), + payload, + response.status.value, + response.headers.toMap() + ) + ) } catch (e: Exception) { super.trigger(RequestErrorEvent(e, payload["requestId"] as String?)) } finally { @@ -254,7 +261,14 @@ open class Http : AbstractProtocol { this.body = if (requestInfo.body != null) JsonSerializer.serialize(requestInfo.body) else "" } // trigger messageReceived - super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?, response.headers.toMap())) + super.trigger( + MessageReceivedEvent( + response.receive(), + payload, + response.status.value, + response.headers.toMap() + ) + ) } catch (e: Exception) { super.trigger(RequestErrorEvent(e, payload["requestId"] as String?)) } finally { diff --git a/src/test/kotlin/io/kuzzle/sdk/protocolTest/WebSocketTests.kt b/src/test/kotlin/io/kuzzle/sdk/protocolTest/WebSocketTests.kt index ab89e502..d9bbf8e0 100644 --- a/src/test/kotlin/io/kuzzle/sdk/protocolTest/WebSocketTests.kt +++ b/src/test/kotlin/io/kuzzle/sdk/protocolTest/WebSocketTests.kt @@ -40,6 +40,7 @@ class WebSocketTests { val responseHeaders = headersOf("Content-Type" to listOf("application/json")) respond("{}", headers = responseHeaders) } + else -> error("Unhandled ${request.url.fullPath}") } }