Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Skunkworks testing: Java as a docs language
Browse files Browse the repository at this point in the history
* Configure Kotlin and Java sources to coexist in the testsuite
* Make a real Java port of an existing Kotlin test
* Hook Java snippets into docs
* Add Java support to Shnippet
* Configure Docusaurus CodeBlock component for Java syntax highlighting
* Add Java support to CodeSnippet component
  • Loading branch information
ALRubinger committed Feb 21, 2024
1 parent 0ac70f1 commit 420a834
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 11 deletions.
2 changes: 1 addition & 1 deletion shnippet.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"rootDirectory": "./site/testsuites",
"outputDirectory": "./site/snippets",
"fileExtensions": [".js", ".ts", ".kt", ".gradle", ".xml", ".bash"],
"fileExtensions": [".java", ".js", ".ts", ".kt", ".gradle", ".xml", ".bash"],
"exclude": ["pfiOverviewReadOfferingsJs", "pfiOverviewWriteJs", "pfiOverviewWriteOfferingsJs"],
"snippetTags": {
"start": ":snippet-start:",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ sidebar_position: 1
import createDidDht from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/createDidDht.snippet.js';
import createDidJwk from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/createDidJwk.snippet.js'
import requiredDidImports from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/requiredDidImports.snippet.js';
import createDidDhtJava from '!!raw-loader!@site/snippets/testsuite-kotlin/src/test/java/website/tbd/developer/site/java/docs/web5/build/decentralizedidentifiers/createDidDhtJava.snippet.java';
import createDidDhtKt from '!!raw-loader!@site/snippets/testsuite-kotlin/src/test/kotlin/docs/web5/build/decentralizedidentifiers/createDidDhtKt.snippet.kt';
import createDidJwtJava from '!!raw-loader!@site/snippets/testsuite-kotlin/src/test/java/website/tbd/developer/site/java/docs/web5/build/decentralizedidentifiers/createDidJwtJava.snippet.java';
import createDidJwkKt from '!!raw-loader!@site/snippets/testsuite-kotlin/src/test/kotlin/docs/web5/build/decentralizedidentifiers/createDidJwkKt.snippet.kt'
import requiredDidImportsKt from '!!raw-loader!@site/snippets/testsuite-kotlin/src/test/kotlin/docs/web5/build/decentralizedidentifiers/requiredDidImportsKt.snippet.kt';
import createADidDependencyGradle from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/createADidDependencyGradle.snippet.gradle';
import createADidDependencyMaven from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/createADidDependencyMaven.snippet.xml';
import createADidDependency from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/createADidDependency.snippet.bash';

<LanguageSwitcher languages="JavaScript, Kotlin" />
<LanguageSwitcher languages="JavaScript, Kotlin, Java" />

# Create a DID

Expand All @@ -39,7 +41,20 @@ You can create a DID using any of the [Web5-supported DID methods](/docs/web5/le
nestedSnippets: {
Gradle: {
snippet: createADidDependencyGradle,
language: 'js',
language: 'kt',
},
Maven: {
snippet: createADidDependencyMaven,
language: 'xml',
},
},
},
{
language: 'Java',
nestedSnippets: {
Gradle: {
snippet: createADidDependencyGradle,
language: 'kt',
},
Maven: {
snippet: createADidDependencyMaven,
Expand Down Expand Up @@ -68,6 +83,7 @@ The following DID methods are supported:
snippets={[
{ snippetContent: createDidDht, language: 'JavaScript' },
{ snippetContent: createDidDhtKt, language: 'Kotlin' },
{ snippetContent: createDidDhtJava, language: 'Java' },
]}
/>

Expand All @@ -78,7 +94,8 @@ The following DID methods are supported:
snippets={[
{ snippetContent: createDidJwk, language: 'JavaScript' },
{ snippetContent: createDidJwkKt, language: 'Kotlin' },
]}
{ snippetContent: createDidKeyJava, language: 'Java' },
]}
/>

## Example of DID properties
Expand Down Expand Up @@ -234,4 +251,4 @@ In the example below, the `d` property of the private key component is masked. T
}
]
}
```
```
2 changes: 1 addition & 1 deletion site/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ let config = {
},
],
},
additionalLanguages: ['kotlin', 'swift', 'dart', 'rust'],
additionalLanguages: ['java', 'kotlin', 'swift', 'dart', 'rust'],
},
}),
};
Expand Down
1 change: 1 addition & 0 deletions site/src/components/CodeSnippet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import codeSnippets from '../../src/util/code-snippets-map.json'; // Import for
// Define the language map for the new system
const languageExtensionMap = {
javascript: 'js',
java: 'java',
kotlin: 'kt',
swift: 'swift',
dart: 'dart',
Expand Down
13 changes: 13 additions & 0 deletions site/testsuites/testsuite-kotlin/kotlin-testsuite.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AutoImportedSourceRoots">
<option name="directories">
<list>
<option value="$MODULE_DIR$/src/main/java" />
<option value="$MODULE_DIR$/src/main/kotlin" />
<option value="$MODULE_DIR$/src/test/java" />
<option value="$MODULE_DIR$/src/test/kotlin" />
</list>
</option>
</component>
</module>
71 changes: 66 additions & 5 deletions site/testsuites/testsuite-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<kotlin.jvm.target>17</kotlin.jvm.target>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<version.assertj>3.25.2</version.assertj>
Expand Down Expand Up @@ -114,8 +115,8 @@
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
Expand Down Expand Up @@ -172,6 +173,66 @@
<configuration>
<jvmTarget>${kotlin.jvm.target}</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand All @@ -192,11 +253,11 @@
<name>jboss</name>
<url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url>
</repository>
<!--
Address Danubetech being down
<!--
Address Danubetech being down
https://github.com/TBD54566975/developer.tbd.website/issues/1242
Remove when we have TBD repo dependencies proxied in a more permanent
Remove when we have TBD repo dependencies proxied in a more permanent
Artifactory repo, or when we no longer have a danubetech dependency in the
supply chain
-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package website.tbd.developer.site.java.docs.web5.build.decentralizedidentifiers;

import foundation.identity.did.DIDDocument;
import org.junit.jupiter.api.Test;
import web5.sdk.crypto.InMemoryKeyManager;
import web5.sdk.dids.DidResolutionResult;
import web5.sdk.dids.methods.dht.CreateDidDhtOptions;
import web5.sdk.dids.methods.dht.DidDht;
import web5.sdk.dids.methods.key.DidKey;

import static org.junit.jupiter.api.Assertions.*;

class HowToCreateDidTest
{

@Test
void createDidDht(){

// :snippet-start: createDidDhtJava
// Creates a DID using the DHT method and publishes the DID Document to the DHT
final DidDht didDht = DidDht.Default.create(
new InMemoryKeyManager(),
new CreateDidDhtOptions(null,null,true,null,null));

// DID String
final String did = didDht.getUri();

// DID and its associated data which can be exported and used in different contexts/apps
final DidResolutionResult portableDid = DidDht.Default.resolve(did,null);

// DID Document
final DIDDocument didDocument = portableDid.getDidDocument();
// :snippet-end:

assertNotNull(did,"DID should not be null");
assertTrue(did.startsWith("did:dht"),"Did should start with 'did:dht'");
assertEquals(did, didDocument.getId().toString(),"ID od DID Document should match DID");
}

@Test
void createDidKey() {
// :snippet-start: createDidKeyJava
// Creates a DID using the did:key method
final DidKey didKey = DidKey.Companion.create(new InMemoryKeyManager(), null);

// DID and its associated data which can be exported and used in different contexts/apps
final DidResolutionResult portableDid = didKey.resolve();

// DID String
final String did = didKey.getUri();

// DID Document
final DIDDocument didDocument = portableDid.getDidDocument();
// :snippet-end:

assertNotNull(did, "DID should not be null");
assertTrue(did.startsWith("did:key"), "DID should start with 'did:key'");
assertNotNull(didDocument, "DID Document should not be null");
assertEquals(did, didDocument.getId().toString(),"ID od DID Document should match DID");
}

}

0 comments on commit 420a834

Please sign in to comment.