Skip to content

Commit

Permalink
Fix checkstyle for tests as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Ciocanu committed Jul 17, 2024
1 parent aa83b20 commit bad0d19
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 82 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<linkXRef>false</linkXRef>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
</sourceDirectories>
</configuration>
<executions>
Expand Down
40 changes: 23 additions & 17 deletions src/test/java/io/diagrid/dapr/DaprComponentTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
/*
* 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.diagrid.dapr;

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

import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

public class DaprComponentTest {

@Test
public void componentStateStoreSerializationTest() {

DaprContainer dapr = new DaprContainer("daprio/daprd")
.withAppName("dapr-app")
.withAppPort(8081)
Expand All @@ -29,12 +40,12 @@ public void componentStateStoreSerializationTest() {

Set<Component> components = dapr.getComponents();
Assert.assertEquals(1, components.size());

Component kvstore = components.iterator().next();
Assert.assertEquals(false, kvstore.getMetadata().isEmpty());

String componentYaml = dapr.componentToYaml(kvstore);

String expectedComponentYAML = "metadata:\n" + " name: statestore\n"
String expectedComponentYaml = "metadata:\n" + " name: statestore\n"
+ "apiVersion: dapr.io/v1alpha1\n"
+ "kind: Component\n"
+ "spec:\n"
Expand All @@ -44,7 +55,7 @@ public void componentStateStoreSerializationTest() {
+ " type: state.in-memory\n"
+ " version: v1\n";

Assert.assertEquals(expectedComponentYAML, componentYaml);
Assert.assertEquals(expectedComponentYaml, componentYaml);
}

@Test
Expand All @@ -58,23 +69,19 @@ public void subscriptionSerializationTest() {
Set<Subscription> subscriptions = dapr.getSubscriptions();
Assert.assertEquals(1, subscriptions.size());

String subscriptionYaml =
dapr.subscriptionToYaml(subscriptions.iterator().next());
System.out.println(subscriptionYaml);

String expectedSubscriptionYAML = "metadata:\n" + " name: my-subscription\n"
String subscriptionYaml = dapr.subscriptionToYaml(subscriptions.iterator().next());
String expectedSubscriptionYaml = "metadata:\n" + " name: my-subscription\n"
+ "apiVersion: dapr.io/v1alpha1\n"
+ "kind: Subscription\n"
+ "spec:\n"
+ " route: /events\n"
+ " pubsubname: pubsub\n"
+ " topic: topic\n";
Assert.assertEquals(expectedSubscriptionYAML, subscriptionYaml);
Assert.assertEquals(expectedSubscriptionYaml, subscriptionYaml);
}

@Test
public void withComponentFromPath() {

URL stateStoreYaml = this.getClass().getClassLoader().getResource("components/statestore.yaml");
Path path = Paths.get(stateStoreYaml.getPath());

Expand All @@ -90,8 +97,7 @@ public void withComponentFromPath() {
Assert.assertEquals(false, kvstore.getMetadata().isEmpty());

String componentYaml = dapr.componentToYaml(kvstore);

String expectedComponentYAML = "metadata:\n" + //
String expectedComponentYaml = "metadata:\n" + //
" name: statestore\n"
+ //
"apiVersion: dapr.io/v1alpha1\n"
Expand Down Expand Up @@ -132,6 +138,6 @@ public void withComponentFromPath() {
+ //
"";

Assert.assertEquals(expectedComponentYAML, componentYaml);
Assert.assertEquals(expectedComponentYaml, componentYaml);
}
}
138 changes: 74 additions & 64 deletions src/test/java/io/diagrid/dapr/DaprContainerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
/*
* 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.diagrid.dapr;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Metadata;
import io.dapr.client.domain.State;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.Testcontainers;

import java.util.Map;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.any;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
Expand All @@ -14,22 +41,18 @@
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Metadata;
import io.dapr.client.domain.State;
import io.restassured.RestAssured;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.Testcontainers;

public class DaprContainerTest {

// Time-to-live for messages published.
private static final String MESSAGE_TTL_IN_SECONDS = "1000";
private static final String STATE_STORE_NAME = "kvstore";
private static final String KEY = "my-key";
private static final String PUBSUB_NAME = "pubsub";
private static final String PUBSUB_TOPIC_NAME = "topic";

@ClassRule
public static WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8081));

Expand All @@ -39,100 +62,87 @@ public class DaprContainerTest {
.withAppPort(8081)
.withAppChannelAddress("host.testcontainers.internal");

private final String STATE_STORE_NAME = "kvstore";
private final String KEY = "my-key";

private final String PUBSUB_NAME = "pubsub";
private final String PUBSUB_TOPIC_NAME = "topic";
// Time-to-live for messages published.
private static final String MESSAGE_TTL_IN_SECONDS = "1000";

/**
* Sets the Dapr properties for the test.
*/
@BeforeClass
public static void setDaprProperties() {
configStub();
Testcontainers.exposeHostPorts(8081);
System.setProperty("dapr.grpc.port", Integer.toString(daprContainer.getGrpcPort()));
}

