Skip to content

Commit

Permalink
extracting classes
Browse files Browse the repository at this point in the history
Signed-off-by: salaboy <Salaboy@gmail.com>
  • Loading branch information
salaboy committed Jul 25, 2024
1 parent 1bdda4a commit 4238719
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 131 deletions.
1 change: 1 addition & 0 deletions testcontainers-dapr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<artifactId>testcontainers-dapr</artifactId>
<name>testcontainers-dapr</name>
<description>Testcontainers Dapr Module</description>
<version>0.12.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2024 The Dapr Authors
* 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 io.dapr.testcontainers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Represents a Dapr component.
*/
public class Component {
private String name;
private String type;
private String version;
private List<MetadataEntry> metadata;

/**
* Creates a new component.
*
* @param name Component name.
* @param type Component type.
* @param version Component version.
* @param metadata Metadata.
*/
public Component(String name, String type, String version, Map<String, Object> metadata) {
this.name = name;
this.type = type;
this.version = version;
this.metadata = new ArrayList<MetadataEntry>();
if (!metadata.isEmpty()) {
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
this.metadata.add(new MetadataEntry(entry.getKey(), entry.getValue()));
}
}
}

/**
* Creates a new component.
*
* @param name Component name.
* @param type Component type.
* @param version Component version.
* @param metadataEntries Component metadata entries.
*/
public Component(String name, String type, String version, List<MetadataEntry> metadataEntries) {
this.name = name;
this.type = type;
this.version = version;
metadata = Objects.requireNonNull(metadataEntries);
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public List<MetadataEntry> getMetadata() {
return metadata;
}

public String getVersion() {
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,127 +32,10 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class DaprContainer extends GenericContainer<DaprContainer> {

public enum DaprLogLevel {
ERROR,
WARN,
INFO,
DEBUG
}

public static class Subscription {
String name;
String pubsubName;
String topic;
String route;

/**
* Creates a new subscription.
* @param name Subscription name.
* @param pubsubName PubSub name.
* @param topic Topic name.
* @param route Route.
*/
public Subscription(String name, String pubsubName, String topic, String route) {
this.name = name;
this.pubsubName = pubsubName;
this.topic = topic;
this.route = route;
}
}

public static class MetadataEntry {
String name;
Object value;

public MetadataEntry(String name, Object value) {
this.name = name;
this.value = value;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Object getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

/**
* Represents a Dapr component.
*/
public static class Component {
String name;

String type;

String version;

List<MetadataEntry> metadata;

/**
* Creates a new component.
* @param name Component name.
* @param type Component type.
* @param version Component version.
* @param metadata Metadata.
*/
public Component(String name, String type, String version, Map<String, Object> metadata) {
this.name = name;
this.type = type;
this.version = version;
this.metadata = new ArrayList<MetadataEntry>();
if (!metadata.isEmpty()) {
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
this.metadata.add(new MetadataEntry(entry.getKey(), entry.getValue()));
}
}
}

/**
* Creates a new component.
* @param name Component name.
* @param type Component type.
* @param version Component version.
* @param metadataEntries Component metadata entries.
*/
public Component(String name, String type, String version, List<MetadataEntry> metadataEntries) {
this.name = name;
this.type = type;
this.version = version;
metadata = Objects.requireNonNull(metadataEntries);
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public List<MetadataEntry> getMetadata() {
return metadata;
}

public String getVersion() {
return version;
}
}

private static final int DAPRD_HTTP_PORT = 3500;
private static final int DAPRD_GRPC_PORT = 50001;
private final Set<Component> components = new HashSet<>();
Expand Down Expand Up @@ -304,15 +187,15 @@ public Map<String, Object> componentToMap(Component component) {
componentProps.put("kind", "Component");

Map<String, String> componentMetadata = new LinkedHashMap<>();
componentMetadata.put("name", component.name);
componentMetadata.put("name", component.getName());
componentProps.put("metadata", componentMetadata);

Map<String, Object> componentSpec = new HashMap<>();
componentSpec.put("type", component.type);
componentSpec.put("version", component.version);
componentSpec.put("type", component.getType());
componentSpec.put("version", component.getVersion());

if (!component.metadata.isEmpty()) {
componentSpec.put("metadata", component.metadata);
if (!component.getMetadata().isEmpty()) {
componentSpec.put("metadata", component.getMetadata());
}
componentProps.put("spec", componentSpec);
return componentProps;
Expand All @@ -329,13 +212,13 @@ public Map<String, Object> subscriptionToMap(Subscription subscription) {
subscriptionProps.put("kind", "Subscription");

Map<String, String> subscriptionMetadata = new LinkedHashMap<>();
subscriptionMetadata.put("name", subscription.name);
subscriptionMetadata.put("name", subscription.getName());
subscriptionProps.put("metadata", subscriptionMetadata);

Map<String, Object> subscriptionSpec = new HashMap<>();
subscriptionSpec.put("pubsubname", subscription.pubsubName);
subscriptionSpec.put("topic", subscription.topic);
subscriptionSpec.put("route", subscription.route);
subscriptionSpec.put("pubsubname", subscription.getPubsubName());
subscriptionSpec.put("topic", subscription.getTopic());
subscriptionSpec.put("route", subscription.getRoute());

subscriptionProps.put("spec", subscriptionSpec);
return subscriptionProps;
Expand Down Expand Up @@ -385,12 +268,12 @@ protected void configure() {

for (Component component : components) {
String componentYaml = componentToYaml(component);
withCopyToContainer(Transferable.of(componentYaml), "/components/" + component.name + ".yaml");
withCopyToContainer(Transferable.of(componentYaml), "/components/" + component.getName() + ".yaml");
}

for (Subscription subscription : subscriptions) {
String subscriptionYaml = subscriptionToYaml(subscription);
withCopyToContainer(Transferable.of(subscriptionYaml), "/components/" + subscription.name + ".yaml");
withCopyToContainer(Transferable.of(subscriptionYaml), "/components/" + subscription.getName() + ".yaml");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.dapr.testcontainers;

public enum DaprLogLevel {
ERROR,
WARN,
INFO,
DEBUG
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 The Dapr Authors
* 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 io.dapr.testcontainers;

public class MetadataEntry {
private String name;
private Object value;

public MetadataEntry(String name, Object value) {
this.name = name;
this.value = value;
}

public String getName() {
return name;
}

public Object getValue() {
return value;
}

public void setName(String name) {
this.name = name;
}

public void setValue(Object value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 The Dapr Authors
* 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 io.dapr.testcontainers;

public class Subscription {
private String name;
private String pubsubName;
private String topic;
private String route;

/**
* Creates a new subscription.
*
* @param name Subscription name.
* @param pubsubName PubSub name.
* @param topic Topic name.
* @param route Route.
*/
public Subscription(String name, String pubsubName, String topic, String route) {
this.name = name;
this.pubsubName = pubsubName;
this.topic = topic;
this.route = route;
}

public String getName() {
return name;
}

public String getPubsubName() {
return pubsubName;
}

public String getTopic() {
return topic;
}

public String getRoute() {
return route;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

package io.dapr.testcontainers;

import io.dapr.testcontainers.DaprContainer.Component;
import io.dapr.testcontainers.DaprContainer.Subscription;
import org.junit.Assert;
import org.junit.Test;

Expand Down

0 comments on commit 4238719

Please sign in to comment.