mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
parent
a795fc99aa
commit
391ddaf03b
|
@ -1,6 +1,7 @@
|
||||||
package catering.users;
|
package catering.users;
|
||||||
|
|
||||||
import org.salespointframework.core.DataInitializer;
|
import org.salespointframework.core.DataInitializer;
|
||||||
|
import org.salespointframework.useraccount.UserAccountManagement;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
@ -13,15 +14,19 @@ class UserDataInitializer implements DataInitializer {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(UserDataInitializer.class);
|
private static final Logger LOG = LoggerFactory.getLogger(UserDataInitializer.class);
|
||||||
private final UserManagement userManagement;
|
private final UserManagement userManagement;
|
||||||
|
private final UserAccountManagement userAccountManagement;
|
||||||
|
|
||||||
UserDataInitializer(UserManagement userManagement) {
|
UserDataInitializer(UserManagement userManagement, UserAccountManagement userAccountManagement) {
|
||||||
Assert.notNull(userManagement, "UserRepository must not be null!");
|
Assert.notNull(userManagement, "UserRepository must not be null!");
|
||||||
this.userManagement = userManagement;
|
this.userManagement = userManagement;
|
||||||
|
this.userAccountManagement = userAccountManagement;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
if (userAccountManagement.findByUsername("admin").isEmpty()) {
|
||||||
LOG.info("Creating default user.");
|
LOG.info("Creating default user.");
|
||||||
userManagement.createAdmin("admin", "admin", "admin");
|
userManagement.createAdmin("admin", "admin", "admin");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package catering.users;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.salespointframework.useraccount.UserAccount.UserAccountIdentifier;
|
||||||
|
import org.salespointframework.useraccount.UserAccountManagement;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class UserDataInitializerIntegrationTests {
|
||||||
|
@Autowired
|
||||||
|
UserManagement userManagement;
|
||||||
|
@Autowired
|
||||||
|
UserAccountManagement userAccountManagement;
|
||||||
|
|
||||||
|
UserDataInitializer userDataInitializer;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void init() {
|
||||||
|
userDataInitializer = new UserDataInitializer(userManagement, userAccountManagement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the administrator’s account is only created once.
|
||||||
|
*
|
||||||
|
* If the admin is either replaced or attempted to be created a second time,
|
||||||
|
* the test will fail or throw an {@link IllegalArgumentException} respectively.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void onlyCreateAdminOnce() throws Exception {
|
||||||
|
UserAccountIdentifier idBefore = userAccountManagement.findByUsername("admin").get().getId();
|
||||||
|
userDataInitializer.initialize();
|
||||||
|
UserAccountIdentifier idAfter = userAccountManagement.findByUsername("admin").get().getId();
|
||||||
|
|
||||||
|
assertThat(idAfter).isEqualTo(idBefore);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue