Search in sources :

Example 1 with ContractNegotiation

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation in project DataSpaceConnector by eclipse-dataspaceconnector.

the class InMemoryContractNegotiationStoreTest method verifyNextForState_avoidsStarvation.

@Test
void verifyNextForState_avoidsStarvation() throws InterruptedException {
    for (int i = 0; i < 10; i++) {
        ContractNegotiation negotiation = createNegotiation("test-negotiation-" + i);
        negotiation.transitionInitial();
        store.save(negotiation);
    }
    var list1 = store.nextForState(ContractNegotiationStates.INITIAL.code(), 5);
    // simulate a short delay to generate different timestamps
    Thread.sleep(50);
    list1.forEach(tp -> store.save(tp));
    var list2 = store.nextForState(ContractNegotiationStates.INITIAL.code(), 5);
    assertThat(list1).isNotEqualTo(list2).doesNotContainAnyElementsOf(list2);
}
Also used : ContractNegotiation(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation) Test(org.junit.jupiter.api.Test)

Example 2 with ContractNegotiation

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation in project DataSpaceConnector by eclipse-dataspaceconnector.

the class InMemoryContractNegotiationStoreTest method verifyOrderingByTimestamp.

@Test
void verifyOrderingByTimestamp() {
    for (int i = 0; i < 100; i++) {
        ContractNegotiation negotiation = createNegotiation("test-negotiation-" + i);
        negotiation.transitionInitial();
        store.save(negotiation);
    }
    List<ContractNegotiation> processes = store.nextForState(ContractNegotiationStates.INITIAL.code(), 50);
    assertThat(processes).hasSize(50);
    assertThat(processes).allMatch(p -> p.getStateTimestamp() > 0);
}
Also used : ContractNegotiation(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation) Test(org.junit.jupiter.api.Test)

Example 3 with ContractNegotiation

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation in project DataSpaceConnector by eclipse-dataspaceconnector.

the class InMemoryContractNegotiationStoreTest method verifyMultipleRequest.

@Test
void verifyMultipleRequest() {
    String id1 = UUID.randomUUID().toString();
    ContractNegotiation negotiation1 = createNegotiation(id1);
    negotiation1.transitionInitial();
    store.save(negotiation1);
    String id2 = UUID.randomUUID().toString();
    ContractNegotiation negotiation2 = createNegotiation(id2);
    negotiation2.transitionInitial();
    store.save(negotiation2);
    ContractNegotiation found1 = store.find(id1);
    assertNotNull(found1);
    ContractNegotiation found2 = store.find(id2);
    assertNotNull(found2);
    var found = store.nextForState(ContractNegotiationStates.INITIAL.code(), 3);
    assertEquals(2, found.size());
}
Also used : ContractNegotiation(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation) Test(org.junit.jupiter.api.Test)

Example 4 with ContractNegotiation

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation in project DataSpaceConnector by eclipse-dataspaceconnector.

the class InMemoryContractNegotiationStoreTest method verifyMultipleRequest.

@Test
void verifyMultipleRequest() {
    String id1 = UUID.randomUUID().toString();
    ContractNegotiation negotiation1 = createNegotiation(id1);
    negotiation1.transitionInitial();
    store.save(negotiation1);
    String id2 = UUID.randomUUID().toString();
    ContractNegotiation negotiation2 = createNegotiation(id2);
    negotiation2.transitionInitial();
    store.save(negotiation2);
    ContractNegotiation found1 = store.find(id1);
    assertNotNull(found1);
    ContractNegotiation found2 = store.find(id2);
    assertNotNull(found2);
    var found = store.nextForState(INITIAL.code(), 3);
    assertEquals(2, found.size());
}
Also used : ContractNegotiation(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation) Test(org.junit.jupiter.api.Test)

Example 5 with ContractNegotiation

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation in project DataSpaceConnector by eclipse-dataspaceconnector.

the class InMemoryContractNegotiationStore method delete.

@Override
public void delete(String processId) {
    lockManager.writeLock(() -> {
        ContractNegotiation process = negotiationById.remove(processId);
        if (process != null) {
            var tempCache = new HashMap<Integer, List<ContractNegotiation>>();
            stateCache.forEach((key, value) -> {
                var list = value.stream().filter(p -> !p.getId().equals(processId)).collect(Collectors.toCollection(ArrayList::new));
                tempCache.put(key, list);
            });
            stateCache.clear();
            stateCache.putAll(tempCache);
            negotiationByCorrelationId.remove(process.getCorrelationId());
            if (process.getContractAgreement() != null) {
                contractAgreements.remove(process.getContractAgreement().getId());
            }
        }
        return null;
    });
}
Also used : ContractAgreement(org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreement) ContractNegotiationStore(org.eclipse.dataspaceconnector.spi.contract.negotiation.store.ContractNegotiationStore) QueryResolver(org.eclipse.dataspaceconnector.spi.query.QueryResolver) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Collectors(java.util.stream.Collectors) QuerySpec(org.eclipse.dataspaceconnector.spi.query.QuerySpec) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Map(java.util.Map) ReflectionBasedQueryResolver(org.eclipse.dataspaceconnector.spi.query.ReflectionBasedQueryResolver) LockManager(org.eclipse.dataspaceconnector.common.concurrency.LockManager) ContractNegotiation(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) ContractNegotiation(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation)

Aggregations

ContractNegotiation (org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation)16 Test (org.junit.jupiter.api.Test)11 Comparator (java.util.Comparator)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 QuerySpec (org.eclipse.dataspaceconnector.spi.query.QuerySpec)5 ContractAgreement (org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreement)5 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)4 Collectors.toList (java.util.stream.Collectors.toList)4 Stream (java.util.stream.Stream)4 LockManager (org.eclipse.dataspaceconnector.common.concurrency.LockManager)4 ContractNegotiationStore (org.eclipse.dataspaceconnector.spi.contract.negotiation.store.ContractNegotiationStore)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 SortOrder (org.eclipse.dataspaceconnector.spi.query.SortOrder)3 Duration (java.time.Duration)2