diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c1d14a8..f84473cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Note: version releases in the 0.x.y range may introduce breaking changes. - Flat : added more test and devise fixes ( see https://github.com/ehrbase/openEHR_SDK/pull/291) - Upgrade to Archie 1.0.4 ([#292](https://github.com/ehrbase/openEHR_SDK/pull/292)) +- cleanup created templates (https://github.com/ehrbase/openEHR_SDK/issues/298) ### Fixed diff --git a/client/src/main/java/org/ehrbase/client/openehrclient/AdminTemplateEndpoint.java b/client/src/main/java/org/ehrbase/client/openehrclient/AdminTemplateEndpoint.java new file mode 100644 index 000000000..07b4b9641 --- /dev/null +++ b/client/src/main/java/org/ehrbase/client/openehrclient/AdminTemplateEndpoint.java @@ -0,0 +1,5 @@ +package org.ehrbase.client.openehrclient; + +public interface AdminTemplateEndpoint { + int delete(String templateId); +} diff --git a/client/src/main/java/org/ehrbase/client/openehrclient/OpenEhrClient.java b/client/src/main/java/org/ehrbase/client/openehrclient/OpenEhrClient.java index 93588f134..4d8fea61c 100644 --- a/client/src/main/java/org/ehrbase/client/openehrclient/OpenEhrClient.java +++ b/client/src/main/java/org/ehrbase/client/openehrclient/OpenEhrClient.java @@ -59,6 +59,8 @@ public interface OpenEhrClient { */ AdminEhrEndpoint adminEhrEndpoint(); + AdminTemplateEndpoint adminTemplateEndpoint(); + /** * Get the {@link VersionedCompositionEndpoint}. * diff --git a/client/src/main/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestAdminTemplateEndpoint.java b/client/src/main/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestAdminTemplateEndpoint.java new file mode 100644 index 000000000..f9ca57a1f --- /dev/null +++ b/client/src/main/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestAdminTemplateEndpoint.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020 Christian Chevalley (Hannover Medical School) and Vitasystems GmbH + * + * This file is part of project EHRbase + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package org.ehrbase.client.openehrclient.defaultrestclient; + +import org.apache.http.HttpResponse; +import org.ehrbase.client.openehrclient.AdminTemplateEndpoint; + +import java.net.URI; + +/** + * Requires that the Admin endpoint is active. + * + * Parameters in .yml configuration matching the runtime context as: + * + * admin-api: + * active: true + * allowDeleteAll: true + */ +public class DefaultRestAdminTemplateEndpoint implements AdminTemplateEndpoint { + + public static final String ADMIN_TEMPLATE_PATH = "rest/admin/template/"; + + private final DefaultRestClient defaultRestClient; + + public DefaultRestAdminTemplateEndpoint(DefaultRestClient defaultRestClient) { + this.defaultRestClient = defaultRestClient; + } + + + @Override + public int delete(String templateId) { + if (templateId == null) + return 0; + + URI uri = defaultRestClient.getConfig().getBaseUri().resolve(ADMIN_TEMPLATE_PATH + templateId); + + HttpResponse response = + defaultRestClient.internalDelete( + uri, + null); + + return response.getStatusLine().getStatusCode(); + } +} diff --git a/client/src/main/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestClient.java b/client/src/main/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestClient.java index ab15f40fb..132001559 100644 --- a/client/src/main/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestClient.java +++ b/client/src/main/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestClient.java @@ -332,6 +332,11 @@ public AdminEhrEndpoint adminEhrEndpoint() { return new DefaultRestAdminEhrEndpoint(this); } + @Override + public AdminTemplateEndpoint adminTemplateEndpoint() { + return new DefaultRestAdminTemplateEndpoint(this); + } + @Override public VersionedCompositionEndpoint versionedCompositionEndpoint(UUID ehrId) { return new DefaultRestVersionedCompositionEndpoint(this, ehrId); diff --git a/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestTemplateEndpointIT.java b/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestTemplateEndpointIT.java index b1f67d77d..e2085e575 100644 --- a/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestTemplateEndpointIT.java +++ b/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/DefaultRestTemplateEndpointIT.java @@ -22,6 +22,7 @@ import org.ehrbase.response.ehrscape.TemplateMetaDataDto; import org.ehrbase.response.openehr.TemplatesResponseData; import org.ehrbase.test_data.operationaltemplate.OperationalTemplateTestData; +import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; import org.openehr.schemas.v1.OPERATIONALTEMPLATE; @@ -45,12 +46,20 @@ public class DefaultRestTemplateEndpointIT { private static final String TEMPLATE_NAME_PREFIX = "ehrbase_blood_pressure_simple.de.v"; + private String templateId = null; //global used for teardown + @BeforeClass public static void setup() throws URISyntaxException { restClient = DefaultRestClientTestHelper.setupDefaultRestClient(); restClientWithDefaultTemplateProvider = DefaultRestClientTestHelper.setupRestClientWithDefaultTemplateProvider(); } + @After + public void tearDown(){ + //delete template with random version id (mostly) + restClient.adminTemplateEndpoint().delete(templateId); + } + @Test public void testFindTemplate() { Optional operationalTemplate = restClient.templateEndpoint() @@ -82,7 +91,7 @@ public void testCreateTemplate() throws IOException, XmlException { OPERATIONALTEMPLATE template = TemplateDocument.Factory.parse(OperationalTemplateTestData.BLOOD_PRESSURE_SIMPLE.getStream()).getTemplate(); - String templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); + templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); template.getTemplateId().setValue(templateId); template.getUid().setValue(UUID.randomUUID().toString()); @@ -95,7 +104,7 @@ public void testCreateTemplateWithDefaultTemplateProvider() throws IOException, OPERATIONALTEMPLATE template = TemplateDocument.Factory.parse(OperationalTemplateTestData.BLOOD_PRESSURE_SIMPLE.getStream()).getTemplate(); - String templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); + templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); template.getTemplateId().setValue(templateId); template.getUid().setValue(UUID.randomUUID().toString()); @@ -106,7 +115,7 @@ public void testCreateTemplateWithDefaultTemplateProvider() throws IOException, @Test public void testFindAllTemplates() throws IOException, XmlException { - String templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); + templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); String savedTemplateId = uploadTemplate(restClient,OperationalTemplateTestData.BLOOD_PRESSURE_SIMPLE, templateId); assertThat(savedTemplateId).isEqualTo(templateId); @@ -121,7 +130,7 @@ public void testFindAllTemplates() throws IOException, XmlException { @Test public void testFindAllTemplatesWithDefaultTemplateProvider() throws IOException, XmlException { - String templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); + templateId = String.format("%s%s", TEMPLATE_NAME_PREFIX, RandomStringUtils.randomNumeric(10)); String savedTemplateId = uploadTemplate(restClientWithDefaultTemplateProvider,OperationalTemplateTestData.BLOOD_PRESSURE_SIMPLE, templateId); assertThat(savedTemplateId).isEqualTo(templateId);