Skip to content

Commit 5932d08

Browse files
committed
fix and add tests
1 parent 9e35295 commit 5932d08

File tree

3 files changed

+260
-153
lines changed

3 files changed

+260
-153
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package es.in2.vcverifier.config;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
5+
import org.springframework.mock.web.MockHttpServletRequest;
6+
import org.springframework.web.servlet.LocaleResolver;
7+
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
8+
9+
import java.util.List;
10+
import java.util.Locale;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
import static org.mockito.Mockito.*;
14+
15+
class I18nConfigTest {
16+
17+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
18+
.withUserConfiguration(I18nConfig.class)
19+
.withBean(FrontendConfig.class, () -> {
20+
FrontendConfig fc = mock(FrontendConfig.class);
21+
when(fc.getDefaultLang()).thenReturn("ca");
22+
return fc;
23+
});
24+
25+
@Test
26+
void localeResolver_shouldBeAcceptHeaderLocaleResolver_withConfiguredSupportedLocales_andDefaultViaResolution() {
27+
contextRunner.run(ctx -> {
28+
assertTrue(ctx.containsBean("localeResolver"));
29+
30+
LocaleResolver resolver = ctx.getBean(LocaleResolver.class);
31+
assertNotNull(resolver);
32+
assertInstanceOf(AcceptHeaderLocaleResolver.class, resolver);
33+
34+
AcceptHeaderLocaleResolver ahlr = (AcceptHeaderLocaleResolver) resolver;
35+
36+
// Supported locales must be exactly [en, es, ca] in order
37+
List<Locale> expectedSupported = List.of(
38+
Locale.forLanguageTag("en"),
39+
Locale.forLanguageTag("es"),
40+
Locale.forLanguageTag("ca")
41+
);
42+
assertEquals(expectedSupported, ahlr.getSupportedLocales());
43+
44+
// Default locale: resolve with no Accept-Language header
45+
MockHttpServletRequest reqNoHeader = new MockHttpServletRequest();
46+
Locale resolvedNoHeader = ahlr.resolveLocale(reqNoHeader);
47+
assertEquals(Locale.forLanguageTag("ca"), resolvedNoHeader,
48+
"When no Accept-Language is present, resolver should return the configured default (ca)");
49+
50+
// If header is unsupported -> fallback to default
51+
MockHttpServletRequest reqUnsupported = new MockHttpServletRequest();
52+
reqUnsupported.addHeader("Accept-Language", "fr-FR");
53+
Locale resolvedUnsupported = ahlr.resolveLocale(reqUnsupported);
54+
assertEquals(Locale.forLanguageTag("ca"), resolvedUnsupported,
55+
"When Accept-Language is unsupported, it should fall back to default (ca)");
56+
57+
// If header is supported -> honor it
58+
MockHttpServletRequest reqSupported = new MockHttpServletRequest();
59+
reqSupported.addHeader("Accept-Language", "es-ES,es;q=0.9");
60+
Locale resolvedSupported = ahlr.resolveLocale(reqSupported);
61+
assertEquals(Locale.forLanguageTag("es"), resolvedSupported,
62+
"When Accept-Language is supported, it should resolve to that locale (es)");
63+
});
64+
}
65+
66+
@Test
67+
void localeResolver_shouldHonorDifferentFrontendDefaultLanguage_viaResolution() {
68+
new ApplicationContextRunner()
69+
.withUserConfiguration(I18nConfig.class)
70+
.withBean(FrontendConfig.class, () -> {
71+
FrontendConfig fc = mock(FrontendConfig.class);
72+
when(fc.getDefaultLang()).thenReturn("es");
73+
return fc;
74+
})
75+
.run(ctx -> {
76+
AcceptHeaderLocaleResolver ahlr = (AcceptHeaderLocaleResolver) ctx.getBean(LocaleResolver.class);
77+
78+
// Resolve with no header -> should match FrontendConfig default ("es")
79+
MockHttpServletRequest reqNoHeader = new MockHttpServletRequest();
80+
Locale resolved = ahlr.resolveLocale(reqNoHeader);
81+
assertEquals(Locale.forLanguageTag("es"), resolved,
82+
"Default via FrontendConfig.getDefaultLang() must be used when no header is present");
83+
});
84+
}
85+
}
Lines changed: 94 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Comments are in English as requested
12
package es.in2.vcverifier.oid4vp.controller;
23

34
import es.in2.vcverifier.config.FrontendConfig;
@@ -9,6 +10,8 @@
910
import org.mockito.junit.jupiter.MockitoExtension;
1011
import org.springframework.ui.Model;
1112

13+
import java.util.Locale;
14+
1215
import static org.junit.jupiter.api.Assertions.assertEquals;
1316
import static org.mockito.Mockito.*;
1417