private static void configStub() {
stubFor(any(urlMatching("/dapr/subscribe"))
.willReturn(aResponse().withBody("[]").withStatus(200)));

stubFor(get(urlMatching("/dapr/config"))
.willReturn(aResponse().withBody("[]").withStatus(200)));

stubFor(any(urlMatching("/([a-z1-9]*)"))
.willReturn(aResponse().withBody("[]").withStatus(200)));

// create a stub
stubFor(post(urlEqualTo("/events"))
.willReturn(aResponse().withBody("event received!").withStatus(200)));

configureFor("localhost", 8081);
}

@Test
public void testDaprContainerDefaults() {
Assert.assertEquals(
assertEquals(
"The pubsub and kvstore component should be configured by default",
2,
daprContainer.getComponents().size());
Assert.assertEquals(
assertEquals(
"A subscription should be configured by default if none is provided",
1,
daprContainer.getSubscriptions().size());
}

@Test
public void testStateStoreAPIs() throws Exception {

public void testStateStore() throws Exception {
try (DaprClient client = (new DaprClientBuilder()).build()) {

client.waitForSidecar(5000);
client.waitForSidecar(5000).block();

String value = "value";
// Save state
client.saveState(STATE_STORE_NAME, KEY, value).block();

// Get the state back from the state store
State<String> retrievedState =
client.getState(STATE_STORE_NAME, KEY, String.class).block();
State<String> retrievedState = client.getState(STATE_STORE_NAME, KEY, String.class).block();

Assert.assertEquals(
"The value retrieved should be the same as the one stored", value, retrievedState.getValue());
assertEquals("The value retrieved should be the same as the one stored", value, retrievedState.getValue());
}
}

@Test
public void testPlacement() throws Exception {
// Here we are just waiting for Dapr to be ready
try (DaprClient client = (new DaprClientBuilder()).build()) {
client.waitForSidecar(5000).block();
}

RestAssured.baseURI = "http://" + daprContainer.getHost() + ":" + daprContainer.getMappedPort(3500);
String actorRuntimePlacement =
RestAssured.given().get("/v1.0/metadata").jsonPath().getString("actorRuntime.placement");
JsonPath response = RestAssured.given().get("/v1.0/metadata").jsonPath();
String actorRuntimePlacement = response.getString("actorRuntime.placement");
boolean isPlacementConnected = actorRuntimePlacement.contentEquals("placement: connected");

assertTrue(isPlacementConnected);
}

@Test
public void testPubSubAPIs() throws Exception {

public void testPubSub() throws Exception {
try (DaprClient client = (new DaprClientBuilder()).build()) {

client.waitForSidecar(5000);
client.waitForSidecar(5000).block();

String message = "message content";
// Save state
client.publishEvent(
PUBSUB_NAME,
PUBSUB_TOPIC_NAME,
message,
singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS))
.block();
Map<String, String> metadata = singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS);
client.publishEvent(PUBSUB_NAME, PUBSUB_TOPIC_NAME, message, metadata).block();
}

verify(getRequestedFor(urlMatching("/dapr/config")));

verify(postRequestedFor(urlEqualTo("/events"))
.withHeader("Content-Type", equalTo("application/cloudevents+json")));
}

private static void configStub() {

stubFor(any(urlMatching("/dapr/subscribe"))
.willReturn(aResponse().withBody("[]").withStatus(200)));

stubFor(get(urlMatching("/dapr/config"))
.willReturn(aResponse().withBody("[]").withStatus(200)));

stubFor(any(urlMatching("/([a-z1-9]*)"))
.willReturn(aResponse().withBody("[]").withStatus(200)));

// create a stub
stubFor(post(urlEqualTo("/events"))
.willReturn(aResponse().withBody("event received!").withStatus(200)));

configureFor("localhost", 8081);
verify(postRequestedFor(urlEqualTo("/events")).withHeader("Content-Type", equalTo("application/cloudevents+json")));
}
}
15 changes: 14 additions & 1 deletion src/test/java/io/diagrid/dapr/DaprPlacementContainerTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
* 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.diagrid.dapr;

import org.junit.Assert;
Expand All @@ -6,7 +19,7 @@

public class DaprPlacementContainerTest {

@ClassRule()
@ClassRule
public static DaprPlacementContainer placement = new DaprPlacementContainer("daprio/placement");

@Test
Expand Down

0 comments on commit bad0d19

Please sign in to comment.