Skip to content

Commit

Permalink
Merge pull request #3 from oktadeveloper/bdemers-add-tests
Browse files Browse the repository at this point in the history
Adding Spring Boot API UTs and ITs
  • Loading branch information
Matt Raible committed May 2, 2018
2 parents 9076ee1 + 1f01007 commit d0e8419
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 20 deletions.
98 changes: 98 additions & 0 deletions holdings-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.17.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.spotify</groupId>
<artifactId>hamcrest-pojo</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.spotify</groupId>
<artifactId>hamcrest-jackson</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand All @@ -64,6 +93,75 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<additionalClasspathElements>
<!-- see: https://github.com/spring-projects/spring-boot/issues/6254#issuecomment-281404852 -->
<additionalClasspathElement>${basedir}/target/classes</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<id>prepare-agent</id>
<phase>process-test-classes</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-agent-integration</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
<goal>report-integration</goal>
</goals>
</execution>
<execution>
<id>jacoco-merge-report</id>
<phase>verify</phase>
<goals>
<goal>merge</goal>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-merge.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-merge</outputDirectory>
<destFile>${project.build.directory}/jacoco-merge.exec</destFile>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>*.exec</include>
</includes>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ public String getCrypto() {
return crypto;
}

public void setCrypto(String crypto) {
public Holding setCrypto(String crypto) {
this.crypto = crypto;
return this;
}

public String getCurrency() {
return currency;
}

public void setCurrency(String currency) {
public Holding setCurrency(String currency) {
this.currency = currency;
return this;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
public Holding setAmount(String amount) {
this.amount = amount;
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class HoldingsController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final ObjectMapper mapper = new ObjectMapper();
private final Client client;
private final String HOLDINGS_ATTRIBUTE_NAME = "holdings";
private static final String HOLDINGS_ATTRIBUTE_NAME = "holdings";

public HoldingsController(Client client) {
this.client = client;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.okta.developer.holdingsapi;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.okta.sdk.client.Client;
import com.okta.sdk.resource.user.User;
import com.okta.sdk.resource.user.UserProfile;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.io.IOException;
import java.security.Principal;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.eq;

import static com.spotify.hamcrest.pojo.IsPojo.pojo;
import static com.spotify.hamcrest.jackson.JsonMatchers.jsonText;
import static com.spotify.hamcrest.jackson.JsonMatchers.jsonObject;
import static com.spotify.hamcrest.jackson.IsJsonArray.jsonArray;


public class HoldingsControllerTest {

@Test
public void getHoldingsTest() {

String username = "joe.coder@example.com";

// define mocks
Client client = mock(Client.class);
User user = mock(User.class);
UserProfile userProfile = mock(UserProfile.class);
Principal principal = mock(Principal.class);

// stub behaviour
when(principal.getName()).thenReturn(username);
when(client.getUser(username)).thenReturn(user);
when(user.getProfile()).thenReturn(userProfile);
when(userProfile.get("holdings")).thenReturn(
"[" +
"{\"crypto\": \"crypto-value1\", \"currency\": \"currency-value1\", \"amount\": \"amount-value1\"}," +
"{\"crypto\": \"crypto-value2\", \"currency\": \"currency-value2\", \"amount\": \"amount-value2\"}" +
"]");

// test the controller
HoldingsController holdingsController = new HoldingsController(client);
List<Holding> holdings = holdingsController.getHoldings(principal);

assertThat(holdings, contains(
pojo(Holding.class)
.withProperty("crypto", is("crypto-value1"))
.withProperty("currency", is("currency-value1"))
.withProperty("amount", is("amount-value1")),
pojo(Holding.class)
.withProperty("crypto", is("crypto-value2"))
.withProperty("currency", is("currency-value2"))
.withProperty("amount", is("amount-value2"))
));
}

@Test
public void saveHoldingsTest() throws IOException {

// define mocks
Client client = mock(Client.class);
User user = mock(User.class);
UserProfile userProfile = mock(UserProfile.class);
Principal principal = mock(Principal.class);

// define behaviour
String username = "joe.coder@example.com";
when(principal.getName()).thenReturn(username);
when(client.getUser(username)).thenReturn(user);
when(user.getProfile()).thenReturn(userProfile);

// test the controller
Holding[] inputHoldings = new Holding[] {
new Holding()
.setCrypto("crypto1")
.setCurrency("currency1")
.setAmount("amount1"),
new Holding()
.setCrypto("crypto2")
.setCurrency("currency2")
.setAmount("amount2")
};

HoldingsController holdingsController = new HoldingsController(client);
Holding[] outputHoldings = holdingsController.saveHoldings(inputHoldings, principal);

//////////////////////
// Validate Results //
//////////////////////

// make sure the input array is the same as the output array
assertThat(outputHoldings, is(inputHoldings));

// verify mock interactions
ArgumentCaptor<String> holdingsJsonCaptor = ArgumentCaptor.forClass(String.class);
verify(userProfile).put(eq("holdings"), holdingsJsonCaptor.capture());
verify(user).update();

// validate the json
JsonNode holdingsParsed = new ObjectMapper().readTree(holdingsJsonCaptor.getValue());
assertThat(holdingsParsed, jsonArray(contains(
jsonObject()
.where("crypto", jsonText("crypto1"))
.where("currency", jsonText("currency1"))
.where("amount", jsonText("amount1")),
jsonObject()
.where("crypto", jsonText("crypto2"))
.where("currency", jsonText("currency2"))
.where("amount", jsonText("amount2"))
)));
}
}

0 comments on commit d0e8419

Please sign in to comment.