Skip to content

Commit

Permalink
Added Jetty12 backed extension and factory and fixed loading via exte…
Browse files Browse the repository at this point in the history
…nsion factories
  • Loading branch information
tomakehurst committed Nov 6, 2024
1 parent 3a44fee commit b673ea1
Show file tree
Hide file tree
Showing 21 changed files with 995 additions and 22 deletions.
28 changes: 18 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {

plugins {
id 'java-library'
id 'java-test-fixtures'
id 'signing'
id 'maven-publish'
id 'idea'
Expand All @@ -34,7 +35,7 @@ allprojects {

ext {
versions = [
wiremock: "3.9.1",
wiremock: "3.10.0",
grpc : "1.68.0",
protobuf: "3.25.5"
]
Expand Down Expand Up @@ -104,14 +105,20 @@ dependencies {

implementation 'javax.annotation:javax.annotation-api:1.3.2'

// testImplementation project(":")
testImplementation(platform('org.junit:junit-bom:5.11.0'))
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.hamcrest:hamcrest-core:3.0"
testImplementation "org.hamcrest:hamcrest-library:3.0"
testImplementation 'org.awaitility:awaitility:4.2.2'
testFixturesApi(platform('org.junit:junit-bom:5.11.0'))
testFixturesApi "org.junit.jupiter:junit-jupiter"
testFixturesApi "org.hamcrest:hamcrest-core:3.0"
testFixturesApi "org.hamcrest:hamcrest-library:3.0"
testFixturesApi 'org.awaitility:awaitility:4.2.2'

testImplementation "io.grpc:grpc-okhttp"
testFixturesApi "io.grpc:grpc-okhttp"

testFixturesApi 'javax.annotation:javax.annotation-api:1.3.2'

testImplementation(testFixtures(project(":")), {
exclude group: 'org.eclipse.jetty'
exclude group: 'org.eclipse.jetty.http2'
})
}

task sourcesJar(type: Jar, dependsOn: classes) {
Expand Down Expand Up @@ -231,8 +238,8 @@ protobuf {
task.generateDescriptorSet = true
task.descriptorSetOptions.includeImports = true
}
ofSourceSet('test').each { task ->
task.descriptorSetOptions.path = "$projectDir/src/test/resources/wiremock/grpc/greetings.dsc"
ofSourceSet('testFixtures').each { task ->
task.descriptorSetOptions.path = "$projectDir/src/testFixtures/resources/wiremock/grpc/greetings.dsc"
}
ofSourceSet('bookings').each { task ->
task.descriptorSetOptions.path = "$projectDir/src/test/resources/wiremock/grpc/bookings.dsc"
Expand All @@ -243,6 +250,7 @@ protobuf {
processTestResources.dependsOn generateProto
processTestResources.dependsOn generateTestProto
processTestResources.dependsOn generateBookingsProto
processTestFixturesResources.dependsOn generateTestFixturesProto

nexusPublishing {
// See https://github.com/wiremock/community/blob/main/infra/maven-central.md
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rootProject.name = 'wiremock-grpc-extension'

include 'wiremock-grpc-extension-standalone'
include 'wiremock-grpc-extension-jetty12'
13 changes: 7 additions & 6 deletions src/main/java/org/wiremock/grpc/GrpcExtensionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
import com.github.tomakehurst.wiremock.extension.Extension;
import com.github.tomakehurst.wiremock.extension.ExtensionFactory;
import com.github.tomakehurst.wiremock.extension.WireMockServices;
import com.github.tomakehurst.wiremock.http.HttpServerFactoryLoader;
import java.util.List;
import org.wiremock.annotations.Beta;
import org.wiremock.grpc.internal.GrpcAdminApiExtension;
import org.wiremock.grpc.internal.GrpcHttpServerFactory;

@Beta(justification = "Incubating extension: https://github.com/wiremock/wiremock/issues/2383")
public class GrpcExtensionFactory implements ExtensionFactory {

@Override
public List<Extension> create(WireMockServices services) {
GrpcHttpServerFactory grpcHttpServerFactory =
new GrpcHttpServerFactory(services.getStores().getBlobStore("grpc"));
return List.of(new GrpcAdminApiExtension(grpcHttpServerFactory), grpcHttpServerFactory);
return List.of(new GrpcHttpServerFactory(services.getStores().getBlobStore("grpc")));
}

@Override
public boolean isLoadable() {
return HttpServerFactoryLoader.isJetty11();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package org.wiremock.grpc.internal;

import com.github.tomakehurst.wiremock.admin.Router;
import com.github.tomakehurst.wiremock.common.Exceptions;
import com.github.tomakehurst.wiremock.core.Options;
import com.github.tomakehurst.wiremock.http.AdminRequestHandler;
import com.github.tomakehurst.wiremock.http.HttpServer;
import com.github.tomakehurst.wiremock.http.HttpServerFactory;
import com.github.tomakehurst.wiremock.http.StubRequestHandler;
import com.github.tomakehurst.wiremock.extension.AdminApiExtension;
import com.github.tomakehurst.wiremock.http.*;
import com.github.tomakehurst.wiremock.jetty11.Jetty11HttpServer;
import com.github.tomakehurst.wiremock.store.BlobStore;
import com.google.protobuf.DescriptorProtos;
Expand All @@ -33,10 +32,10 @@
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class GrpcHttpServerFactory implements HttpServerFactory {
public class GrpcHttpServerFactory implements HttpServerFactory, AdminApiExtension {

private final BlobStore protoDescriptorStore;
private GrpcFilter grpcFilter;
protected GrpcFilter grpcFilter;

public GrpcHttpServerFactory(BlobStore protoDescriptorStore) {
this.protoDescriptorStore = protoDescriptorStore;
Expand Down Expand Up @@ -93,4 +92,9 @@ protected void decorateMockServiceContextBeforeConfig(
}
};
}

@Override
public void contributeAdminApiRoutes(Router router) {
router.add(RequestMethod.POST, "/ext/grpc/reset", new GrpcResetAdminApiTask(this));
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
37 changes: 37 additions & 0 deletions src/testFixtures/resources/wiremock/mappings/hello-world.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"mappings": [{
"request": {
"headers": {"Accept": {"or": [
{"equalTo": "*/*"},
{"equalTo": "text/plain"}
]}},
"method": "GET",
"urlPath": "/hello"
},
"metadata": {"mocklab": {
"response-example-attachment": "Hello World",
"created": {
"at": "2024-01-09T15:32:35.424189986Z",
"by": "vqe9q",
"via": "ADMIN_API"
},
"updated": {
"at": "2024-01-09T15:34:11.584464236Z",
"by": "vqe9q",
"via": "ADMIN_API"
}
}},
"response": {
"headers": {"Content-Type": "text/plain"},
"body": "Hello World!",
"status": 200
},
"name": "Hello",
"postServeActions": [],
"id": "d469c289-22fb-4186-90ab-deb6ffb6db5c",
"persistent": true,
"priority": 5,
"uuid": "d469c289-22fb-4186-90ab-deb6ffb6db5c"
}],
"meta": {"total": 1}
}
146 changes: 146 additions & 0 deletions wiremock-grpc-extension-jetty12/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
buildscript {
repositories {
maven {
url "https://oss.sonatype.org"
}
mavenCentral()
}
}

plugins {
id 'java-library'
id 'java-test-fixtures'
id 'signing'
id 'maven-publish'
id 'idea'
id 'eclipse'
id 'project-report'
id 'com.diffplug.spotless' version '6.25.0'
id 'com.github.johnrengelman.shadow' version '8.1.1'
id "com.google.protobuf" version "0.9.4"
}

repositories {
mavenLocal()
mavenCentral()
}

group 'org.wiremock'

dependencies {
api project(":"), {
exclude group: 'org.eclipse.jetty'
exclude group: 'org.eclipse.jetty.http2'
}
api "org.wiremock:wiremock-jetty12:$versions.wiremock"

testImplementation(testFixtures(project(":")), {
exclude group: 'org.eclipse.jetty'
exclude group: 'org.eclipse.jetty.http2'
})
}

task sourcesJar(type: Jar, dependsOn: classes) {
archiveClassifier.set('sources')
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
archiveClassifier.set('javadoc')
from javadoc.destinationDir
}

task testJar(type: Jar, dependsOn: testClasses) {
archiveClassifier.set('tests')
from sourceSets.test.output
}

publishing {
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/wiremock/wiremock-grpc-extension"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}

publications {
main(MavenPublication) { publication ->
from components.java
artifact sourcesJar
artifact javadocJar
artifact testJar

pom.packaging 'jar'
pom.withXml {
asNode().appendNode('description', 'Mock gRPC services with WireMock')
asNode().children().last() + pomInfo
}
}
}
}

signing {
// Docs: https://github.com/wiremock/community/blob/main/infra/maven-central.md
required {
!version.toString().contains("SNAPSHOT") && (gradle.taskGraph.hasTask("uploadArchives") || gradle.taskGraph.hasTask("publish") || gradle.taskGraph.hasTask("publishToMavenLocal"))
}
def signingKey = providers.environmentVariable("OSSRH_GPG_SECRET_KEY").orElse("").get()
def signingPassphrase = providers.environmentVariable("OSSRH_GPG_SECRET_KEY_PASSWORD").orElse("").get()
if (!signingKey.isEmpty() && !signingPassphrase.isEmpty()) {
println "Using PGP key from env vars"
useInMemoryPgpKeys(signingKey, signingPassphrase)
} else {
println "Using default PGP key"
}

sign publishing.publications
}

assemble.dependsOn clean, shadowJar
publishMainPublicationToMavenLocal.dependsOn jar
publishMainPublicationToGitHubPackagesRepository.dependsOn jar


task localRelease {
dependsOn clean, assemble, publishToMavenLocal
}


test {
useJUnitPlatform()
testLogging {
events "PASSED", "FAILED", "SKIPPED"
exceptionFormat "full"
}
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.27.5"
}

plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:$versions.grpc"
}
}
generateProtoTasks {
all()*.plugins {
grpc {
outputSubDir = 'java'
}
}

all().each { task ->
task.generateDescriptorSet = true
task.descriptorSetOptions.path = "$projectDir/src/test/resources/wiremock/grpc/services.dsc"
}
}
}

processTestResources.dependsOn generateProto
processTestResources.dependsOn generateTestProto
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2023-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wiremock.grpc;

import com.github.tomakehurst.wiremock.extension.Extension;
import com.github.tomakehurst.wiremock.extension.ExtensionFactory;
import com.github.tomakehurst.wiremock.extension.WireMockServices;
import java.util.List;

import com.github.tomakehurst.wiremock.http.HttpServerFactoryLoader;
import org.wiremock.annotations.Beta;
import org.wiremock.grpc.internal.GrpcAdminApiExtension;
import org.wiremock.grpc.internal.GrpcHttpServerFactory;
import org.wiremock.grpc.internal.Jetty12GrpcHttpServerFactory;

@Beta(justification = "Incubating extension: https://github.com/wiremock/wiremock/issues/2383")
public class Jetty12GrpcExtensionFactory implements ExtensionFactory {

@Override
public List<Extension> create(WireMockServices services) {
return List.of(new Jetty12GrpcHttpServerFactory(services.getStores().getBlobStore("grpc")));
}

@Override
public boolean isLoadable() {
return !HttpServerFactoryLoader.isJetty11();
}
}
Loading

0 comments on commit b673ea1

Please sign in to comment.