### Question:
CustomDataSourceContextStrategy implements
        ContextManagementStrategy<ClientCustomDataSource, ClientCustomDataSource>,
        GenericTypeMetadataBuilder<ClientCustomDataSource>,
        ContextParametrizedMetadataBuilder<ClientCustomDataSource, PartialMetadataOptions> { @Override public ClientCustomDataSource createContext(ClientCustomDataSource contextDescription, Map<String, Object> data) throws IllegalParameterValueException { boolean passed = false; Exception exception = null; try { final CustomReportDataSource reportDataSource = toServer(contextDescription); ReportDataSourceService service = customDataSourceFactory.createService(reportDataSource); if (service instanceof ConnectionTestingDataSourceService) { passed = ((ConnectionTestingDataSourceService) service).testConnection(); } else { passed = true; } if (service instanceof ReportDataAdapterService) { engine.getMetaDataFromConnector(reportDataSource); } } catch (Exception e) { exception = e; } if (!passed || exception != null) { if (exception instanceof ErrorDescriptorException) { throw (ErrorDescriptorException) exception; } else { throw new ContextCreationFailedException(contextDescription, null, "Connection failed", exception, secureExceptionHandler); } } return contextDescription; }  @Override ClientCustomDataSource createContext(ClientCustomDataSource contextDescription, Map<String, Object> data); @Override void deleteContext(ClientCustomDataSource contextDescription, Map<String, Object> data); @Override ClientCustomDataSource getContextForClient(ClientCustomDataSource contextDescription, Map<String, Object> data, Map<String, String[]> additionalProperties); boolean isMetadataSupported(ClientCustomDataSource clientCustomDataSource, String metadataClientType); @Override Object build(ClientCustomDataSource context, Map<String, String[]> options, Map<String, Object> contextData); @Override String getMetadataClientResourceType(ClientCustomDataSource connectionDescription); @Override Object build(ClientCustomDataSource context, PartialMetadataOptions options, Map<String, Object> contextData);  }### Answer:
@Test public void createConnection_noPasswordProperty_success() throws Exception { doReturn(true).when(customReportDataSourceService).testConnection(); final ClientCustomDataSource connection = strategy.createContext(customDataSource, null); assertSame(connection, customDataSource); }
@Test public void createConnection_noTestConnection_success() throws JRException { reset(customDataSourceFactory); when(customDataSourceFactory.createService(serverCustomReportDataSource)).thenReturn(mock(ReportDataSourceService.class)); final ClientCustomDataSource connection = strategy.createContext(customDataSource, null); assertSame(connection, customDataSource); }
@Test(expectedExceptions = ContextCreationFailedException.class) public void createConnection_testConnectionReturnsFalse_failure() throws Exception { doReturn(false).when(customReportDataSourceService).testConnection(); final ClientCustomDataSource connection = strategy.createContext(customDataSource, null); assertSame(connection, customDataSource); }
@Test(expectedExceptions = ContextCreationFailedException.class) public void createConnection_testConnectionThrowsException_failure() throws Exception { doThrow(new RuntimeException()).when(customReportDataSourceService).testConnection(); final ClientCustomDataSource connection = strategy.createContext(customDataSource, null); assertSame(connection, customDataSource); }
@Test public void createConnection_passwordIsNull_success() throws Exception { doReturn(true).when(customReportDataSourceService).testConnection(); serverCustomReportDataSource.getPropertyMap().put("password", null); final CustomReportDataSourceImpl dsFromRepository = new CustomReportDataSourceImpl(); dsFromRepository.setPropertyMap(new HashMap()); final String expectedPassword = "expectedPassword"; dsFromRepository.getPropertyMap().put("password", expectedPassword); customDataSource.setUri(serverCustomReportDataSource.getURIString()); when(repository.getResource(null, serverCustomReportDataSource.getURIString())).thenReturn(dsFromRepository); reset(customDataSourceFactory); ArgumentCaptor<CustomReportDataSource> argumentCaptor = ArgumentCaptor.forClass(CustomReportDataSource.class); doReturn(customReportDataSourceService).when(customDataSourceFactory).createService(argumentCaptor.capture()); final ClientCustomDataSource connection = strategy.createContext(customDataSource, null); assertSame(connection, customDataSource); final CustomReportDataSource reportDataSource = argumentCaptor.getValue(); assertNotNull(reportDataSource); assertNotNull(reportDataSource.getPropertyMap()); assertEquals(reportDataSource.getPropertyMap().get("password"), expectedPassword); }