@Test
public void generateSSLCertificate_With_MetaDataFailure() throws Exception {

    String jsonStr = "{  \"username\": \"testusername1\",  \"password\": \"testpassword1\"}";
    CertManagerLoginRequest certManagerLoginRequest = getCertManagerLoginRequest();
    certManagerLoginRequest.setUsername("username");
    certManagerLoginRequest.setPassword("password");
    SSLCertificateRequest sslCertificateRequest = getSSLCertificateRequest();
    sslCertificateRequest.setCertificateName("certificatename");
    String[] dnsNames = { };
    sslCertificateRequest.setDnsList(dnsNames);
    Map<String, Object> requestMap = new HashMap<>();
    requestMap.put("access_token", "12345");
    requestMap.put("token_type", "type");
    when(ControllerUtil.parseJson(jsonStr)).thenReturn(requestMap);
    CertManagerLogin certManagerLogin = new CertManagerLogin();
    certManagerLogin.setToken_type("token type");
    certManagerLogin.setAccess_token("1234");
    CertResponse response = new CertResponse();
    response.setHttpstatus(HttpStatus.OK);
    response.setResponse(jsonStr);
    response.setSuccess(true);
    when(reqProcessor.processCert(eq("/auth/certmanager/login"), anyObject(), anyString(), anyString())).thenReturn(response);
    when(reqProcessor.processCert(eq("/certmanager/findCertificate"), anyObject(), anyString(), anyString())).thenReturn(response);
    CertResponse response1 = new CertResponse();
    response1.setHttpstatus(HttpStatus.OK);
    response1.setResponse(jsonStr);
    response1.setSuccess(true);
    //Create Target System Validation
    when(reqProcessor.processCert(eq("/certmanager/findTargetSystem"), anyObject(), anyString(), anyString())).thenReturn(response1);
    String createTargetSystemResponse = "{  \"name\": \"TARGET SYSTEM1\",  \"password\": \"testpassword1\"}";
    response1.setResponse(createTargetSystemResponse);
    Map<String, Object> createTargetSystemMap = new HashMap<>();
    createTargetSystemMap.put("targetSystemID", 29);
    createTargetSystemMap.put("name", "TARGET SYSTEM1");
    createTargetSystemMap.put("description", "TARGET SYSTEM1");
    createTargetSystemMap.put("address", "address");
    when(ControllerUtil.parseJson(createTargetSystemResponse)).thenReturn(createTargetSystemMap);
    when(reqProcessor.processCert(eq("/certmanager/targetsystem/create"), anyObject(), anyString(), anyString())).thenReturn(response1);
    // loadTargetSystemServiceData();
    //Create Target System Validation
    CertResponse response2 = new CertResponse();
    String jsonStr1 = "{  \"name\": \"targetService\",  \"address\": \"targetServiceaddress\"}";
    response2.setHttpstatus(HttpStatus.OK);
    response2.setResponse(jsonStr1);
    response2.setSuccess(true);
    when(reqProcessor.processCert(eq("/certmanager/findTargetSystemService"), anyObject(), anyString(), anyString())).thenReturn(response2);
    String createTargetSystemServiceResponse =
            "{  \"name\": \"TARGETSYSTEMService\",  \"password\": , \"testpassword1\"}";
    response2.setResponse(createTargetSystemServiceResponse);
    Map<String, Object> createTargetSystemServiceMap = new HashMap<>();
    createTargetSystemServiceMap.put("targetSystemServiceId", 40);
    createTargetSystemServiceMap.put("hostname", "TARGETSYSTEMSERVICEHOST");
    createTargetSystemServiceMap.put("name", "TARGETSYSTEMSERVICE");
    createTargetSystemServiceMap.put("port", 443);
    createTargetSystemServiceMap.put("targetSystemGroupId", 11);
    createTargetSystemServiceMap.put("targetSystemId", 12);
    Response responseNoContent = getMockResponse(HttpStatus.OK, true, "");
    when(ControllerUtil.createMetadata(Mockito.any(), any())).thenReturn(false);
    when(reqProcessor.process(eq("/access/update"),any(),eq(token))).thenReturn(responseNoContent);
    when(ControllerUtil.parseJson(createTargetSystemServiceResponse)).thenReturn(createTargetSystemServiceMap);
    when(reqProcessor.processCert(eq("/certmanager/targetsystemservice/create"), anyObject(), anyString(), anyString())).thenReturn(response2);
    //getEnrollCA Validation
    when(reqProcessor.processCert(eq("/certmanager/getEnrollCA"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCAResponse());
    ///putEnrollCA Validation
    when(reqProcessor.processCert(eq("/certmanager/putEnrollCA"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCAResponse());
    ///getEnrollTemplate Validation
    when(reqProcessor.processCert(eq("/certmanager/getEnrollTemplates"), anyObject(), anyString(), anyString())).thenReturn(getEnrollTemplateResponse());
    ///getEnrollTemplate Validation
    when(reqProcessor.processCert(eq("/certmanager/putEnrollTemplates"), anyObject(), anyString(), anyString())).thenReturn(getEnrollTemplateResponse());
    ///getEnrollKeys Validation
    when(reqProcessor.processCert(eq("/certmanager/getEnrollkeys"), anyObject(), anyString(), anyString())).thenReturn(getEnrollKeysResponse());
    ///putEnrollKeys Validation
    when(reqProcessor.processCert(eq("/certmanager/putEnrollKeys"), anyObject(), anyString(), anyString())).thenReturn(getEnrollKeysResponse());
    ///getEnrollCSR Validation
    when(reqProcessor.processCert(eq("/certmanager/getEnrollCSR"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCSRResponse());
    ///putEnrollCSR Validation
    when(reqProcessor.processCert(eq("/certmanager/putEnrollCSR"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCSRResponse());
    //enroll
    when(reqProcessor.processCert(eq("/certmanager/enroll"), anyObject(), anyString(), anyString())).thenReturn(getEnrollResonse());
    ResponseEntity<?> enrollResponse =
            sSLCertificateService.generateSSLCertificate(sslCertificateRequest,userDetails,token);
    //Assert
    assertNotNull(enrollResponse);
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, enrollResponse.getStatusCode());
}