Skip to content

Commit d2c9b19

Browse files
committed
Include a new Class LoginController and LoginControllerTest
1 parent 8a6a6e4 commit d2c9b19

File tree

4 files changed

+117
-52
lines changed

4 files changed

+117
-52
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package de.doubleslash.keeptime.view;
2+
3+
import java.util.Properties;
4+
5+
public class LoginController {
6+
7+
private String username;
8+
private String password;
9+
Properties properties = new Properties();
10+
11+
public LoginController(final String username, final String password) {
12+
this.username = username;
13+
this.password = password;
14+
}
15+
16+
public String getUsername() {
17+
username = extractValue(properties.getProperty("spring.security.user.name"));
18+
19+
return username;
20+
}
21+
22+
public String getPassword() {
23+
password = extractValue(properties.getProperty("spring.security.user.password"));
24+
return password;
25+
}
26+
27+
public void setUsername(final String username) {
28+
this.username = username;
29+
}
30+
31+
public void setPassword(final String password) {
32+
this.password = password;
33+
}
34+
35+
public void createAndSaveUser() {
36+
properties.setProperty("spring.security.user.name", "${BASIC_AUTH_USER:" + this.username + "}");
37+
properties.setProperty("spring.security.user.password", "${BASIC_AUTH_PASSWORD:" + this.password + "}");
38+
}
39+
40+
public static String extractValue(String input) {
41+
int startIndex = input.indexOf(":") + 1;
42+
int endIndex = input.lastIndexOf("}");
43+
return input.substring(startIndex, endIndex);
44+
}
45+
}

