From 792d59d645ce2bae0a5f2f612717ef8dfc72b119 Mon Sep 17 00:00:00 2001 From: Radoslaw Orlowski <59831983+radoslaw-orlowski-amido@users.noreply.github.com> Date: Tue, 9 Jun 2020 15:42:23 +0100 Subject: [PATCH] Increase test code coverage. (#377) * Increase test code coverage. * Increase test code coverage, pt.2. * avoid running functional tests twice in nightly build * Add missing assertions in a test. Co-authored-by: Shravan Mechineni --- Jenkinsfile_nightly | 17 --------------- .../reform/idam/web/helper/JSPHelper.java | 15 ++++++------- .../reform/idam/web/helper/JSPHelperTest.java | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Jenkinsfile_nightly b/Jenkinsfile_nightly index f564d5305..1bb1d6b86 100644 --- a/Jenkinsfile_nightly +++ b/Jenkinsfile_nightly @@ -58,23 +58,6 @@ withNightlyPipeline(type, product, component) { after('fullFunctionalTest') { - sh "./gradlew smoke" - - archiveArtifacts '**/build/test-results/**/*' - - publishHTML target: [ - allowMissing : true, - alwaysLinkToLastBuild: true, - keepAll : true, - reportDir : "output", - reportFiles : "idam-web-public-e2e-result.html", - reportName : "IDAM Web Public E2E smoke tests result" - ] - - sh "./gradlew functional" - - archiveArtifacts '**/build/test-results/**/*' - publishHTML target: [ allowMissing : true, alwaysLinkToLastBuild: true, diff --git a/src/main/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelper.java b/src/main/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelper.java index 6b648776b..c0d2c4180 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelper.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelper.java @@ -1,8 +1,9 @@ package uk.gov.hmcts.reform.idam.web.helper; import lombok.NonNull; -import lombok.SneakyThrows; import lombok.experimental.UtilityClass; +import org.apache.commons.codec.DecoderException; +import org.apache.commons.codec.net.URLCodec; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.web.context.request.RequestContextHolder; @@ -14,8 +15,6 @@ import javax.annotation.Nonnull; import javax.servlet.http.HttpServletRequest; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; import java.util.Locale; import java.util.Optional; @@ -23,6 +22,7 @@ public class JSPHelper { private static final UrlPathHelper PATH_HELPER = new UrlPathHelper(); + private static final URLCodec URL_CODEC = new URLCodec(); private static MessageSource messageSource; /** @@ -31,16 +31,17 @@ public class JSPHelper { * @should throw if there is no request in context */ @Nonnull - @SneakyThrows(UnsupportedEncodingException.class) - public String getOtherLocaleUrl() { + public String getOtherLocaleUrl() throws DecoderException { final ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()); final HttpServletRequest request = servletRequestAttributes != null ? servletRequestAttributes.getRequest() : null; final String targetLocale = getTargetLocale(); + if (request != null) { final String requestUri = PATH_HELPER.getOriginatingRequestUri(request); final String originatingQueryString = PATH_HELPER.getOriginatingQueryString(request); - final String requestQueryString = originatingQueryString == null ? null : URLDecoder.decode(originatingQueryString, "UTF-8"); //NOSONAR + final String requestQueryString; //NOSONAR + requestQueryString = originatingQueryString == null ? null : URL_CODEC.decode(originatingQueryString); final UriComponentsBuilder initialUrl = UriComponentsBuilder.fromPath(requestUri).replaceQuery(requestQueryString); return overrideLocaleParameter(initialUrl, targetLocale); @@ -54,9 +55,9 @@ public String getOtherLocaleUrl() { * * @should override existing parameter * @should add nonexisting parameter + * @should throw on any of the parameters being null */ public static String overrideLocaleParameter(@NonNull final UriComponentsBuilder builder, @NonNull final String targetLocale) { - return builder.replaceQueryParam(MessagesConfiguration.UI_LOCALES_PARAM_NAME, new Locale(targetLocale)).toUriString(); } diff --git a/src/test/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelperTest.java b/src/test/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelperTest.java index 3f94d7a62..7045a20ed 100644 --- a/src/test/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelperTest.java +++ b/src/test/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelperTest.java @@ -56,6 +56,27 @@ public void overrideLocaleParameter_shouldAddNonexistingParameter() { Assert.assertEquals(CORRECT_BASE_TEST_URI + targetLocale, rewrittenUrl); } + /** + * @verifies throw on any of the parameters being null + * @see JSPHelper#overrideLocaleParameter(org.springframework.web.util.UriComponentsBuilder, String) + */ + @Test + public void overrideLocaleParameter_shouldThrowOnAnyOfTheParametersBeingNull() { + try { + JSPHelper.overrideLocaleParameter(UriComponentsBuilder.newInstance(), null); + Assert.fail(); + } catch (NullPointerException e) { + // do nothing, expected + } + + try { + JSPHelper.overrideLocaleParameter(null, "en"); + Assert.fail(); + } catch (NullPointerException e) { + // do nothing, expected + } + } + /** * @verifies return en if current locale is welsh * @see JSPHelper#getTargetLocale()