SSLCertificateService { public ResponseEntity<String> getTargetSystemList(String token, UserDetails userDetails,String certType) throws Exception { if(!certType.matches(SSLCertificateConstants.CERT_TYPE_MATCH_STRING)){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "getTargetSystemList") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } String getTargetSystemEndpoint = "/certmanager/findTargetSystem"; SSLCertType sslCertType = certType.equalsIgnoreCase("internal")? SSLCertType.valueOf("PRIVATE_SINGLE_SAN"): SSLCertType.valueOf("PUBLIC_SINGLE_SAN"); String findTargetSystemEndpoint = findTargetSystem.replace("tsgid", String.valueOf(getTargetSystemGroupId(sslCertType))); List<TargetSystemDetails> targetSystemDetails = new ArrayList<>(); String nclmToken = getNclmToken(); if(!StringUtils.isEmpty(nclmToken)) { CertResponse response = reqProcessor.processCert(getTargetSystemEndpoint, "", nclmToken, getCertmanagerEndPoint(findTargetSystemEndpoint)); if (HttpStatus.OK.equals(response.getHttpstatus())) { JsonParser jsonParser = new JsonParser(); JsonObject jsonObject = (JsonObject) jsonParser.parse(response.getResponse()); if (jsonObject != null && jsonObject.get(SSLCertificateConstants.TARGETSYSTEMS) != null && !jsonObject.get(SSLCertificateConstants.TARGETSYSTEMS).toString().equalsIgnoreCase("null")) { JsonArray jsonArray = jsonObject.getAsJsonArray(SSLCertificateConstants.TARGETSYSTEMS); for (int i = 0; i < jsonArray.size(); i++) { JsonObject jsonElement = jsonArray.get(i).getAsJsonObject(); targetSystemDetails.add(new TargetSystemDetails(jsonElement.get(SSLCertificateConstants.NAME).getAsString(), jsonElement.get(SSLCertificateConstants.DESCRIPTION).getAsString(), jsonElement.get(SSLCertificateConstants.ADDRESS).getAsString(), jsonElement.get(SSLCertificateConstants.TARGETSYSTEM_ID).getAsString())); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getTargetSystemList"). put(LogMessage.MESSAGE, "Successfully retrieved target system list from NCLM"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"data\": "+JSONUtil.getJSONasDefaultPrettyPrint(targetSystemDetails)+"}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getTargetSystemList"). put(LogMessage.MESSAGE, "Retrieved empty target system list from NCLM"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"data\": "+JSONUtil.getJSONasDefaultPrettyPrint(targetSystemDetails)+"}"); } if (HttpStatus.INTERNAL_SERVER_ERROR.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getTargetSystemList"). put(LogMessage.MESSAGE, "NCLM services are down. Please try after some time"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + nclmErrorMessage + "\"]}"); } }else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "geTargetSystemList"). put(LogMessage.MESSAGE, "NCLM services are down. Please try after some time."). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"errors\":[\"" + nclmErrorMessage + "\"]}"); } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getTargetSystemList"). put(LogMessage.MESSAGE, "Failed to get Target system list from NCLM"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Failed to get Target system list\"]}"); }  ResponseEntity<String> authenticate(CertManagerLoginRequest certManagerLoginRequest); CertManagerLogin login(CertManagerLoginRequest certManagerLoginRequest); ResponseEntity<String> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest,
                                                               UserDetails userDetails ,String token); ResponseEntity<String> getServiceCertificates(String token, UserDetails userDetails, String certName, Integer limit, Integer offset, String certType); String getNclmToken(); ResponseEntity<String> getTargetSystemList(String token, UserDetails userDetails,String certType); ResponseEntity<String> getTargetSystemServiceList(String token, UserDetails userDetails, String targetSystemId); ResponseEntity<String> getRevocationReasons(Integer certificateId, String token); ResponseEntity<String> issueRevocationRequest(String certType, String certificateName, UserDetails userDetails, String token,
			RevocationRequest revocationRequest); String getCurrentLocalDateTimeStamp(); Boolean validateOwnerPermissionForNonAdmin(UserDetails userDetails, String certificateName); ResponseEntity<String> addUserToCertificate(CertificateUser certificateUser, UserDetails userDetails, boolean addSudoPermission); ResponseEntity<String> addGroupToCertificate(UserDetails userDetails, String userToken, CertificateGroup certificateGroup); boolean isAuthorized(UserDetails userDetails, String certificatename, String certType); ResponseEntity<String> addingGroupToCertificate(String token, CertificateGroup certificateGroup, UserDetails userDetails); ResponseEntity<String> associateApproletoCertificate(CertificateApprole certificateApprole, UserDetails userDetails); boolean hasDownloadPermission(String certificateName, UserDetails userDetails, String certType); SSLCertificateMetadataDetails getCertificateMetadata(String token, String certificateName); ResponseEntity<InputStreamResource> downloadCertificateWithPrivateKey(String token, CertificateDownloadRequest certificateDownloadRequest, UserDetails userDetails); ResponseEntity<InputStreamResource> downloadCertificateWithPrivateKey(CertificateDownloadRequest certificateDownloadRequest, SSLCertificateMetadataDetails sslCertificateMetadataDetails); ResponseEntity<InputStreamResource> downloadCertificate(String token, UserDetails userDetails,
                                                                   String certificateName, String certificateType,
                                                                   String sslCertType); ResponseEntity<String> getCertificateDetails(String token, String certificateName, String certificateType); ResponseEntity<String> renewCertificate(String certType, String certificateName, UserDetails userDetails, String token); ResponseEntity<String> removeUserFromCertificate(CertificateUser certificateUser, UserDetails userDetails); ResponseEntity<String> removeGroupFromCertificate(CertificateGroup certificateGroup, UserDetails userDetails); ResponseEntity<String> getListOfCertificates(String token, String certificateType); ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId,  UserDetails userDetails); ResponseEntity<String> validateApprovalStatusAndGetCertificateDetails(String certName, String certType,
			UserDetails userDetails); ResponseEntity<String> deleteCertificate( String token, String certType, String certificateName, UserDetails userDetails); Boolean validateCertOwnerPermissionForNonAdmin(UserDetails userDetails, String certificateName, String certType); ResponseEntity<String> getAllCertificates(String token, String certName, Integer limit, Integer offset); ResponseEntity<String> getAllCertificatesOnCertType(UserDetails userDetails, String certificateType); ResponseEntity<String> checkCertificateStatus(String certName, String certType,	
			UserDetails userDetails); ResponseEntity<String> getAllSelfServiceGroups(UserDetails userDetails); ResponseEntity<String> removeGroupFromCertificateForDelete(CertificateGroup certificateGroup, UserDetails userDetails); @Value("${sslcertmanager.targetsystemgroup.public_single_san.ts_gp_id}")
    private int public_single_san_ts_gp_id; @Value("${sslcertmanager.targetsystemgroup.public_multi_san.ts_gp_id}")
    private int public_multi_san_ts_gp_id; }
