Search in sources :

Example 96 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class MetadataVersionController method getMetaDataVersion.

//Gets the version by versionName or latest system version
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT, method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public MetadataVersion getMetaDataVersion(@RequestParam(value = "versionName", required = false) String versionName) throws MetadataVersionException, BadRequestException {
    MetadataVersion versionToReturn = null;
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        if (StringUtils.isNotEmpty(versionName)) {
            versionToReturn = versionService.getVersionByName(versionName);
            if (versionToReturn == null) {
                throw new MetadataVersionException("No metadata version with name " + versionName + " exists. Please check again later.");
            }
        } else {
            versionToReturn = versionService.getCurrentVersion();
            if (versionToReturn == null) {
                throw new MetadataVersionException("No metadata versions exist. Please check again later.");
            }
        }
        return versionToReturn;
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException("Exception occurred while getting metadata version." + (StringUtils.isNotEmpty(versionName) ? versionName : " ") + ex.getMessage(), ex);
    }
}
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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 97 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class MetadataVersionController method downloadGZipVersion.

//endpoint to download metadata in gzip format
@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/{versionName}/data.gz", method = RequestMethod.GET, produces = "*/*")
public void downloadGZipVersion(@PathVariable("versionName") String versionName, HttpServletResponse response) throws MetadataVersionException, IOException, BadRequestException {
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_GZIP, CacheStrategy.NO_CACHE, "metadata.json.gz", true);
        response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
        String versionData = versionService.getVersionData(versionName);
        if (versionData == null) {
            throw new MetadataVersionException("No metadata version snapshot found for the given version " + versionName);
        }
        GZIPOutputStream gos = new GZIPOutputStream(response.getOutputStream());
        gos.write(versionData.getBytes(StandardCharsets.UTF_8));
        gos.close();
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException("Unable to download version from system: " + versionName + ex.getMessage());
    }
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) GZIPOutputStream(java.util.zip.GZIPOutputStream) 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)

Example 98 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithServerError.

@Test
public void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithServerError() throws Exception {
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    String url = "http://localhost:9080/api/metadata/version?versionName=Version_Name";
    when(metadataSystemSettingService.getVersionDetailsUrl("testversion")).thenReturn(url);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    HttpResponse httpResponse = mock(HttpResponse.class);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.GATEWAY_TIMEOUT.value());
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    PowerMockito.when(HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromJson(response, MetadataVersion.class)).thenReturn(metadataVersion);
    expectedException.expect(MetadataVersionServiceException.class);
    expectedException.expectMessage("Server Error. Http call failed with status code: " + HttpStatus.GATEWAY_TIMEOUT.value() + " Caused by: " + response);
    metadataVersionDelegate.getRemoteMetadataVersion("testVersion");
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) HttpResponse(org.apache.http.HttpResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 99 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldReturnEmptyMetadataDifference.

@Test
public void testShouldReturnEmptyMetadataDifference() throws Exception {
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    String url = "http://localhost:9080/api/metadata/version/history?baseline=testVersion";
    when(metadataSystemSettingService.getMetaDataDifferenceURL("testVersion")).thenReturn(url);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.BAD_REQUEST.value());
    PowerMockito.when(HttpUtils.httpGET(baselineUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromMetadataVersion(any(InputStream.class), eq(RenderFormat.JSON))).thenThrow(new IOException(""));
    expectedException.expect(MetadataVersionServiceException.class);
    expectedException.expectMessage("Client Error. Http call failed with status code: 400 Caused by: " + dhisHttpResponse.getResponse());
    metadataVersionDelegate.getMetaDataDifference(metadataVersion);
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) InputStream(org.omg.CORBA.portable.InputStream) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 100 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenRenderServiceThrowsExceptionWhileGettingMetadataDifference.

@Test
public void testShouldThrowExceptionWhenRenderServiceThrowsExceptionWhileGettingMetadataDifference() throws Exception {
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    String url = "http://localhost:9080/api/metadata/version/history?baseline=testVersion";
    when(metadataSystemSettingService.getMetaDataDifferenceURL("testVersion")).thenReturn(url);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.value());
    PowerMockito.when(HttpUtils.httpGET(baselineUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromMetadataVersion(any(InputStream.class), eq(RenderFormat.JSON))).thenThrow(new IOException(""));
    expectedException.expect(MetadataVersionServiceException.class);
    expectedException.expectMessage("Exception occurred while trying to do JSON conversion. Caused by: ");
    metadataVersionDelegate.getMetaDataDifference(metadataVersion);
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) InputStream(org.omg.CORBA.portable.InputStream) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DhisSpringTest (org.hisp.dhis.DhisSpringTest)55 Test (org.junit.Test)55 List (java.util.List)46 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)46 ClassPathResource (org.springframework.core.io.ClassPathResource)42 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)37 DataElement (org.hisp.dhis.dataelement.DataElement)25 User (org.hisp.dhis.user.User)20 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)19 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)19 Metadata (com.google.android.exoplayer2.metadata.Metadata)18 DataSet (org.hisp.dhis.dataset.DataSet)15 ArrayList (java.util.ArrayList)14 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 IOException (java.io.IOException)10 Metadata (org.hisp.dhis.dxf2.metadata.Metadata)10 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)10 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)10 IntegrationTest (org.hisp.dhis.IntegrationTest)9