Search in sources :

Example 11 with MetadataVersion

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);
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 12 with MetadataVersion

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);
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 13 with MetadataVersion

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;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) ArrayList(java.util.ArrayList) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with MetadataVersion

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());
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 15 with MetadataVersion

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;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams)

Aggregations

MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)53 DhisSpringTest (org.hisp.dhis.DhisSpringTest)31 Test (org.junit.Test)31 IntegrationTest (org.hisp.dhis.IntegrationTest)15 Date (java.util.Date)14 ArrayList (java.util.ArrayList)12 AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)12 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)10 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)6 MetadataRetryContext (org.hisp.dhis.dxf2.metadata.tasks.MetadataRetryContext)6 IOException (java.io.IOException)5 MetadataSyncServiceException (org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException)5 HttpResponse (org.apache.http.HttpResponse)4 ComplexNode (org.hisp.dhis.node.types.ComplexNode)3 RootNode (org.hisp.dhis.node.types.RootNode)3 BadRequestException (org.hisp.dhis.webapi.controller.exception.BadRequestException)3 MetadataVersionException (org.hisp.dhis.webapi.controller.exception.MetadataVersionException)3 Before (org.junit.Before)3