@Test public void test_getgetTargetSystemList_success()throws Exception{ String token = "12345"; String certType= "internal"; String jsonStr = "{\"targetSystems\": [ {" + " \"name\" : \"abc.com\"," + " \"description\" : \"\"," + " \"address\" : \"abc.com\"," + " \"targetSystemID\" : \"234\"" + "}, {" + " \"name\" : \"cde.com\"," + " \"description\" : \"cde.com\"," + " \"address\" : \"cde.com\"," + " \"targetSystemID\" : \"123\"" + "}]}"; CertResponse response = new CertResponse(); response.setHttpstatus(HttpStatus.OK); response.setResponse(jsonStr); response.setSuccess(true); String jsonStrUser = "{ \"username\": \"testusername1\", \"password\": \"testpassword1\"}"; CertResponse responseUser = new CertResponse(); responseUser.setHttpstatus(HttpStatus.OK); responseUser.setResponse(jsonStrUser); responseUser.setSuccess(true); Map<String, Object> requestMap = new HashMap<>(); requestMap.put("access_token", "12345"); requestMap.put("token_type", "type"); when(ControllerUtil.parseJson(jsonStrUser)).thenReturn(requestMap); when(reqProcessor.processCert(eq("/auth/certmanager/login"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(responseUser); when(reqProcessor.processCert(eq( "/certmanager/findTargetSystem"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); when(JSONUtil.getJSONasDefaultPrettyPrint(Mockito.any())).thenReturn(jsonStr); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"data\": "+jsonStr+"}"); ResponseEntity<String> responseEntityActual = sSLCertificateService.getTargetSystemList(token, getMockUser(true), certType); assertEquals(HttpStatus.OK, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected.getBody(), responseEntityActual.getBody()); }
@Test public void test_getgetTargetSystemList_External_success()throws Exception{ String token = "12345"; String certType= "external"; String jsonStr = "{\"targetSystems\": [ {" + " \"name\" : \"abc.com\"," + " \"description\" : \"\"," + " \"address\" : \"abc.com\"," + " \"targetSystemID\" : \"234\"" + "}, {" + " \"name\" : \"cde.com\"," + " \"description\" : \"cde.com\"," + " \"address\" : \"cde.com\"," + " \"targetSystemID\" : \"123\"" + "}]}"; CertResponse response = new CertResponse(); response.setHttpstatus(HttpStatus.OK); response.setResponse(jsonStr); response.setSuccess(true); String jsonStrUser = "{ \"username\": \"testusername1\", \"password\": \"testpassword1\"}"; CertResponse responseUser = new CertResponse(); responseUser.setHttpstatus(HttpStatus.OK); responseUser.setResponse(jsonStrUser); responseUser.setSuccess(true); Map<String, Object> requestMap = new HashMap<>(); requestMap.put("access_token", "12345"); requestMap.put("token_type", "type"); when(ControllerUtil.parseJson(jsonStrUser)).thenReturn(requestMap); when(reqProcessor.processCert(eq("/auth/certmanager/login"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(responseUser); when(reqProcessor.processCert(eq( "/certmanager/findTargetSystem"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); when(JSONUtil.getJSONasDefaultPrettyPrint(Mockito.any())).thenReturn(jsonStr); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"data\": "+jsonStr+"}"); ResponseEntity<String> responseEntityActual = sSLCertificateService.getTargetSystemList(token, getMockUser(true), certType); assertEquals(HttpStatus.OK, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected.getBody(), responseEntityActual.getBody()); }
@Test public void test_getgetTargetSystemList_failed()throws Exception{ String token = "12345"; String certType= "internal"; String jsonStr = "{\"targetSystems\": [ {" + " \"name\" : \"abc.com\"," + " \"description\" : \"\"," + " \"address\" : \"abc.com\"," + " \"targetSystemID\" : \"234\"" + "}, {" + " \"name\" : \"cde.com\"," + " \"description\" : \"cde.com\"," + " \"address\" : \"cde.com\"," + " \"targetSystemID\" : \"123\"" + "}]}"; CertResponse response = new CertResponse(); response.setHttpstatus(HttpStatus.INTERNAL_SERVER_ERROR); response.setResponse("{\"errors\":[\"Your request cannot be processed now due to some technical issue. Please try after some time\"]}"); response.setSuccess(false); String jsonStrUser = "{ \"username\": \"testusername1\", \"password\": \"testpassword1\"}"; CertResponse responseUser = new CertResponse(); responseUser.setHttpstatus(HttpStatus.OK); responseUser.setResponse(jsonStrUser); responseUser.setSuccess(true); Map<String, Object> requestMap = new HashMap<>(); requestMap.put("access_token", "12345"); requestMap.put("token_type", "type"); when(ControllerUtil.parseJson(jsonStrUser)).thenReturn(requestMap); when(reqProcessor.processCert(eq("/auth/certmanager/login"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(responseUser); when(reqProcessor.processCert(eq( "/certmanager/findTargetSystem"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Your request cannot be processed now due to some technical issue. Please try after some time\"]}"); ResponseEntity<String> responseEntityActual = sSLCertificateService.getTargetSystemList(token, getMockUser(true), certType); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected.getBody(), responseEntityActual.getBody()); }
@Test public void test_getgetTargetSystemList_empty()throws Exception{ String token = "12345"; String certType= "internal"; String jsonStr = "{\"data\": [ ]}"; CertResponse response = new CertResponse(); response.setHttpstatus(HttpStatus.OK); response.setResponse(jsonStr); response.setSuccess(false); String jsonStrUser = "{ \"username\": \"testusername1\", \"password\": \"testpassword1\"}"; CertResponse responseUser = new CertResponse(); responseUser.setHttpstatus(HttpStatus.OK); responseUser.setResponse(jsonStrUser); responseUser.setSuccess(true); Map<String, Object> requestMap = new HashMap<>(); requestMap.put("access_token", "12345"); requestMap.put("token_type", "type"); when(ControllerUtil.parseJson(jsonStrUser)).thenReturn(requestMap); when(reqProcessor.processCert(eq("/auth/certmanager/login"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(responseUser); when(reqProcessor.processCert(eq( "/certmanager/findTargetSystem"), Mockito.anyObject(), Mockito.anyString(), Mockito.anyString())).thenReturn(response); when(JSONUtil.getJSONasDefaultPrettyPrint(Mockito.any())).thenReturn("[]"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"data\": []}"); ResponseEntity<String> responseEntityActual = sSLCertificateService.getTargetSystemList(token, getMockUser(true), certType); assertEquals(HttpStatus.OK, responseEntityActual.getStatusCode()); assertEquals(responseEntityExpected.getBody(), responseEntityActual.getBody()); }