-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: dispatch contractdefinition events (#1538)
* core: dispatch contractdefinition events * PR remarks
- Loading branch information
Showing
30 changed files
with
404 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...nector/api/datamanagement/contractdefinition/service/EventContractDefinitionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.dataspaceconnector.api.datamanagement.contractdefinition.service; | ||
|
||
import org.eclipse.dataspaceconnector.spi.contract.definition.observe.ContractDefinitionListener; | ||
import org.eclipse.dataspaceconnector.spi.event.EventRouter; | ||
import org.eclipse.dataspaceconnector.spi.event.contractdefinition.ContractDefinitionCreated; | ||
import org.eclipse.dataspaceconnector.spi.event.contractdefinition.ContractDefinitionDeleted; | ||
import org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractDefinition; | ||
|
||
import java.time.Clock; | ||
|
||
/** | ||
* Listener responsible for creating and publishing events regarding ContractDefinition state changes | ||
*/ | ||
public class EventContractDefinitionListener implements ContractDefinitionListener { | ||
private final Clock clock; | ||
private final EventRouter eventRouter; | ||
|
||
public EventContractDefinitionListener(Clock clock, EventRouter eventRouter) { | ||
this.clock = clock; | ||
this.eventRouter = eventRouter; | ||
} | ||
|
||
@Override | ||
public void created(ContractDefinition contractDefinition) { | ||
var event = ContractDefinitionCreated.Builder.newInstance() | ||
.contractDefinitionId(contractDefinition.getId()) | ||
.at(clock.millis()) | ||
.build(); | ||
|
||
eventRouter.publish(event); | ||
} | ||
|
||
@Override | ||
public void deleted(ContractDefinition contractDefinition) { | ||
var event = ContractDefinitionDeleted.Builder.newInstance() | ||
.contractDefinitionId(contractDefinition.getId()) | ||
.at(clock.millis()) | ||
.build(); | ||
|
||
eventRouter.publish(event); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...or/api/datamanagement/contractdefinition/service/ContractDefinitionEventDispatchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.dataspaceconnector.api.datamanagement.contractdefinition.service; | ||
|
||
import org.eclipse.dataspaceconnector.junit.extensions.EdcExtension; | ||
import org.eclipse.dataspaceconnector.spi.asset.AssetSelectorExpression; | ||
import org.eclipse.dataspaceconnector.spi.event.EventRouter; | ||
import org.eclipse.dataspaceconnector.spi.event.EventSubscriber; | ||
import org.eclipse.dataspaceconnector.spi.event.contractdefinition.ContractDefinitionCreated; | ||
import org.eclipse.dataspaceconnector.spi.event.contractdefinition.ContractDefinitionDeleted; | ||
import org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractDefinition; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.mockito.ArgumentMatchers.isA; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(EdcExtension.class) | ||
public class ContractDefinitionEventDispatchTest { | ||
|
||
private final EventSubscriber eventSubscriber = mock(EventSubscriber.class); | ||
|
||
@Test | ||
void shouldDispatchEventOnContractDefinitionCreationAndDeletion(ContractDefinitionService service, EventRouter eventRouter) { | ||
eventRouter.register(eventSubscriber); | ||
var contractDefinition = ContractDefinition.Builder.newInstance() | ||
.id(UUID.randomUUID().toString()) | ||
.contractPolicyId(UUID.randomUUID().toString()) | ||
.accessPolicyId(UUID.randomUUID().toString()) | ||
.selectorExpression(AssetSelectorExpression.SELECT_ALL) | ||
.build(); | ||
|
||
service.create(contractDefinition); | ||
|
||
await().untilAsserted(() -> verify(eventSubscriber).on(isA(ContractDefinitionCreated.class))); | ||
|
||
service.delete(contractDefinition.getId()); | ||
|
||
await().untilAsserted(() -> verify(eventSubscriber).on(isA(ContractDefinitionDeleted.class))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.