-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
160 additions
and
14 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ain/java/org/example/DemoApplication.java → ...java/org/example/mvc/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../src/main/java/org/example/Generated.java → .../main/java/org/example/mvc/Generated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...main/java/org/example/HomeController.java → .../java/org/example/mvc/HomeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...n/java/org/example/ProfileController.java → ...va/org/example/mvc/ProfileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...main/java/org/example/SecurityConfig.java → .../java/org/example/mvc/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...java/org/example/DemoApplicationTest.java → .../org/example/mvc/DemoApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.example; | ||
package org.example.mvc; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
.../java/org/example/HomeControllerTest.java → ...a/org/example/mvc/HomeControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...va/org/example/ProfileControllerTest.java → ...rg/example/mvc/ProfileControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...test/java/org/example/SecurityConfig.java → .../java/org/example/mvc/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ain/java/org/example/DemoApplication.java → .../org/example/webflux/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...main/java/org/example/HomeController.java → ...a/org/example/webflux/HomeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...main/java/org/example/SecurityConfig.java → ...a/org/example/webflux/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
webflux-login/src/test/java/org/example/webflux/DemoApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.example.webflux; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class DemoApplicationTest { | ||
|
||
@Test | ||
public void main() { | ||
// Act | ||
DemoApplication.main(new String[] {}); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
webflux-login/src/test/java/org/example/webflux/HomeControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.example.webflux; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.ui.Model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.*; | ||
|
||
@SpringBootTest | ||
class HomeControllerTest { | ||
|
||
private HomeController homeController; | ||
|
||
@Mock | ||
private Model model; | ||
|
||
@Mock | ||
private OidcUser principal; | ||
|
||
@BeforeEach | ||
public void beforeEach(){ | ||
homeController = new HomeController(); | ||
} | ||
|
||
@Test | ||
public void testHomeModel(){ | ||
// Act | ||
String returnValue = homeController.home(model, principal); | ||
|
||
// Assert | ||
verify(model, times(1)).addAttribute(eq("profile"), Mockito.any()); | ||
assertEquals("index", returnValue); | ||
} | ||
|
||
@Test | ||
public void testHomeModelNullPrincipal(){ | ||
// Act | ||
String returnValue = homeController.home(model, null); | ||
|
||
// Assert | ||
verify(model, never()).addAttribute(eq("profile"), Mockito.any()); | ||
assertEquals("index", returnValue); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
webflux-login/src/test/java/org/example/webflux/ProfileControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.example.webflux; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.slf4j.Logger; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.ui.Model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.*; | ||
|
||
@SpringBootTest | ||
public class ProfileControllerTest { | ||
|
||
private ProfileController profileController; | ||
|
||
@Mock | ||
private Model model; | ||
|
||
@Mock | ||
private OidcUser oidcUser; | ||
|
||
@BeforeEach | ||
public void beforeEach(){ | ||
profileController = new ProfileController(); | ||
} | ||
|
||
@Test | ||
public void testProfileModel() { | ||
// Act | ||
String returnValue = profileController.profile(model, oidcUser); | ||
|
||
// Assert | ||
verify(model, times(1)).addAttribute(eq("profile"), Mockito.any()); | ||
verify(model, times(1)).addAttribute(eq("profileJson"), Mockito.any()); | ||
assertEquals("profile", returnValue); | ||
} | ||
|
||
@Test | ||
public void testProfileModel_Exception() throws JsonProcessingException { | ||
// Prepare | ||
// Mock Logger | ||
Logger mockedLogger = Mockito.mock(Logger.class); | ||
ReflectionTestUtils.setField(profileController, "log", mockedLogger); | ||
|
||
// Mocked ObjectWriter calls writeValueAsString() and throws JsonProcessingException | ||
ObjectWriter mockedWriter = Mockito.mock(ObjectWriter.class); | ||
when(mockedWriter.writeValueAsString(Mockito.any())).thenThrow(new JsonProcessingException("Error"){}); | ||
|
||
// Mocked ObjectMapper returns mocked ObjectWriter | ||
ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class); | ||
when(mockedMapper.writerWithDefaultPrettyPrinter()).thenReturn(mockedWriter); | ||
|
||
// Set mocked ObjectMapper on class through reflection | ||
ReflectionTestUtils.setField(profileController, "mapper", mockedMapper); | ||
|
||
// Act | ||
String returnValue = profileController.profile(model, oidcUser); | ||
|
||
// Assert | ||
verify(model, times(1)).addAttribute(eq("profile"), Mockito.any()); | ||
verify(model, times(1)).addAttribute(eq("profileJson"), Mockito.any()); | ||
assertEquals("profile", returnValue); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
webflux-login/src/test/java/org/example/webflux/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.example.webflux; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Profile("test") | ||
@Configuration | ||
class SecurityConfig {} |