src/main/java/de/doubleslash/keeptime/view/SettingsController.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ public class SettingsController {
198198

199199
private Stage thisStage;
200200

201-
public String username;
202-
public String password;
203-
204201
@Autowired
205202
ViewController mainscreen;
206203

@@ -257,14 +254,13 @@ private void initialize() {
257254
String userName = properties.getProperty("spring.security.user.name");
258255
String userPassword = properties.getProperty("spring.security.user.password");
259256

260-
261257
if (port != null) {
262258
authPort.setText(port);
263259

264260
}
265-
if (userName != null) {
266-
authName.setText(extractValue(userName));
267-
authPassword.setText(extractValue(userPassword));
261+
if (userName!= null) {
262+
authName.setText(LoginController.extractValue(userName));
263+
authPassword.setText(LoginController.extractValue(userPassword));
268264

269265
}
270266
} else if (apistatus.equals("OFF")) {
@@ -630,10 +626,12 @@ private void handleApiOff() {
630626
}
631627

632628
private void handleApiOn() {
633-
username = authName.getText();
634-
password = authPassword.getText();
629+
String username = authName.getText();
630+
String password = authPassword.getText();
635631

636-
createAndSaveUser(username, password);
632+
LoginController loginController = new LoginController(username, password);
633+
634+
loginController.createAndSaveUser();
637635

638636
Map<String, String> propertiesToUpdate = new HashMap<>();
639637
propertiesToUpdate.put("spring.main.web-application-type", "");
@@ -649,13 +647,7 @@ private void setWebApplicationType(String value) {
649647
propertyWrite("spring.main.web-application-type", value);
650648
}
651649

652-
private void createAndSaveUser(String username, String password) {
653-
Properties properties = new Properties();
654-
properties.setProperty("spring.security.user.name", "${BASIC_AUTH_USER:" + username + "}");
655-
properties.setProperty("spring.security.user.password", "${BASIC_AUTH_PASSWORD:" + password + "}");
656-
}
657-
658-
private void propertyWrite(String key, String value) {
650+
public void propertyWrite(String key, String value) {
659651
Properties properties = new Properties();
660652
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFilePath);
661653
FileOutputStream outputStream = new FileOutputStream(propertiesFilePath)) {
@@ -687,9 +679,4 @@ private void propertyWrite(Map<String, String> propertiesToUpdate) {
687679
}
688680
}
689681

690-
public static String extractValue(String input) {
691-
int startIndex = input.indexOf(":") + 1;
692-
int endIndex = input.lastIndexOf("}");
693-
return input.substring(startIndex, endIndex);
694-
}
695682
}

src/test/java/de/doubleslash/keeptime/REST_API_Test/ApiTest.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,39 @@
1616

1717
package de.doubleslash.keeptime.REST_API_Test;
1818

19-
import static org.junit.jupiter.api.Assertions.*;
20-
import static org.mockito.Mockito.mock;
21-
import static org.mockito.Mockito.when;
22-
23-
import java.io.InputStream;
2419
import java.util.Properties;
2520

2621
import de.doubleslash.keeptime.view.SettingsController;
2722
import org.junit.jupiter.api.Test;
2823

24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
2926
public class ApiTest {
27+
SettingsController settingsController;
3028

3129
@Test
3230
public void testSaveUserAndAuthorities() {
3331
String username = "user";
3432
String password = "1234";
35-
SettingsController settingsController;
36-
3733

3834
Properties properties = new Properties();
39-
properties.put("spring.security.user.name", "${BASIC_AUTH_USER:" + username+ "}");
35+
properties.put("spring.security.user.name", "${BASIC_AUTH_USER:" + username + "}");
4036
properties.setProperty("spring.security.user.password", "${BASIC_AUTH_PASSWORD:" + password + "}");
4137

42-
assertEquals(properties.getProperty("spring.security.user.password"),"${BASIC_AUTH_PASSWORD:1234}");
43-
assertEquals(properties.getProperty("spring.security.user.name"),"${BASIC_AUTH_USER:user}");
44-
}
45-
}
38+
assertEquals(properties.getProperty("spring.security.user.password"), "${BASIC_AUTH_PASSWORD:1234}");
39+
assertEquals(properties.getProperty("spring.security.user.name"), "${BASIC_AUTH_USER:user}");
40+
}}
4641

47-
//@Test public void testIsPropertyPresent() throws Exception {
48-
// // Mock InputStream und Properties
49-
// InputStream mockInputStream = mock(InputStream.class);
50-
// Properties mockProperties = mock(Properties.class);
51-
// when(mockProperties.containsKey("api")).thenReturn(true);
52-
// when(mockProperties.containsKey("non_existing_property")).thenReturn(false);
53-
//
54-
// // Mock SettingsController und setzen Sie den InputStream
55-
// SettingsController settingsController = new SettingsController();
56-
// SettingsController spySettingsController = org.mockito.Mockito.spy(settingsController);
57-
// org.mockito.Mockito.doReturn(mockInputStream).when(spySettingsController).getClass().getClassLoader().getResourceAsStream("application.properties");
58-
// org.mockito.Mockito.doReturn(mockProperties).when(spySettingsController).loadProperties(mockInputStream);
59-
//
60-
// // Testen, ob die Eigenschaft vorhanden ist
61-
// assertTrue(spySettingsController.isPropertyPresent("api"));
62-
// assertFalse(spySettingsController.isPropertyPresent("non_existing_property"));
63-
//}
64-
//}
42+
43+
44+
// @Test
45+
// public void testHandleApiOn() {
46+
// settingsController.authName.setText("testUser");
47+
// settingsController.authPassword.setText("testPassword");
48+
// settingsController.authPort.setText("8080");
49+
//
50+
// settingsController.handleApiOn();
51+
//
52+
//
53+
// }
6554

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.doubleslash.keeptime.view;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Properties;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class LoginControllerTest {
10+
LoginController loginController;
11+
12+
@Test
13+
public void testExtractValue() {
14+
String inputUser = "${BASIC_AUTH_USER:user}";
15+
String inputPassword = "${BASIC_AUTH_PASSWORD:123}";
16+
String expectedUser = "user";
17+
String expectedPassword = "123";
18+
String resultUser = LoginController.extractValue(inputUser);
19+
String resultPassword = LoginController.extractValue(inputPassword);
20+
assertEquals(expectedUser, resultUser);
21+
assertEquals(expectedPassword,resultPassword);
22+
}
23+
24+
@Test
25+
public void testExtractValuePassword() {
26+
String inputPassword = "${BASIC_AUTH_PASSWORD:123}";
27+
String expectedPassword = "123";
28+
String resultPassword = LoginController.extractValue(inputPassword);
29+
assertEquals(expectedPassword,resultPassword);
30+
}
31+
32+
@Test
33+
public void testCreateAndSaveUser() {
34+
String username = "testUser";
35+
String password = "testPassword";
36+
LoginController loginController = new LoginController(username, password);
37+
38+
loginController.createAndSaveUser();
39+
Properties properties = loginController.properties;
40+
assertEquals("${BASIC_AUTH_USER:testUser}", properties.getProperty("spring.security.user.name"));
41+
assertEquals("${BASIC_AUTH_PASSWORD:testPassword}", properties.getProperty("spring.security.user.password"));
42+
}
43+
}
44+

0 commit comments

Comments
 (0)