Skip to content

Commit 8a6a6e4

Browse files
committed
#165: Code Refactoring
1 parent 07df243 commit 8a6a6e4

File tree

2 files changed

+56
-44
lines changed

2 files changed

+56
-44
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ public class SettingsController {
198198

199199
private Stage thisStage;
200200

201-
private String username;
202-
private String password;
201+
public String username;
202+
public String password;
203203

204204
@Autowired
205205
ViewController mainscreen;
@@ -254,11 +254,18 @@ private void initialize() {
254254
radioApiOn.setSelected(true);
255255
radioApiOff.setSelected(false);
256256
String port = properties.getProperty("server.port");
257+
String userName = properties.getProperty("spring.security.user.name");
258+
String userPassword = properties.getProperty("spring.security.user.password");
259+
260+
257261
if (port != null) {
258262
authPort.setText(port);
263+
259264
}
260-
if (username != null) {
261-
authName.setText(username);
265+
if (userName != null) {
266+
authName.setText(extractValue(userName));
267+
authPassword.setText(extractValue(userPassword));
268+
262269
}
263270
} else if (apistatus.equals("OFF")) {
264271
radioApiOn.setSelected(false);
@@ -679,4 +686,10 @@ private void propertyWrite(Map<String, String> propertiesToUpdate) {
679686
e.printStackTrace();
680687
}
681688
}
689+
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+
}
682695
}

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

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,52 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
1817
package de.doubleslash.keeptime.REST_API_Test;
1918

20-
//import de.doubleslash.keeptime.model.Authorities;
21-
//import de.doubleslash.keeptime.model.User;
22-
//import de.doubleslash.keeptime.model.repos.AuthoritiesRepository;
23-
//import de.doubleslash.keeptime.model.repos.UserRepository;
24-
import org.junit.jupiter.api.Test;
25-
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
19+
import static org.junit.jupiter.api.Assertions.*;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.when;
2722

28-
import static org.junit.jupiter.api.Assertions.assertEquals;
29-
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import java.io.InputStream;
24+
import java.util.Properties;
25+
26+
import de.doubleslash.keeptime.view.SettingsController;
27+
import org.junit.jupiter.api.Test;
3028

31-
@DataJpaTest
3229
public class ApiTest {
3330

34-
// @Autowired
35-
// private UserRepository userRepository;
36-
//
37-
// @Autowired
38-
// private AuthoritiesRepository authoritiesRepository;
31+
@Test
32+
public void testSaveUserAndAuthorities() {
33+
String username = "user";
34+
String password = "1234";
35+
SettingsController settingsController;
3936

40-
// @Test
41-
// public void testSaveUserAndAuthorities() {
42-
// String username = "user";
43-
// String password = "1234";
44-
//
45-
// User user = new User();
46-
// Authorities authorities = new Authorities();
47-
// user.setUserName(username);
48-
// user.setPassword("{noop}" + password);
49-
// user.setEnabled(true);
50-
// authorities.setUserName(username);
51-
// authorities.setAuthority("ROLE_USER");
52-
//
53-
// userRepository.save(user);
54-
// authoritiesRepository.save(authorities);
55-
//
56-
// User savedUser = userRepository.findByUserName(username);
57-
// assertNotNull(savedUser);
58-
// assertEquals(username, savedUser.getUserName());
59-
//
60-
// Authorities savedAuthorities = authoritiesRepository.findByUserName(username);
61-
// assertNotNull(savedAuthorities);
62-
// assertEquals("ROLE_USER", savedAuthorities.getAuthority());
63-
// }
37+
38+
Properties properties = new Properties();
39+
properties.put("spring.security.user.name", "${BASIC_AUTH_USER:" + username+ "}");
40+
properties.setProperty("spring.security.user.password", "${BASIC_AUTH_PASSWORD:" + password + "}");
41+
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+
}
6445
}
6546

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+
//}
6665

0 commit comments

Comments
 (0)