### Question:
TargetTypesServiceImpl implements TargetTypesService { @Override public String addTargetTypeDetails(final CreateUpdateTargetTypeDetailsRequest targetTypeDetailsRequest, final String userId) throws PacManException { try { String dataSource = targetTypeDetailsRequest.getDataSource().toLowerCase().trim(); String type = targetTypeDetailsRequest.getName().toLowerCase().trim().replaceAll(" ", "-"); String indexName = dataSource+"_"+type; StringBuilder payLoad = new StringBuilder("{ \"mappings\": {"); payLoad.append("\""+type+"\":{},\"issue_"+type+"\": { \"_parent\": {\"type\": \""+type+"\"}},\"recommendation_"+type+"\": { \"_parent\": {\"type\": \""+type+"\"}},\"issue_"+type+"_audit\": { \"_parent\": {\"type\": \"issue_"+type+"\"}},\"issue_"+type+"_comment\": { \"_parent\": {\"type\": \"issue_"+type+"\"}},\"issue_"+type+"_exception\": { \"_parent\": {\"type\": \"issue_"+type+"\"}}"); payLoad.append("}}"); if(!indexExists(indexName)) { Response indexResponse = invokeAPI("PUT", indexName, payLoad.toString()); Response aliasResponse = invokeAPI("PUT", "/"+indexName+"/_alias/"+dataSource,null); if ((aliasResponse != null) && (indexResponse != null)) { if ((indexResponse.getStatusLine().getStatusCode() == 200) && (aliasResponse.getStatusLine().getStatusCode() == 200)) { return processTargetTypeCreation(targetTypeDetailsRequest, userId); } else { throw new PacManException(AdminConstants.TARGET_TYPE_CREATION_FAILURE); } } else { throw new PacManException(AdminConstants.TARGET_TYPE_CREATION_FAILURE); } } else { throw new PacManException(AdminConstants.TARGET_TYPE_INDEX_EXITS); } } catch(Exception exception) { log.error(UNEXPECTED_ERROR_OCCURRED, exception); throw new PacManException(AdminConstants.TARGET_TYPE_CREATION_FAILURE); } }  @Override Collection<String> getTargetTypesNamesByDataSourceName(final String dataSourceName); @Override List<TargetTypes> getAllTargetTypesByDomainList(final List<String> domains); @Override Page<TargetTypesProjections> getAllTargetTypeDetails(String searchTerm, int page, int size); @Override List<String> getAllTargetTypesCategories(); @Override List<TargetTypesDetails> getAllTargetTypes(List<AssetGroupTargetTypes> selectedTargetTypes); @Override List<TargetTypeAttribute> getTargetTypeAttributes(final List<TargetTypes> targetTypes); @Override Map<String, Object> getAttributeValues(AttributeValuesRequest attributeValuesRequest); @Override TargetTypes getTargetTypesByName(String targetTypeName); @Override String updateTargetTypeDetails(CreateUpdateTargetTypeDetailsRequest targetTypesDetails, final String userId); @Override String addTargetTypeDetails(final CreateUpdateTargetTypeDetailsRequest targetTypeDetailsRequest, final String userId); @Override void deleteIndex(String targetTypeIndex);  }### Answer:
@SuppressWarnings({ "unchecked", "unused" }) @Test public void addTargetTypeDetailsTest() throws Exception { TargetTypesServiceImpl mock = PowerMockito.spy(new TargetTypesServiceImpl()); when(targetTypesRepository.existsById("targetTypeName123")).thenReturn(true); when(targetTypesRepository.findById("targetTypeName123")).thenReturn(getTargetTypesValues()); when(config.getElasticSearch()).thenReturn(getElasticSearchProperty()); ElasticSearchProperty elasticSearchProperty = getElasticSearchProperty(); HttpEntity jsonEntity = new StringEntity("{}", ContentType.APPLICATION_JSON); when(targetTypesRepository.existsById(anyString())).thenReturn(false); when(response.getEntity()).thenReturn(jsonEntity); when(config.getElasticSearch()).thenReturn(elasticSearchProperty); when(restClient.performRequest(anyString(), anyString(), any(Map.class), any(HttpEntity.class), Matchers.<Header>anyVararg())).thenReturn(response); ReflectionTestUtils.setField(targetTypesServiceImpl, "restClient", restClient); when(sl.getStatusCode()).thenReturn(200); when(response.getStatusLine()).thenReturn(sl); assertThatThrownBy(() -> targetTypesService.addTargetTypeDetails(getTargetTypeDetailsRequest(), "userId")).isInstanceOf(PacManException.class); }