format all files

This commit is contained in:
Peter Straßer
2021-04-19 20:03:32 +02:00
parent 1d37669463
commit 3ff3a0cde4
57 changed files with 704 additions and 650 deletions

View File

@@ -21,98 +21,99 @@ import java.util.Properties;
public class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
private static final String WRAPPER_VERSION = "0.5.6";
/** Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. */
private static final String DEFAULT_DOWNLOAD_URL =
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION
+ "/maven-wrapper-"
+ WRAPPER_VERSION
+ ".jar";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to use
* instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/** Path where the maven-wrapper.jar will be saved to. */
private static final String MAVEN_WRAPPER_JAR_PATH = ".mvn/wrapper/maven-wrapper.jar";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if (mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if (mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if (mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
if (mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '"
+ outputFile.getParentFile().getAbsolutePath()
+ "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}

8
application/mvnw vendored
View File

@@ -316,7 +316,7 @@ export MAVEN_CMD_LINE_ARGS
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
$MAVEN_OPTS \
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"

View File

@@ -1,7 +1,7 @@
package de.strasser.peter.hexagonal.application.customer.exception;
public class AddressTypeDoesNotExistsExc extends BusinessException {
public AddressTypeDoesNotExistsExc(String type) {
super(String.format("Adress of type '%s' does not exist!", type));
}
public AddressTypeDoesNotExistsExc(String type) {
super(String.format("Adress of type '%s' does not exist!", type));
}
}

View File

@@ -1,7 +1,7 @@
package de.strasser.peter.hexagonal.application.customer.exception;
public abstract class BusinessException extends IllegalStateException {
public BusinessException(String error) {
super(error);
}
public BusinessException(String error) {
super(error);
}
}

View File

@@ -2,7 +2,7 @@ package de.strasser.peter.hexagonal.application.customer.exception;
public class DefaultAdressRequiredToActivateExc extends BusinessException {
public DefaultAdressRequiredToActivateExc() {
super("Customer needs to have at least a default adress to be able to be activated!");
}
public DefaultAdressRequiredToActivateExc() {
super("Customer needs to have at least a default adress to be able to be activated!");
}
}

View File

@@ -3,7 +3,7 @@ package de.strasser.peter.hexagonal.application.customer.exception;
import java.text.MessageFormat;
public class TooYoungExc extends BusinessException {
public TooYoungExc(int age) {
super(MessageFormat.format("Customer is too young. Expected: > 18 yrs, Actual: {0}", age));
}
public TooYoungExc(int age) {
super(MessageFormat.format("Customer is too young. Expected: > 18 yrs, Actual: {0}", age));
}
}

View File

@@ -6,5 +6,5 @@ import org.mapstruct.Mapper;
@Mapper
public interface AddAddressMapper {
ValidateAddressCommand toOutCmd(AddAddressCommand addAddressCommand);
ValidateAddressCommand toOutCmd(AddAddressCommand addAddressCommand);
}

View File

@@ -9,5 +9,6 @@ import java.math.BigInteger;
import java.util.List;
public interface AddAddressUseCase {
void addAddresses(@Min(0) BigInteger customerId, @Valid @NotEmpty List<AddAddressCommand> addresses);
void addAddresses(
@Min(0) BigInteger customerId, @Valid @NotEmpty List<AddAddressCommand> addresses);
}

View File

@@ -5,5 +5,5 @@ import de.strasser.peter.hexagonal.application.customer.port.in.commands.Registe
import javax.validation.Valid;
public interface RegisterCustomerUseCase {
void register(@Valid RegisterCustomerCommand registerCustomerCommand);
void register(@Valid RegisterCustomerCommand registerCustomerCommand);
}

View File

@@ -11,10 +11,12 @@ import javax.validation.constraints.NotEmpty;
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class AddAddressCommand {
@NotEmpty String type;
@NotEmpty String street;
Integer houseNumber;
@Min(0)
Integer zipCode;
@NotEmpty String country;
@NotEmpty String type;
@NotEmpty String street;
Integer houseNumber;
@Min(0)
Integer zipCode;
@NotEmpty String country;
}

View File

@@ -1,6 +1,5 @@
package de.strasser.peter.hexagonal.application.customer.port.in.commands;
import de.strasser.peter.hexagonal.application.customer.validator.SecurePassword;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -14,7 +13,7 @@ import java.time.LocalDate;
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class RegisterCustomerCommand {
@NotEmpty String name;
@Past LocalDate birthDay;
@SecurePassword String clearPassword;
@NotEmpty String name;
@Past LocalDate birthDay;
@SecurePassword String clearPassword;
}

View File

@@ -4,5 +4,5 @@ import de.strasser.peter.hexagonal.application.customer.domain.Address;
import de.strasser.peter.hexagonal.application.customer.port.out.commands.ValidateAddressCommand;
public interface AddressValidatorPort {
Address validate(ValidateAddressCommand validateAddressCommand);
Address validate(ValidateAddressCommand validateAddressCommand);
}

View File

@@ -5,5 +5,5 @@ import de.strasser.peter.hexagonal.application.customer.domain.Customer;
import java.math.BigInteger;
public interface LoadCustomerPort {
Customer findById(BigInteger id);
Customer findById(BigInteger id);
}

View File

@@ -3,5 +3,5 @@ package de.strasser.peter.hexagonal.application.customer.port.out;
import de.strasser.peter.hexagonal.application.customer.domain.Customer;
public interface SaveCustomerPort {
void upsert(Customer customer);
void upsert(Customer customer);
}

View File

@@ -22,28 +22,30 @@ import java.util.Map;
@Service
@RequiredArgsConstructor
class AddressService implements AddAddressUseCase {
private final SaveCustomerPort saveCustomerAdapter;
private final AddressValidatorPort addressValidatorAdapter;
private final LoadCustomerPort loadCustomerAdapter;
private final AddAddressMapper addAddressMapper;
private final SaveCustomerPort saveCustomerAdapter;
private final AddressValidatorPort addressValidatorAdapter;
private final LoadCustomerPort loadCustomerAdapter;
private final AddAddressMapper addAddressMapper;
@Override
public void addAddresses(@Min(0) BigInteger customerId, @Valid @NotEmpty List<AddAddressCommand> addAddressCmds) {
final Customer customer = loadCustomerAdapter.findById(customerId);
@Override
public void addAddresses(
@Min(0) BigInteger customerId, @Valid @NotEmpty List<AddAddressCommand> addAddressCmds) {
final Customer customer = loadCustomerAdapter.findById(customerId);
final Map<Address.AddressType, Address> addresses = new HashMap<>();
addAddressCmds.forEach(addAddressCommand -> validateAndAddToMap(addresses, addAddressCommand));
final Map<Address.AddressType, Address> addresses = new HashMap<>();
addAddressCmds.forEach(addAddressCommand -> validateAndAddToMap(addresses, addAddressCommand));
customer.addAddresses(addresses);
customer.addAddresses(addresses);
saveCustomerAdapter.upsert(customer);
}
saveCustomerAdapter.upsert(customer);
}
private void validateAndAddToMap(Map<Address.AddressType, Address> addresses, AddAddressCommand addAddressCommand) {
final var addressType = Address.AddressType.fromString(addAddressCommand.getType());
final var validateAddressCmd = addAddressMapper.toOutCmd(addAddressCommand);
final var validatedAddress = addressValidatorAdapter.validate(validateAddressCmd);
private void validateAndAddToMap(
Map<Address.AddressType, Address> addresses, AddAddressCommand addAddressCommand) {
final var addressType = Address.AddressType.fromString(addAddressCommand.getType());
final var validateAddressCmd = addAddressMapper.toOutCmd(addAddressCommand);
final var validatedAddress = addressValidatorAdapter.validate(validateAddressCmd);
addresses.put(addressType, validatedAddress);
}
addresses.put(addressType, validatedAddress);
}
}

View File

@@ -16,17 +16,18 @@ import javax.validation.Valid;
@Validated
@RequiredArgsConstructor
class RegisterCustomerService implements RegisterCustomerUseCase {
private final SaveCustomerPort saveUser;
private final SaveCustomerPort saveUser;
@Override
public void register(@Valid RegisterCustomerCommand registerCmd) {
var encryptedPw = this.superSecureHashingAlgorithm(registerCmd.getClearPassword());
var newCustomer = Customer.newCustomer(registerCmd.getName(), encryptedPw, registerCmd.getBirthDay());
@Override
public void register(@Valid RegisterCustomerCommand registerCmd) {
var encryptedPw = this.superSecureHashingAlgorithm(registerCmd.getClearPassword());
var newCustomer =
Customer.newCustomer(registerCmd.getName(), encryptedPw, registerCmd.getBirthDay());
saveUser.upsert(newCustomer);
}
saveUser.upsert(newCustomer);
}
private String superSecureHashingAlgorithm(String s) {
return new StringBuilder(s).reverse().toString();
}
private String superSecureHashingAlgorithm(String s) {
return new StringBuilder(s).reverse().toString();
}
}

View File

@@ -2,9 +2,7 @@ package de.strasser.peter.hexagonal.application.customer.domain;
import de.strasser.peter.hexagonal.application.customer.exception.TooOldToDeactivateExc;
import de.strasser.peter.hexagonal.application.customer.exception.TooYoungExc;
import de.strasser.peter.hexagonal.application.customer.mapper.AddAddressMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import java.time.LocalDate;
import java.util.Collections;
@@ -13,7 +11,6 @@ import static org.junit.jupiter.api.Assertions.*;
class CustomerTest {
@Test
public void should_CreateNewCustomer() {
final var customer = Customer.newCustomer("name", "pw", LocalDate.of(1980, 1, 1));
@@ -57,10 +54,8 @@ class CustomerTest {
}
@Test
public void should_ThrowTooOldErr_When_DeactivatingOldCustomer(){
public void should_ThrowTooOldErr_When_DeactivatingOldCustomer() {
final Customer customer = Customer.newCustomer("name", "pw", LocalDate.of(1950, 1, 1));
assertThrows(TooOldToDeactivateExc.class, customer::deactivate);
}
}

View File

@@ -2,7 +2,6 @@ package de.strasser.peter.hexagonal.application.customer.service;
import de.strasser.peter.hexagonal.application.customer.domain.Address;
import de.strasser.peter.hexagonal.application.customer.domain.Customer;
import de.strasser.peter.hexagonal.application.customer.mapper.AddAddressMapper;
import de.strasser.peter.hexagonal.application.customer.port.in.commands.AddAddressCommand;
import de.strasser.peter.hexagonal.application.customer.port.out.AddressValidatorPort;
import de.strasser.peter.hexagonal.application.customer.port.out.LoadCustomerPort;
@@ -12,7 +11,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import java.math.BigInteger;
import java.time.LocalDate;

View File

@@ -1,6 +1,5 @@
package de.strasser.peter.hexagonal.application.customer.service;
import de.strasser.peter.hexagonal.application.customer.mapper.AddAddressMapper;
import de.strasser.peter.hexagonal.application.customer.port.in.commands.RegisterCustomerCommand;
import de.strasser.peter.hexagonal.application.customer.port.out.AddressValidatorPort;
import de.strasser.peter.hexagonal.application.customer.port.out.LoadCustomerPort;
@@ -21,48 +20,47 @@ import static org.mockito.BDDMockito.willDoNothing;
@SpringBootTest
class RegisterCustomerServiceTest {
@Autowired
private RegisterCustomerService sut;
@Autowired private RegisterCustomerService sut;
@MockBean
private SaveCustomerPort saveCustomerAdapterMock;
@MockBean
private AddressValidatorPort addressValidatorAdapterMock;
@MockBean
private LoadCustomerPort loadCustomerAdapterMock;
@MockBean private SaveCustomerPort saveCustomerAdapterMock;
@MockBean private AddressValidatorPort addressValidatorAdapterMock;
@MockBean private LoadCustomerPort loadCustomerAdapterMock;
@BeforeEach
public void setUp() {
willDoNothing().given(saveCustomerAdapterMock).upsert(any());
}
@BeforeEach
public void setUp() {
willDoNothing().given(saveCustomerAdapterMock).upsert(any());
}
@Test
public void should_ThrowInvalidBirthDate_When_PassingDateInFuture() {
RegisterCustomerCommand registerCustomerCommand =
new RegisterCustomerCommand("Hans", LocalDate.now().plusDays(1), "secretPassword");
@Test
public void should_ThrowInvalidBirthDate_When_PassingDateInFuture() {
RegisterCustomerCommand registerCustomerCommand =
new RegisterCustomerCommand("Hans", LocalDate.now().plusDays(1), "secretPassword");
assertThrows(ConstraintViolationException.class, () -> {
sut.register(registerCustomerCommand);
assertThrows(
ConstraintViolationException.class,
() -> {
sut.register(registerCustomerCommand);
});
}
}
@Test
public void should_ThrowInvalidPassword_When_PassingInsecurePassword() {
LocalDate birthday = LocalDate.of(1980, 1, 1);
RegisterCustomerCommand registerCustomerCommand =
new RegisterCustomerCommand("Hans", birthday, "secretPassword");
@Test
public void should_ThrowInvalidPassword_When_PassingInsecurePassword() {
LocalDate birthday = LocalDate.of(1980, 1, 1);
RegisterCustomerCommand registerCustomerCommand =
new RegisterCustomerCommand("Hans", birthday, "secretPassword");
assertThrows(ConstraintViolationException.class, () -> {
sut.register(registerCustomerCommand);
assertThrows(
ConstraintViolationException.class,
() -> {
sut.register(registerCustomerCommand);
});
}
}
@Test
public void should_SaveUser() {
LocalDate birthday = LocalDate.of(1980, 1, 1);
RegisterCustomerCommand registerCustomerCommand =
new RegisterCustomerCommand("Hans", birthday, "Ha012!321+dw");
sut.register(registerCustomerCommand);
}
@Test
public void should_SaveUser() {
LocalDate birthday = LocalDate.of(1980, 1, 1);
RegisterCustomerCommand registerCustomerCommand =
new RegisterCustomerCommand("Hans", birthday, "Ha012!321+dw");
sut.register(registerCustomerCommand);
}
}