@@ -31,86 +34,95 @@ class ClientErrorControllerTest {
3134
private static final String FAVICON_SRC = "faviconSrcValue";
3235
private static final String DEFAULT_LANG = "en";
3336

34-
//todo
35-
// @Test
36-
// void showErrorPage_withValidParameters_shouldReturnViewNameAndAddAttributesToModel() {
37-
// // Arrange
38-
// String errorCode = "ERROR_CODE_123";
39-
// String errorMessage = "An error occurred during client authentication.";
40-
// String clientUrl = "https://client.example.com";
41-
// String supportUri = "https://support.example.com";
42-
// String originalRequestURL = "https://original.example.com";
43-
//
44-
//
45-
// when(frontendConfig.getSupportUrl()).thenReturn(supportUri);
46-
// when(frontendConfig.getPrimaryColor()).thenReturn(PRIMARY_COLOR);
47-
// when(frontendConfig.getPrimaryContrastColor()).thenReturn(PRIMARY_CONTRAST_COLOR);
48-
// when(frontendConfig.getSecondaryColor()).thenReturn(SECONDARY_COLOR);
49-
// when(frontendConfig.getSecondaryContrastColor()).thenReturn(SECONDARY_CONTRAST_COLOR);
50-
// when(frontendConfig.getFaviconSrc()).thenReturn(FAVICON_SRC);
51-
// when(frontendConfig.getDefaultLang()).thenReturn(DEFAULT_LANG);
52-
//
53-
// // Act
54-
// String viewName = clientErrorController.showErrorPage(errorCode, errorMessage, clientUrl, originalRequestURL ,model);
55-
//
56-
// // Assert
57-
// assertEquals("client-authentication-error-en", viewName);
58-
//
59-
// verify(model).addAttribute("errorCode", errorCode);
60-
// verify(model).addAttribute("errorMessage", errorMessage);
61-
// verify(model).addAttribute("clientUrl", clientUrl);
62-
// verify(model).addAttribute("supportUri", supportUri);
63-
// verify(model).addAttribute("originalRequestURL", originalRequestURL);
64-
// verify(model).addAttribute("primary", PRIMARY_COLOR);
65-
// verify(model).addAttribute("primaryContrast", PRIMARY_CONTRAST_COLOR);
66-
// verify(model).addAttribute("secondary", SECONDARY_COLOR);
67-
// verify(model).addAttribute("secondaryContrast", SECONDARY_CONTRAST_COLOR);
68-
// verify(model).addAttribute("faviconSrc", FAVICON_SRC);
69-
//
70-
// }
71-
//
72-
// @Test
73-
// void showErrorPage_withNullSupportUri_shouldAddNullSupportUriToModel() {
74-
// // Arrange
75-
// String errorCode = "ERROR_CODE_456";
76-
// String errorMessage = "Another error occurred.";
77-
// String clientUrl = "https://client.example.com";
78-
// String originalRequestURL = "https://original.example.com";
79-
// when(frontendConfig.getSupportUrl()).thenReturn(null);
80-
// when(frontendConfig.getDefaultLang()).thenReturn(DEFAULT_LANG);
81-
//
82-
// // Act
83-
// String viewName = clientErrorController.showErrorPage(errorCode, errorMessage, clientUrl, originalRequestURL, model);
84-
//
85-
// // Assert
86-
// assertEquals("client-authentication-error-en", viewName);
87-
//
88-
// verify(model).addAttribute("errorCode", errorCode);
89-
// verify(model).addAttribute("errorMessage", errorMessage);
90-
// verify(model).addAttribute("clientUrl", clientUrl);
91-
// verify(model).addAttribute("supportUri", null);
92-
// verify(model).addAttribute("originalRequestURL", originalRequestURL);
93-
// }
94-
//
95-
// @Test
96-
// void showErrorPage_withNullParameters_shouldAddNullValuesToModel() {
97-
// // Arrange
98-
// String supportUri = "https://support.example.com";
99-
//
100-
// when(frontendConfig.getSupportUrl()).thenReturn(supportUri);
101-
// when(frontendConfig.getDefaultLang()).thenReturn(DEFAULT_LANG);
102-
//
103-
// // Act
104-
// String viewName = clientErrorController.showErrorPage(null, null, null, null,model);
105-
//
106-
// // Assert
107-
// assertEquals("client-authentication-error-en", viewName);
108-
//
109-
// verify(model).addAttribute("errorCode", null);
110-
// verify(model).addAttribute("errorMessage", null);
111-
// verify(model).addAttribute("clientUrl", null);
112-
// verify(model).addAttribute("originalRequestURL", null);
113-
// verify(model).addAttribute("supportUri", supportUri);
114-
// }
115-
}
37+
@Test
38+
void showErrorPage_withValidParameters_shouldReturnViewNameAndAddAttributesToModel() {
39+
// Arrange
40+
String errorCode = "ERROR_CODE_123";
41+
String errorMessage = "An error occurred during client authentication.";
42+
String clientUrl = "https://client.example.com";
43+
String supportUri = "https://support.example.com";
44+
String originalRequestURL = "https://original.example.com";
45+
Locale locale = Locale.forLanguageTag("es"); // not used by controller, fallback uses defaultLang
46+
47+
when(frontendConfig.getSupportUrl()).thenReturn(supportUri);
48+
when(frontendConfig.getPrimaryColor()).thenReturn(PRIMARY_COLOR);
49+
when(frontendConfig.getPrimaryContrastColor()).thenReturn(PRIMARY_CONTRAST_COLOR);
50+
when(frontendConfig.getSecondaryColor()).thenReturn(SECONDARY_COLOR);
51+
when(frontendConfig.getSecondaryContrastColor()).thenReturn(SECONDARY_CONTRAST_COLOR);
52+
when(frontendConfig.getFaviconSrc()).thenReturn(FAVICON_SRC);
53+
when(frontendConfig.getDefaultLang()).thenReturn(DEFAULT_LANG);
54+
55+
// Act
56+
String viewName = clientErrorController.showErrorPage(
57+
errorCode, errorMessage, clientUrl, originalRequestURL, locale, model
58+
);
59+
60+
// Assert
61+
assertEquals("client-authentication-error-en", viewName);
62+
63+
verify(model).addAttribute("errorCode", errorCode);
64+
verify(model).addAttribute("errorMessage", errorMessage);
65+
verify(model).addAttribute("clientUrl", clientUrl);
66+
verify(model).addAttribute("supportUri", supportUri);
67+
verify(model).addAttribute("originalRequestURL", originalRequestURL);
68+
69+
verify(model).addAttribute("primary", PRIMARY_COLOR);
70+
verify(model).addAttribute("primaryContrast", PRIMARY_CONTRAST_COLOR);
71+
verify(model).addAttribute("secondary", SECONDARY_COLOR);
72+
verify(model).addAttribute("secondaryContrast", SECONDARY_CONTRAST_COLOR);
73+
verify(model).addAttribute("faviconSrc", FAVICON_SRC);
74+
75+
verifyNoMoreInteractions(model);
76+
}
77+
78+
@Test
79+
void showErrorPage_withNullSupportUri_shouldAddNullSupportUriToModel() {
80+
// Arrange
81+
String errorCode = "ERROR_CODE_456";
82+
String errorMessage = "Another error occurred.";
83+
String clientUrl = "https://client.example.com";
84+
String originalRequestURL = "https://original.example.com";
85+
Locale locale = Locale.forLanguageTag("en");
11686

87+
when(frontendConfig.getSupportUrl()).thenReturn(null);
88+
when(frontendConfig.getDefaultLang()).thenReturn(DEFAULT_LANG);
89+
90+
// Act
91+
String viewName = clientErrorController.showErrorPage(
92+
errorCode, errorMessage, clientUrl, originalRequestURL, locale, model
93+
);
94+
95+
// Assert
96+
assertEquals("client-authentication-error-en", viewName);
97+
98+
verify(model).addAttribute("errorCode", errorCode);
99+
verify(model).addAttribute("errorMessage", errorMessage);
100+
verify(model).addAttribute("clientUrl", clientUrl);
101+
verify(model).addAttribute("supportUri", null);
102+
verify(model).addAttribute("originalRequestURL", originalRequestURL);
103+
}
104+
105+
@Test
106+
void showErrorPage_withNullParameters_shouldAddNullValuesToModel() {
107+
// Arrange
108+
String supportUri = "https://support.example.com";
109+
Locale locale = Locale.forLanguageTag("fr");
110+
111+
when(frontendConfig.getSupportUrl()).thenReturn(supportUri);
112+
when(frontendConfig.getDefaultLang()).thenReturn(DEFAULT_LANG);
113+
114+
// Act
115+
String viewName = clientErrorController.showErrorPage(
116+
null, null, null, null, locale, model
117+
);
118+
119+
// Assert
120+
assertEquals("client-authentication-error-en", viewName);
121+
122+
verify(model).addAttribute("errorCode", null);
123+
verify(model).addAttribute("errorMessage", null);
124+
verify(model).addAttribute("clientUrl", null);
125+
verify(model).addAttribute("originalRequestURL", null);
126+
verify(model).addAttribute("supportUri", supportUri);
127+
}
128+
}

0 commit comments

Comments
 (0)