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

Commit 470edaa

Browse files
ALRubingerAndrew Rubinger
authored andcommitted
Skunkworks testing: Java as a docs language
* 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 * Work around a ReDoc bug (java language support needs scala too)
1 parent cbb05bd commit 470edaa

File tree

8 files changed

+174
-7
lines changed

8 files changed

+174
-7
lines changed

shnippet.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rootDirectory": "./site/testsuites",
33
"outputDirectory": "./site/snippets",
4-
"fileExtensions": [".js", ".ts", ".kt", ".gradle", ".xml", ".bash"],
4+
"fileExtensions": [".java", ".js", ".ts", ".kt", ".gradle", ".xml", ".bash"],
55
"exclude": ["pfiOverviewReadOfferingsJs", "pfiOverviewWriteJs", "pfiOverviewWriteOfferingsJs"],
66
"snippetTags": {
77
"start": ":snippet-start:",

site/docs/web5/build/decentralized-identifiers/how-to-create-did.mdx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import createADidDependencyGradle from '!!raw-loader!@site/snippets/testsuite-ja
99
import createADidDependencyMaven from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/createADidDependencyMaven.snippet.xml';
1010
import createADidDependency from '!!raw-loader!@site/snippets/testsuite-javascript/__tests__/web5/build/decentralized-identifiers/createADidDependency.snippet.bash';
1111

12-
<LanguageSwitcher languages="JavaScript, Kotlin" />
12+
<LanguageSwitcher languages="JavaScript, Kotlin, Java" />
1313

1414
# Create a DID
1515

@@ -34,7 +34,20 @@ You can create a DID using any of the [Web5-supported DID methods](/docs/web5/le
3434
nestedSnippets: {
3535
Gradle: {
3636
snippet: createADidDependencyGradle,
37-
language: 'js',
37+
language: 'kt',
38+
},
39+
Maven: {
40+
snippet: createADidDependencyMaven,
41+
language: 'xml',
42+
},
43+
},
44+
},
45+
{
46+
language: 'Java',
47+
nestedSnippets: {
48+
Gradle: {
49+
snippet: createADidDependencyGradle,
50+
language: 'kt',
3851
},
3952
Maven: {
4053
snippet: createADidDependencyMaven,
@@ -63,6 +76,7 @@ The following DID methods are supported:
6376
snippets={[
6477
{ snippetName: 'createDidDht', language: 'JavaScript' },
6578
{ snippetName: 'createDidDhtKt', language: 'Kotlin' },
79+
{ snippetName: 'createDidDhtJava', language: 'Java' },
6680
]}
6781
/>
6882

@@ -73,6 +87,7 @@ The following DID methods are supported:
7387
snippets={[
7488
{ snippetName: 'createDidJwk', language: 'JavaScript' },
7589
{ snippetName: 'createDidJwkKt', language: 'Kotlin' },
90+
{ snippetName: 'createDidJwkJava', language: 'Java' },
7691
]}
7792
/>
7893

@@ -229,4 +244,4 @@ In the example below, the `d` property of the private key component is masked. T
229244
}
230245
]
231246
}
232-
```
247+
```

site/docusaurus.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ let config = {
256256
},
257257
],
258258
},
259-
additionalLanguages: ['kotlin', 'swift', 'dart', 'rust'],
259+
// Must add Scala here to work around a Redoc bug: https://github.com/facebook/docusaurus/issues/7209
260+
additionalLanguages: ['kotlin', 'swift', 'dart', 'rust', 'java', 'scala'],
260261
},
261262
}),
262263
};

