Skip to content

Commit

Permalink
fix: deprovision phase for provision-http (#4660)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored Dec 10, 2024
1 parent 60a20fe commit 7eb2ef7
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,22 @@ public void resumeTransfer(String id) {
.statusCode(204);
}

public void terminateTransfer(String id) {
var requestBodyBuilder = createObjectBuilder()
.add(CONTEXT, createObjectBuilder().add(VOCAB, EDC_NAMESPACE))
.add(TYPE, "TerminateTransfer")
.add("reason", "any reason");

managementEndpoint.baseRequest()
.contentType(JSON)
.body(requestBodyBuilder.build())
.when()
.post("/v3/transferprocesses/{id}/terminate", id)
.then()
.log().ifError()
.statusCode(204);
}

/**
* Get current state of a contract negotiation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ public HttpProviderProvisioner(ProvisionerConfiguration configuration,

@Override
public boolean canProvision(ResourceDefinition resourceDefinition) {
return resourceDefinition instanceof HttpProviderResourceDefinition && dataAddressType.equals(((HttpProviderResourceDefinition) resourceDefinition).getDataAddressType());
return resourceDefinition instanceof HttpProviderResourceDefinition definition && dataAddressType.equals(definition.getDataAddressType());
}

@Override
public boolean canDeprovision(ProvisionedResource provisionedResource) {
return provisionedResource instanceof HttpProvisionedContentResource &&
dataAddressType.equals(((HttpProvisionedContentResource) provisionedResource).getDataAddress().getType());
return provisionedResource instanceof HttpProvisionedContentResource;
}

@Override
Expand All @@ -100,10 +99,8 @@ public CompletableFuture<StatusResult<ProvisionResponse>> provision(HttpProvider
if (response.isSuccessful()) {
return completedFuture(StatusResult.success(ProvisionResponse.Builder.newInstance().inProcess(true).build()));
} else if (response.code() >= 500 && response.code() <= 504) {
// retry
return completedFuture(StatusResult.failure(ResponseStatus.ERROR_RETRY, "HttpProviderProvisioner: received error code: " + response.code()));
} else {
// fatal error
return completedFuture(StatusResult.failure(ResponseStatus.FATAL_ERROR, "HttpProviderProvisioner: received fatal error code: " + response.code()));
}
} catch (IOException e) {
Expand All @@ -128,15 +125,13 @@ public CompletableFuture<StatusResult<DeprovisionedResource>> deprovision(HttpPr
try (var response = httpClient.execute(request)) {
if (response.code() == 200) {
var deprovisionedResource = DeprovisionedResource.Builder.newInstance()
.provisionedResourceId(provisionedResource.getTransferProcessId())
.provisionedResourceId(provisionedResource.getId())
.inProcess(true)
.build();
return completedFuture(StatusResult.success(deprovisionedResource));
} else if (response.code() >= 500 && response.code() <= 504) {
// retry
return completedFuture(StatusResult.failure(ResponseStatus.ERROR_RETRY, "Received error code: " + response.code()));
} else {
// fatal error
return completedFuture(StatusResult.failure(ResponseStatus.FATAL_ERROR, "Received fatal error code: " + response.code()));
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public String getResourceDefinitionId() {
return resourceDefinitionId;
}

public Type getType() {
return type;
}

public enum Type {
@JsonProperty("provision")
PROVISION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.MalformedURLException;
import java.net.URL;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.connector.controlplane.provision.http.config.ProvisionerConfiguration.ProvisionerType.PROVIDER;
import static org.eclipse.edc.http.client.testfixtures.HttpTestUtils.testHttpClient;
Expand All @@ -39,8 +40,9 @@
import static org.mockito.Mockito.when;

class HttpProviderProvisionerTest {

private final Interceptor delegate = mock();
private HttpProviderProvisioner provisioner;
private Interceptor delegate;

@BeforeEach
void setUp() throws MalformedURLException {
Expand All @@ -52,7 +54,6 @@ void setUp() throws MalformedURLException {
.endpoint(new URL("http://bar.com"))
.build();

delegate = mock(Interceptor.class);
provisioner = new HttpProviderProvisioner(configuration, new URL("http://foo.com"), mock(PolicyEngine.class),
testHttpClient(delegate), new ObjectMapper(), mock(Monitor.class));
}
Expand All @@ -75,18 +76,6 @@ void verifyCanProvision() {
void verifyCanDeprovision() {
assertThat(provisioner.canDeprovision(createProvisionedResource())).isTrue();
assertThat(provisioner.canDeprovision(new TestProvisionedResource())).isFalse();

var dataAddress = DataAddress.Builder.newInstance().type("another-type").build();
var differentType = HttpProvisionedContentResource.Builder.newInstance()
.assetId("1")
.transferProcessId("2")
.resourceName("test")
.dataAddress(dataAddress)
.resourceDefinitionId("3")
.id("3")
.build();

assertThat(provisioner.canDeprovision(differentType)).isFalse();
}

@Test
Expand All @@ -97,10 +86,10 @@ void verifyProvisionOkAndInProcess() throws Exception {

var definition = createResourceDefinition();

var result = provisioner.provision(definition, policy).get();

assertThat(result.succeeded()).isTrue();
assertThat(result.getContent().isInProcess()).isTrue();
assertThat(provisioner.provision(definition, policy)).succeedsWithin(5, SECONDS).satisfies(result -> {
assertThat(result.succeeded()).isTrue();
assertThat(result.getContent().isInProcess()).isTrue();
});
}

@Test
Expand All @@ -111,10 +100,10 @@ void verifyProvision404Response() throws Exception {

var definition = createResourceDefinition();

var result = provisioner.provision(definition, policy).get();

assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.FATAL_ERROR);
assertThat(provisioner.provision(definition, policy)).succeedsWithin(5, SECONDS).satisfies(result -> {
assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.FATAL_ERROR);
});
}

@Test
Expand All @@ -125,10 +114,10 @@ void verifyProvisionRetryResponse() throws Exception {

var definition = createResourceDefinition();

var result = provisioner.provision(definition, policy).get();

assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.ERROR_RETRY);
assertThat(provisioner.provision(definition, policy)).succeedsWithin(5, SECONDS).satisfies(result -> {
assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.ERROR_RETRY);
});
}

@Test
Expand All @@ -139,28 +128,26 @@ void verifyDeprovisionOkAndInProcess() throws Exception {

var definition = createProvisionedResource();

var result = provisioner.deprovision(definition, policy).get();

assertThat(result.succeeded()).isTrue();
assertThat(result.getContent().isInProcess()).isTrue();
assertThat(provisioner.deprovision(definition, policy)).succeedsWithin(5, SECONDS).satisfies(result -> {
assertThat(result.succeeded()).isTrue();
assertThat(result.getContent().getProvisionedResourceId()).isEqualTo(definition.getId());
assertThat(result.getContent().isInProcess()).isTrue();
});
}


@Test
void verifyDeprovision404Response() throws Exception {
when(delegate.intercept(any())).thenAnswer((invocation -> HttpProvisionerFixtures.createResponse(404, invocation)));

var policy = Policy.Builder.newInstance().build();

var definition = createProvisionedResource();

var result = provisioner.deprovision(definition, policy).get();

assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.FATAL_ERROR);
assertThat(provisioner.deprovision(definition, policy)).succeedsWithin(5, SECONDS).satisfies(result -> {
assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.FATAL_ERROR);
});
}


@Test
void verifyDeprovisionRetryResponse() throws Exception {
when(delegate.intercept(any())).thenAnswer((invocation -> HttpProvisionerFixtures.createResponse(503, invocation)));
Expand All @@ -169,13 +156,12 @@ void verifyDeprovisionRetryResponse() throws Exception {

var definition = createProvisionedResource();

var result = provisioner.deprovision(definition, policy).get();

assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.ERROR_RETRY);
assertThat(provisioner.deprovision(definition, policy)).succeedsWithin(5, SECONDS).satisfies(result -> {
assertThat(result.failed()).isTrue();
assertThat(result.getFailure().status()).isEqualTo(ResponseStatus.ERROR_RETRY);
});
}


private HttpProviderResourceDefinition createResourceDefinition() {
return HttpProviderResourceDefinition.Builder.newInstance()
.assetId("1")
Expand All @@ -198,7 +184,6 @@ private HttpProvisionedContentResource createProvisionedResource() {
.build();
}


private static class TestResourceDefinition extends ResourceDefinition {
@Override
public <RD extends ResourceDefinition, B extends Builder<RD, B>> B toBuilder() {
Expand Down
Loading

0 comments on commit 7eb2ef7

Please sign in to comment.