DataSetNameInjection { public PersistentPreparation injectDatasetNameBasedOnId(PersistentPreparation preparation) { String dataSetName = preparation.getDataSetName(); if (dataSetName == null) { try { final String tenantId = security.getTenantId(); final Cache<String, String> tenantCache = cache.get(tenantId, this::createTenantCache); assert tenantCache != null; String dataSetId = preparation.getDataSetId(); dataSetName = tenantCache.get(dataSetId, this::getDatasetLabel); if (dataSetName != null) { preparation.setDataSetName(dataSetName); preparationRepository.add(preparation); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } } return preparation; }  PersistentPreparation injectDatasetNameBasedOnId(PersistentPreparation preparation);  }
@Test public void injectDatasetNameBasedOnId() { PersistentPreparation input = new PersistentPreparation(); String dataSetId = "1234"; input.setDataSetId(dataSetId); when(security.getTenantId()).thenReturn("tenant id"); Dataset datasetMetadata = new Dataset(); String datasetLabel = "dataset label"; datasetMetadata.setLabel(datasetLabel); when(dataCatalogClient.getMetadata(dataSetId)).thenReturn(datasetMetadata); PersistentPreparation result = dataSetNameInjection.injectDatasetNameBasedOnId(input); assertEquals(datasetLabel, result.getDataSetName()); verify(security).getTenantId(); verifyNoMoreInteractions(security); verify(securityProxy).asTechnicalUserForDataSet(); verify(securityProxy).releaseIdentity(); verifyNoMoreInteractions(securityProxy); verify(dataCatalogClient).getMetadata(dataSetId); verifyNoMoreInteractions(dataCatalogClient); verify(preparationRepository).add(input); verifyNoMoreInteractions(preparationRepository); }
@Test public void injectDatasetNameBasedOnId_alreadyHasADatasetName() { PersistentPreparation input = new PersistentPreparation(); String dataSetId = "1234"; input.setDataSetId(dataSetId); String datasetLabel = "dataset label"; input.setDataSetName(datasetLabel); PersistentPreparation result = dataSetNameInjection.injectDatasetNameBasedOnId(input); assertEquals(datasetLabel, result.getDataSetName()); verifyZeroInteractions(security); verifyZeroInteractions(securityProxy); verifyZeroInteractions(dataCatalogClient); verifyZeroInteractions(preparationRepository); }
@Test public void injectDatasetNameBasedOnId_datasetClientError() { PersistentPreparation input = new PersistentPreparation(); String dataSetId = "1234"; input.setDataSetId(dataSetId); when(security.getTenantId()).thenReturn("tenant id"); Dataset datasetMetadata = new Dataset(); String datasetLabel = "dataset label"; datasetMetadata.setLabel(datasetLabel); when(dataCatalogClient.getMetadata(dataSetId)).thenThrow(new TDPException()); PersistentPreparation result = dataSetNameInjection.injectDatasetNameBasedOnId(input); assertNull(result.getDataSetName()); verify(security).getTenantId(); verifyNoMoreInteractions(security); verify(securityProxy).asTechnicalUserForDataSet(); verify(securityProxy).releaseIdentity(); verifyNoMoreInteractions(securityProxy); verify(dataCatalogClient).getMetadata(dataSetId); verifyNoMoreInteractions(dataCatalogClient); verifyZeroInteractions(preparationRepository); }