site/src/components/CodeSnippet.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import codeSnippets from '../../src/util/code-snippets-map.json'; // Import for
55
// Define the language map for the new system
66
const languageExtensionMap = {
77
javascript: 'js',
8+
java: 'java',
89
kotlin: 'kt',
910
swift: 'swift',
1011
dart: 'dart',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module version="4">
3+
<component name="AutoImportedSourceRoots">
4+
<option name="directories">
5+
<list>
6+
<option value="$MODULE_DIR$/testsuite-kotlin/src/main/java" />
7+
<option value="$MODULE_DIR$/testsuite-kotlin/src/main/kotlin" />
8+
<option value="$MODULE_DIR$/testsuite-kotlin/src/test/java" />
9+
<option value="$MODULE_DIR$/testsuite-kotlin/src/test/kotlin" />
10+
</list>
11+
</option>
12+
</component>
13+
</module>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module version="4">
3+
<component name="AutoImportedSourceRoots">
4+
<option name="directories">
5+
<list>
6+
<option value="$MODULE_DIR$/src/main/java" />
7+
<option value="$MODULE_DIR$/src/main/kotlin" />
8+
<option value="$MODULE_DIR$/src/test/java" />
9+
<option value="$MODULE_DIR$/src/test/kotlin" />
10+
</list>
11+
</option>
12+
</component>
13+
</module>

site/testsuites/testsuite-kotlin/pom.xml

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<properties>
1515
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<java.version>17</java.version>
1617
<kotlin.jvm.target>17</kotlin.jvm.target>
1718
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
1819
<version.assertj>3.25.2</version.assertj>
@@ -121,8 +122,8 @@
121122
</dependencies>
122123

123124
<build>
124-
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
125-
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
125+
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
126+
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
126127
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
127128
<plugins>
128129
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
@@ -179,6 +180,66 @@
179180
<configuration>
180181
<jvmTarget>${kotlin.jvm.target}</jvmTarget>
181182
</configuration>
183+
<executions>
184+
<execution>
185+
<id>compile</id>
186+
<phase>process-sources</phase>
187+
<goals>
188+
<goal>compile</goal>
189+
</goals>
190+
<configuration>
191+
<sourceDirs>
192+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
193+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
194+
</sourceDirs>
195+
</configuration>
196+
</execution>
197+
<execution>
198+
<id>test-compile</id>
199+
<phase>process-test-sources</phase>
200+
<goals>
201+
<goal>test-compile</goal>
202+
</goals>
203+
<configuration>
204+
<sourceDirs>
205+
<sourceDir>${project.basedir}/src/test/java</sourceDir>
206+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
207+
</sourceDirs>
208+
</configuration>
209+
</execution>
210+
</executions>
211+
</plugin>
212+
<plugin>
213+
<groupId>org.apache.maven.plugins</groupId>
214+
<artifactId>maven-compiler-plugin</artifactId>
215+
<configuration>
216+
<source>${java.version}</source>
217+
<target>${java.version}</target>
218+
</configuration>
219+
<executions>
220+
<execution>
221+
<id>default-compile</id>
222+
<phase>none</phase>
223+
</execution>
224+
<execution>
225+
<id>default-testCompile</id>
226+
<phase>none</phase>
227+
</execution>
228+
<execution>
229+
<id>java-compile</id>
230+
<phase>compile</phase>
231+
<goals>
232+
<goal>compile</goal>
233+
</goals>
234+
</execution>
235+
<execution>
236+
<id>java-test-compile</id>
237+
<phase>test-compile</phase>
238+
<goals>
239+
<goal>testCompile</goal>
240+
</goals>
241+
</execution>
242+
</executions>
182243
</plugin>
183244
</plugins>
184245
</build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package website.tbd.developer.site.java.docs.web5.build.decentralizedidentifiers;
2+
3+
import foundation.identity.did.DIDDocument;
4+
import org.junit.jupiter.api.Test;
5+
import web5.sdk.crypto.InMemoryKeyManager;
6+
import web5.sdk.dids.DidResolutionResult;
7+
import web5.sdk.dids.methods.dht.CreateDidDhtOptions;
8+
import web5.sdk.dids.methods.dht.DidDht;
9+
import web5.sdk.dids.methods.jwk.DidJwk;
10+
import web5.sdk.dids.methods.key.DidKey;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
class HowToCreateDidTest
15+
{
16+
17+
@Test
18+
void createDidDht(){
19+
20+
// :snippet-start: createDidDhtJava
21+
// Creates a DID using the DHT method and publishes the DID Document to the DHT
22+
final DidDht didDht = DidDht.Default.create(
23+
new InMemoryKeyManager(),
24+
new CreateDidDhtOptions(null,null,true,null,null));
25+
26+
// DID String
27+
final String did = didDht.getUri();
28+
29+
// DID and its associated data which can be exported and used in different contexts/apps
30+
final DidResolutionResult portableDid = DidDht.Default.resolve(did,null);
31+
32+
// DID Document
33+
final DIDDocument didDocument = portableDid.getDidDocument();
34+
// :snippet-end:
35+
36+
assertNotNull(did,"DID should not be null");
37+
assertTrue(did.startsWith("did:dht"),"Did should start with 'did:dht'");
38+
assertEquals(did, didDocument.getId().toString(),"ID of DID Document should match DID");
39+
}
40+
41+
@Test
42+
void createDidJwt() {
43+
// :snippet-start: createDidJwkJava
44+
// Creates a DID using the did:jwk method
45+
final DidJwk didJwk = DidJwk.Companion.create(new InMemoryKeyManager(), null);
46+
47+
// DID and its associated data which can be exported and used in different contexts/apps
48+
final DidResolutionResult portableDid = didJwk.resolve();
49+
50+
// DID String
51+
final String did = didJwk.getUri();
52+
53+
// DID Document
54+
final DIDDocument didDocument = portableDid.getDidDocument();
55+
// :snippet-end:
56+
57+
assertNotNull(did, "DID should not be null");
58+
assertTrue(did.startsWith("did:jwk"), "DID should start with 'did:jwk'");
59+
assertNotNull(didDocument, "DID Document should not be null");
60+
assertEquals(did, didDocument.getId().toString(),"ID od DID Document should match DID");
61+
}
62+
63+
}

0 commit comments

Comments
 (0)