use of org.hisp.dhis.metadata.version.MetadataVersion in project dhis2-core by dhis2.
the class DefaultMetadataSyncServiceTest method testShouldThrowExceptionWhenSnapshotReturnsNullForGivenVersion.
@Test
public void testShouldThrowExceptionWhenSnapshotReturnsNullForGivenVersion() throws DhisVersionMismatchException {
MetadataSyncParams syncParams = Mockito.mock(MetadataSyncParams.class);
MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.ATOMIC);
when(syncParams.getVersion()).thenReturn(metadataVersion);
when(metadataVersionDelegate.downloadMetadataVersionSnapshot(metadataVersion)).thenReturn(null);
expectedException.expect(MetadataSyncServiceException.class);
expectedException.expectMessage("Metadata snapshot can't be null.");
metadataSyncService.doMetadataSync(syncParams);
}
use of org.hisp.dhis.metadata.version.MetadataVersion in project dhis2-core by dhis2.
the class DefaultMetadataSyncServiceTest method testShouldThrowExceptionWhenDHISVersionsMismatch.
@Test
public void testShouldThrowExceptionWhenDHISVersionsMismatch() throws DhisVersionMismatchException {
MetadataSyncParams syncParams = Mockito.mock(MetadataSyncParams.class);
MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.ATOMIC);
String expectedMetadataSnapshot = "{\"date\":\"2016-05-24T05:27:25.128+0000\"}";
when(syncParams.getVersion()).thenReturn(metadataVersion);
when(metadataVersionDelegate.downloadMetadataVersionSnapshot(metadataVersion)).thenReturn(expectedMetadataSnapshot);
when(metadataSyncDelegate.shouldStopSync(expectedMetadataSnapshot)).thenReturn(true);
when(metadataVersionService.isMetadataPassingIntegrity(metadataVersion, expectedMetadataSnapshot)).thenReturn(true);
expectedException.expect(DhisVersionMismatchException.class);
expectedException.expectMessage("Metadata sync failed because your version of DHIS does not match the master version");
metadataSyncService.doMetadataSync(syncParams);
}
use of org.hisp.dhis.metadata.version.MetadataVersion in project dhis2-core by dhis2.
the class MetadataVersionController method getMetaDataVersionHistory.
//Gets the list of all versions in between the passed version name and latest system version
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/history", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getMetaDataVersionHistory(@RequestParam(value = "baseline", required = false) String versionName) throws MetadataVersionException, BadRequestException {
List<MetadataVersion> allVersionsInBetween = new ArrayList<>();
boolean enabled = isMetadataVersioningEnabled();
try {
if (!enabled) {
throw new BadRequestException("Metadata versioning is not enabled for this instance.");
}
Date startDate;
if (versionName == null || versionName.isEmpty()) {
MetadataVersion initialVersion = versionService.getInitialVersion();
if (initialVersion == null) {
return versionService.getMetadataVersionsAsNode(allVersionsInBetween);
}
startDate = initialVersion.getCreated();
} else {
startDate = versionService.getCreatedDate(versionName);
}
if (startDate == null) {
throw new MetadataVersionException("There is no such metadata version. The latest version is Version " + versionService.getCurrentVersion().getName());
}
Date endDate = new Date();
allVersionsInBetween = versionService.getAllVersionsInBetween(startDate, endDate);
if (allVersionsInBetween != null) {
//now remove the baseline version details
for (Iterator<MetadataVersion> iterator = allVersionsInBetween.iterator(); iterator.hasNext(); ) {
MetadataVersion m = iterator.next();
if (m.getName().equals(versionName)) {
iterator.remove();
break;
}
}
if (!allVersionsInBetween.isEmpty()) {
return versionService.getMetadataVersionsAsNode(allVersionsInBetween);
}
}
} catch (MetadataVersionServiceException ex) {
throw new MetadataVersionException(ex.getMessage(), ex);
}
return null;
}
use of org.hisp.dhis.metadata.version.MetadataVersion in project dhis2-core by dhis2.
the class MetadataVersionController method createSystemVersion.
//Creates version in versioning table, exports the metadata and saves the snapshot in datastore
@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/create", method = RequestMethod.POST, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public MetadataVersion createSystemVersion(@RequestParam(value = "type") VersionType versionType) throws MetadataVersionException, BadRequestException {
MetadataVersion versionToReturn = null;
boolean enabled = isMetadataVersioningEnabled();
try {
if (!enabled) {
throw new BadRequestException("Metadata versioning is not enabled for this instance.");
}
synchronized (versionService) {
versionService.saveVersion(versionType);
versionToReturn = versionService.getCurrentVersion();
return versionToReturn;
}
} catch (MetadataVersionServiceException ex) {
throw new MetadataVersionException("Unable to create version in system. " + ex.getMessage());
}
}
use of org.hisp.dhis.metadata.version.MetadataVersion in project dhis2-core by dhis2.
the class DefaultMetadataSyncService method getParamsFromMap.
@Override
public MetadataSyncParams getParamsFromMap(Map<String, List<String>> parameters) {
List<String> versionName = getVersionsFromParams(parameters);
MetadataSyncParams syncParams = new MetadataSyncParams();
syncParams.setImportParams(new MetadataImportParams());
String versionNameStr = versionName.get(0);
if (StringUtils.isNotEmpty(versionNameStr)) {
MetadataVersion version;
try {
version = metadataVersionDelegate.getRemoteMetadataVersion(versionNameStr);
} catch (MetadataVersionServiceException e) {
throw new MetadataSyncServiceException(e.getMessage(), e);
}
if (version == null) {
throw new MetadataSyncServiceException("The MetadataVersion could not be fetched from the remote server for the versionName: " + versionNameStr);
}
syncParams.setVersion(version);
}
syncParams.setParameters(parameters);
return syncParams;
}
Aggregations