Skip to content

Commit

Permalink
first draft spring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Waldemar Siebert committed May 1, 2024
1 parent 1674900 commit 82e7fe8
Show file tree
Hide file tree
Showing 7 changed files with 709 additions and 0 deletions.
8 changes: 8 additions & 0 deletions BankingAppServer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
</dependencyManagement>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.wiremock/wiremock -->
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.5.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.simplytest</groupId>
<artifactId>core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import java.util.HashMap;

import jakarta.validation.ValidationException;
import org.iban4j.IbanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -16,6 +19,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.server.ResponseStatusException;

import com.simplytest.core.Error;
Expand Down Expand Up @@ -172,6 +176,60 @@ public Result<Boolean, Error> sendMoney(
}
}

@Value("${validationurl}")
String validationurl;

@ResponseBody
@PostMapping(path = "{accountId}/sendexternal")
public Result<Boolean, Error> sendMoneyExternal(
@RequestHeader(name = HttpHeaders.AUTHORIZATION) String token,
@PathVariable @Valid long accountId, @RequestBody SendMoney data,
HttpServletResponse response)
{
var parsedToken = JWT.getId(token);

if (parsedToken.isEmpty())
{
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return Result.error(Error.BadCredentials);
}

var id = new Id(parsedToken.get(), accountId);

try
{
String validationUrl = this.validationurl + "/validator/validate?iban=" + data.target().raw();
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> validationResponse = restTemplate.getForEntity(validationUrl, String.class);

if (!validationResponse.getStatusCode().is2xxSuccessful()) {
throw new ValidationException("Fehler beim Validieren der IBAN");
}

} catch (Exception e)
{
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return Result.error(Error.BadIban);


}

try (var updatable = getAccount(id))
{
var account = updatable.value();
var result = account.sendMoney(data.amount(), data.target().value());

if (!result.successful())
{
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return Result.error(result.error());
}

return Result.success();
}
}


@ResponseBody
@PostMapping(path = "{accountId}/transfer")
public Result<Boolean, Error> transferMoney(
Expand Down
1 change: 1 addition & 0 deletions BankingAppServer/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
validationurl=https://simplytest.de
Loading

0 comments on commit 82e7fe8

Please sign in to comment.