Skip to content

Commit

Permalink
feat!: Removed MockWebServer deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa committed Nov 4, 2021
1 parent 45400cd commit ace88b1
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 165 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Bugs

#### Improvements
* Fix #3562: Kubernetes Mock Server improvements

#### Dependency Upgrade
* Fix #3562: Bump MockWebServer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@
*/
package io.fabric8.camelk.mock;

import io.fabric8.camelk.client.CamelKClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher;
import io.fabric8.mockwebserver.Context;
import io.fabric8.mockwebserver.ServerRequest;
import io.fabric8.mockwebserver.ServerResponse;
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import io.fabric8.camelk.client.CamelKClient;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.rules.ExternalResource;

import java.util.HashMap;
import java.util.Queue;

public class CamelKServer extends ExternalResource {

protected CamelKMockServer mock;
private CamelKClient client;

private boolean https;
private final boolean https;
// In this mode the mock web server will store, read, update and delete
// kubernetes resources using an in memory map and will appear as a real api
// server.
private boolean crudMode;
private final boolean crudMode;

public CamelKServer() {
this(true, false);
Expand All @@ -51,6 +48,7 @@ public CamelKServer(boolean https, boolean crudMode) {
this.crudMode = crudMode;
}

@Override
public void before() {
mock = crudMode
? new CamelKMockServer(new Context(), new MockWebServer(), new HashMap<>(), new KubernetesCrudDispatcher(), true)
Expand All @@ -59,6 +57,7 @@ public void before() {
client = mock.createCamelKClient();
}

@Override
public void after() {
mock.destroy();
client.close();
Expand All @@ -83,8 +82,4 @@ public <T> void expectAndReturnAsJson(String path, int code, T body) {
public void expectAndReturnAsString(String path, int code, String body) {
expect().withPath(path).andReturn(code, body).always();
}

public MockWebServer getMockServer() {
return mock.getServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@
*/
package io.fabric8.certmanager.server.mock;

import io.fabric8.certmanager.client.CertManagerClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher;
import io.fabric8.mockwebserver.Context;
import io.fabric8.mockwebserver.ServerRequest;
import io.fabric8.mockwebserver.ServerResponse;
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import io.fabric8.certmanager.client.CertManagerClient;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.rules.ExternalResource;

import java.util.HashMap;
import java.util.Queue;

public class CertManagerServer extends ExternalResource {

protected CertManagerMockServer mock;
private CertManagerClient client;

private boolean https;
private final boolean https;
// In this mode the mock web server will store, read, update and delete
// kubernetes resources using an in memory map and will appear as a real api
// server.
private boolean crudMode;
private final boolean crudMode;

public CertManagerServer() {
this(true, false);
Expand All @@ -51,6 +48,7 @@ public CertManagerServer(boolean https, boolean crudMode) {
this.crudMode = crudMode;
}

@Override
public void before() {
mock = crudMode
? new CertManagerMockServer(new Context(), new MockWebServer(), new HashMap<>(), new KubernetesCrudDispatcher(), true)
Expand All @@ -59,6 +57,7 @@ public void before() {
client = mock.createCertManager();
}

@Override
public void after() {
mock.destroy();
client.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@
*/
package io.fabric8.knative.mock;

import io.fabric8.knative.client.KnativeClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher;
import io.fabric8.mockwebserver.Context;
import io.fabric8.mockwebserver.ServerRequest;
import io.fabric8.mockwebserver.ServerResponse;
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import io.fabric8.knative.client.KnativeClient;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.rules.ExternalResource;

import java.util.HashMap;
import java.util.Queue;

public class KnativeServer extends ExternalResource {

protected KnativeMockServer mock;
private KnativeClient client;

private boolean https;
private final boolean https;
// In this mode the mock web server will store, read, update and delete
// kubernetes resources using an in memory map and will appear as a real api
// server.
private boolean crudMode;
private final boolean crudMode;

public KnativeServer() {
this(true, false);
Expand All @@ -51,6 +48,7 @@ public KnativeServer(boolean https, boolean crudMode) {
this.crudMode = crudMode;
}

@Override
public void before() {
mock = crudMode
? new KnativeMockServer(new Context(), new MockWebServer(), new HashMap<>(), new KubernetesCrudDispatcher(), true)
Expand All @@ -59,17 +57,16 @@ public void before() {
client = mock.createKnative();
}

@Override
public void after() {
mock.destroy();
client.close();
}


public KnativeClient getKnativeClient() {
return client;
}


public MockServerExpectation expect() {
return mock.expect();
}
Expand All @@ -83,8 +80,4 @@ public <T> void expectAndReturnAsJson(String path, int code, T body) {
public void expectAndReturnAsString(String path, int code, String body) {
expect().withPath(path).andReturn(code, body).always();
}

public MockWebServer getMockServer() {
return mock.getServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public class ServiceCatalogServer extends ExternalResource {
protected ServiceCatalogMockServer mock;
private ServiceCatalogClient client;

private boolean https;
private final boolean https;
// In this mode the mock web server will store, read, update and delete
// kubernetes resources using an in memory map and will appear as a real api
// server.
private boolean crudMode;
private final boolean crudMode;

public ServiceCatalogServer() {
this(true, false);
Expand All @@ -51,6 +51,7 @@ public ServiceCatalogServer(boolean https, boolean crudMode) {
this.crudMode = crudMode;
}

@Override
public void before() {
mock = crudMode
? new ServiceCatalogMockServer(new Context(), new MockWebServer(), new HashMap<>(), new KubernetesCrudDispatcher(), true)
Expand All @@ -59,12 +60,12 @@ public void before() {
client = mock.createServiceCatalog();
}

@Override
public void after() {
mock.destroy();
client.close();
}


public ServiceCatalogClient getServiceCatalogClient() {
return client;
}
Expand All @@ -83,8 +84,4 @@ public <T> void expectAndReturnAsJson(String path, int code, T body) {
public void expectAndReturnAsString(String path, int code, String body) {
expect().withPath(path).andReturn(code, body).always();
}

public MockWebServer getMockServer() {
return mock.getServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,23 @@

import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher;
import io.fabric8.mockwebserver.Context;
import io.fabric8.mockwebserver.ServerRequest;
import io.fabric8.mockwebserver.ServerResponse;
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import io.fabric8.tekton.client.TektonClient;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.rules.ExternalResource;

import java.util.HashMap;
import java.util.Queue;

public class TektonServer extends ExternalResource {

protected TektonMockServer mock;
private TektonClient client;

private boolean https;
private final boolean https;
// In this mode the mock web server will store, read, update and delete
// kubernetes resources using an in memory map and will appear as a real api
// server.
private boolean crudMode;
private final boolean crudMode;

public TektonServer() {
this(true, false);
Expand All @@ -51,6 +48,7 @@ public TektonServer(boolean https, boolean crudMode) {
this.crudMode = crudMode;
}

@Override
public void before() {
mock = crudMode
? new TektonMockServer(new Context(), new MockWebServer(), new HashMap<>(), new KubernetesCrudDispatcher(), true)
Expand All @@ -59,6 +57,7 @@ public void before() {
client = mock.createTekton();
}

@Override
public void after() {
mock.destroy();
client.close();
Expand All @@ -69,7 +68,6 @@ public TektonClient getTektonClient() {
return client;
}


public MockServerExpectation expect() {
return mock.expect();
}
Expand All @@ -83,8 +81,4 @@ public <T> void expectAndReturnAsJson(String path, int code, T body) {
public void expectAndReturnAsString(String path, int code, String body) {
expect().withPath(path).andReturn(code, body).always();
}

public MockWebServer getMockServer() {
return mock.getServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.mockwebserver.Context;
import io.fabric8.mockwebserver.ContextBuilder;
import io.fabric8.mockwebserver.DefaultMockServer;
import io.fabric8.mockwebserver.ServerRequest;
import io.fabric8.mockwebserver.ServerResponse;
Expand All @@ -38,15 +38,14 @@
// The class has moved under mvn:io.fabric8:kubernetes-server-mock in package: io.fabric8.client.server.mock
public class KubernetesMockServer extends DefaultMockServer {

private static final Context context = new ContextBuilder()
.build();
private static final Context context = new Context(Serialization.jsonMapper());

public KubernetesMockServer() {
this(true);
}

public KubernetesMockServer(boolean useHttps) {
this(new MockWebServer(), new HashMap(), useHttps);
this(new MockWebServer(), new HashMap<>(), useHttps);
}

public KubernetesMockServer(MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses, boolean useHttps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.rules.ExternalResource;

@Deprecated
// The class has moved under mvn:io.fabric8:kubernetes-server-mock in package: io.fabric8.client.server.mock
public class KubernetesServer extends ExternalResource {
private KubernetesMockServer mock;
private NamespacedKubernetesClient client;
private boolean https;
private final boolean https;

public KubernetesServer() {
this(true);
Expand Down Expand Up @@ -70,8 +69,4 @@ public <T> void expectAndReturnAsJson(String method, String path, int code, T bo
public void expectAndReturnAsString(String method, String path, int code, String body) {
expect().withPath(path).andReturn(code, body).always();
}

public MockWebServer getMockServer() {
return mock.getServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CustomResourceDefinitionProcessor {
private static final String V1BETA1_PATH = "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions";
private static final String V1_PATH = "/apis/apiextensions.k8s.io/v1/customresourcedefinitions";

private KubernetesAttributesExtractor extractor;
private final KubernetesAttributesExtractor extractor;

public CustomResourceDefinitionProcessor(KubernetesAttributesExtractor extractor) {
this.extractor = extractor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import static io.fabric8.mockwebserver.crud.AttributeType.NOT_EXISTS;
import static io.fabric8.mockwebserver.crud.AttributeType.WITHOUT;

public class KubernetesAttributesExtractor implements AttributeExtractor<HasMetadata> {
public class KubernetesAttributesExtractor implements AttributeExtractor {

private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesAttributesExtractor.class);

Expand Down Expand Up @@ -88,7 +88,7 @@ public class KubernetesAttributesExtractor implements AttributeExtractor<HasMeta
private static final String SCHEME = "http";
private static final String HOST = "localhost";

private Map<List<String>, CustomResourceDefinitionContext> crdContexts;
private final Map<List<String>, CustomResourceDefinitionContext> crdContexts;

public KubernetesAttributesExtractor() {
this(Collections.emptyList());
Expand Down Expand Up @@ -160,12 +160,6 @@ public AttributeSet fromResource(String s) {
return new AttributeSet();
}

@Override
public AttributeSet extract(String s) {
return fromPath(s);
}

@Override
public AttributeSet extract(HasMetadata hasMetadata) {
AttributeSet metadataAttributes = new AttributeSet();
String apiVersion = hasMetadata.getApiVersion();
Expand Down
Loading

0 comments on commit ace88b1

Please sign in to comment.