### Question:
X509ExternalMapper extends X509AbstractMapper { @Override public String mapCertificateToMainframeUserId(X509Certificate certificate) { if (isClientAuthCertificate(certificate)) { try { String jwtToken = tokenCreationService.createJwtTokenWithoutCredentials(externalMapperUser); HttpPost httpPost = new HttpPost(new URI(externalMapperUrl)); HttpEntity httpEntity = new ByteArrayEntity(certificate.getEncoded()); httpPost.setEntity(httpEntity); httpPost.setHeader(new BasicHeader("Cookie", "apimlAuthenticationToken=" + jwtToken)); HttpResponse httpResponse = httpClientProxy.execute(httpPost); String response = EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8); if (response == null || response.isEmpty()) { return null; } ObjectMapper objectMapper = new ObjectMapper(); CertMapperResponse certMapperResponse = objectMapper.readValue(response, CertMapperResponse.class); return certMapperResponse.getUserId().trim(); } catch (URISyntaxException e) { log.error("Wrong service URI provided", e); } catch (CertificateEncodingException e) { log.error("Can`t get encoded data from certificate", e); } catch (IOException e) { log.error("Not able to send certificate to mapper", e); } return null; } return null; }  @Override String mapCertificateToMainframeUserId(X509Certificate certificate);  }### Answer:
@Test void givenValidHttpResponse_thenReturnUserId() throws CertificateEncodingException, IOException, CertificateParsingException { x509ExternalMapper = new X509ExternalMapper(closeableHttpClient, tokenCreationService); ReflectionTestUtils.setField(x509ExternalMapper,"externalMapperUrl",""); when(x509Certificate.getExtendedKeyUsage()).thenReturn(Collections.singletonList(CLIENT_AUTH_OID)); when(x509Certificate.getEncoded()).thenReturn(new byte[2]); when(httpResponse.getEntity()).thenReturn(entity); when(entity.getContent()).thenReturn(new ByteArrayInputStream("{\"userid\":\"ZOSUSER\",\"rc\":0,\"saf_rc\":0,\"racf_rc\":0,\"reason_code\":0}".getBytes())); String userId = x509ExternalMapper.mapCertificateToMainframeUserId(x509Certificate); assertEquals("ZOSUSER", userId); }
@Test void givenValidHttpResponse_andUserIsNotFound_thenReturnNull() throws CertificateEncodingException, IOException, CertificateParsingException { x509ExternalMapper = new X509ExternalMapper(closeableHttpClient, tokenCreationService); ReflectionTestUtils.setField(x509ExternalMapper,"externalMapperUrl",""); when(x509Certificate.getExtendedKeyUsage()).thenReturn(Collections.singletonList(CLIENT_AUTH_OID)); when(x509Certificate.getEncoded()).thenReturn(new byte[2]); when(httpResponse.getEntity()).thenReturn(entity); when(entity.getContent()).thenReturn(new ByteArrayInputStream("".getBytes())); String userId = x509ExternalMapper.mapCertificateToMainframeUserId(x509Certificate); assertNull(userId); }
@Test void givenInvalidUriCharacters_thenNullIsReturned_andExceptionIsHandled() throws CertificateParsingException { when(x509Certificate.getExtendedKeyUsage()).thenReturn(Collections.singletonList(CLIENT_AUTH_OID)); x509ExternalMapper = new X509ExternalMapper(closeableHttpClient, tokenCreationService); ReflectionTestUtils.setField(x509ExternalMapper,"externalMapperUrl","%"); assertNull(x509ExternalMapper.mapCertificateToMainframeUserId(x509Certificate)); }
@Test void givenInvalidCertificateEncodedData_thenNullIsReturned_andExceptionIsHandled() throws CertificateEncodingException, CertificateParsingException { when(x509Certificate.getExtendedKeyUsage()).thenReturn(Collections.singletonList(CLIENT_AUTH_OID)); x509ExternalMapper = new X509ExternalMapper(closeableHttpClient, tokenCreationService); ReflectionTestUtils.setField(x509ExternalMapper,"externalMapperUrl",""); when(x509Certificate.getEncoded()).thenThrow(new CertificateEncodingException()); assertNull(x509ExternalMapper.mapCertificateToMainframeUserId(x509Certificate)); }