Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions sharding-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>6.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alpha.mongodb.sharding.example;

import com.alpha.mongodb.sharding.example.state.ScenarioState;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import static io.cucumber.spring.CucumberTestContext.SCOPE_CUCUMBER_GLUE;

@Configuration
public class CucumberTestConfiguration {

@Bean
@Scope(SCOPE_CUCUMBER_GLUE)
public ScenarioState featureState() {
return new ScenarioState();
}

@Bean
public OkHttpClient httpClient() {
return new OkHttpClient();
}

@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.alpha.mongodb.sharding.example;

import lombok.experimental.UtilityClass;

@UtilityClass
public class CucumberTestConstants {

public static final String SCOPE = "scope";
public static final String CUKE = "cuke";

public static final String SCHEME = "http";
public static final String BASE_HOSTNAME = "localhost";
public static final int PORT = 8080;
public static final String BASE_PATH = "sharding-example";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.alpha.mongodb.sharding.example;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/integration/resources/features", plugin = {"pretty", "html:target/site/cucumber.html"})
public class ExampleApplicationCucumber {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.alpha.mongodb.sharding.example;

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@CucumberContextConfiguration
@ContextConfiguration(classes = {Application.class, ExampleApplicationCucumber.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class SpringIntegrationTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alpha.mongodb.sharding.example.state;

import io.cucumber.java.Scenario;
import io.cucumber.spring.ScenarioScope;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@ScenarioScope
@Data
@NoArgsConstructor(onConstructor = @__(@Autowired))
public class ScenarioState {
private List<StepDefinitionState> stepDefinitionState = new ArrayList<>();

private StepDefinitionState sharedStepDefinitionState;

private Scenario scenario;

public StepDefinitionState getPreviousStepDefinitionState() {
return stepDefinitionState.get(stepDefinitionState.size() - 2);
}

public StepDefinitionState getCurrentStepDefinitionState() {
return stepDefinitionState.get(stepDefinitionState.size() - 1);
}

public void addStep() {
stepDefinitionState.add(new StepDefinitionState());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.alpha.mongodb.sharding.example.state;

import lombok.Data;
import okhttp3.Request;
import okhttp3.Response;

@Data
public class StepDefinitionState {
private Request request;
private Object requestBody;
private Response response;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.alpha.mongodb.sharding.example.steps;

import com.alpha.mongodb.sharding.example.CucumberTestConstants;
import com.alpha.mongodb.sharding.example.state.ScenarioState;
import com.alpha.mongodb.sharding.example.utils.ExampleServiceClientUtils;
import io.cucumber.java.BeforeStep;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import lombok.extern.log4j.Log4j2;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.logging.log4j.ThreadContext;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

@Log4j2
public class ApplicationSteps {

@Autowired
private OkHttpClient okHttpClient;

@Autowired
private ScenarioState scenarioState;

@Before
public void beforeScenario(Scenario scenario) {
ThreadContext.put(CucumberTestConstants.SCOPE, CucumberTestConstants.CUKE);
scenarioState.setScenario(scenario);
}

@BeforeStep
public void beforeStep() {
scenarioState.addStep();
}

@When("I call the health API of the sharding example application")
public void whenThatShardingExampleApplicationIsUp() throws IOException {
HttpUrl httpUrl = ExampleServiceClientUtils.baseUrlBuilder()
.addPathSegment("actuator").addPathSegment("health").build();
System.out.println(httpUrl);
Request request = new Request.Builder().url(httpUrl).get().build();
Response healthApiResponse = okHttpClient.newCall(request).execute();

scenarioState.getCurrentStepDefinitionState().setRequest(request);
scenarioState.getCurrentStepDefinitionState().setResponse(healthApiResponse);
}

@Then("I get a response with status code {int}")
public void thenIGetResponseWithStatusCode(int status) {
assertEquals(status, scenarioState.getPreviousStepDefinitionState().getResponse().code());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alpha.mongodb.sharding.example.utils;

import com.alpha.mongodb.sharding.example.CucumberTestConstants;
import lombok.experimental.UtilityClass;
import okhttp3.HttpUrl;

@UtilityClass
public class ExampleServiceClientUtils {

public static HttpUrl.Builder baseUrlBuilder() {
return new HttpUrl.Builder()
.scheme(CucumberTestConstants.SCHEME)
.host(CucumberTestConstants.BASE_HOSTNAME)
.port(CucumberTestConstants.PORT)
.addPathSegment(CucumberTestConstants.BASE_PATH);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Verify that the sharding example application comes up

Scenario: Sharding Example comes up successfully when the application is run
When I call the health API of the sharding example application
Then I get a response with status code 200