Remove org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder and fix deprecated Specification.where (#2760)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-10-16 16:01:29 +03:00
committed by GitHub
parent 8da851a75a
commit 4b3c3cc870
7 changed files with 25 additions and 193 deletions

View File

@@ -56,6 +56,7 @@ import org.eclipse.hawkbit.repository.exception.IncompleteDistributionSetExcepti
import org.eclipse.hawkbit.repository.exception.InvalidDistributionSetException;
import org.eclipse.hawkbit.repository.exception.MultiAssignmentIsNotEnabledException;
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
import org.eclipse.hawkbit.repository.jpa.JpaManagementHelper;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction_;
@@ -65,7 +66,6 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget_;
import org.eclipse.hawkbit.repository.jpa.repository.TargetRepository;
import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetSpecification;
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.ActionStatusCreate;
import org.eclipse.hawkbit.repository.model.Action.ActionType;
@@ -1354,9 +1354,9 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest {
List<? extends DistributionSet> allFoundDS = distributionSetManagement.findAll(PAGE).getContent();
assertThat(allFoundDS).as("no ds should be founded").isEmpty();
assertThat(distributionSetRepository.findAll(SpecificationsBuilder.combineWithAnd(Arrays
.asList(DistributionSetSpecification.isDeleted(true), DistributionSetSpecification.isCompleted(true))),
PAGE).getContent()).as("wrong size of founded ds").hasSize(noOfDistributionSets);
assertThat(distributionSetRepository.findAll(JpaManagementHelper.combineWithAnd(
List.of(DistributionSetSpecification.isDeleted(true), DistributionSetSpecification.isCompleted(true))), PAGE).getContent())
.as("wrong size of founded ds").hasSize(noOfDistributionSets);
IntStream.range(0, deploymentResult.getDistributionSets().size()).forEach(i -> testdataFactory.sendUpdateActionStatusToTargets(
deploymentResult.getDeployedTargets(), Status.FINISHED, Collections.singletonList("blabla alles gut")));
@@ -1367,9 +1367,9 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest {
// successfully and no activeAction is referring to created distribution sets
allFoundDS = distributionSetManagement.findAll(pageRequest).getContent();
assertThat(allFoundDS).as("no ds should be founded").isEmpty();
assertThat(distributionSetRepository.findAll(SpecificationsBuilder.combineWithAnd(Arrays
.asList(DistributionSetSpecification.isDeleted(true), DistributionSetSpecification.isCompleted(true))),
PAGE).getContent()).as("wrong size of founded ds").hasSize(noOfDistributionSets);
assertThat(distributionSetRepository.findAll(JpaManagementHelper.combineWithAnd(
List.of(DistributionSetSpecification.isDeleted(true), DistributionSetSpecification.isCompleted(true))), PAGE).getContent())
.as("wrong size of founded ds").hasSize(noOfDistributionSets);
}
/**

View File

@@ -1,111 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.jpa.specifications;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.junit.jupiter.api.Test;
import org.springframework.data.jpa.domain.Specification;
/**
* Feature: Unit Tests - Repository<br/>
* Story: Specifications builder
*/
class SpecificationsBuilderTest {
/**
* Test the combination of specs on an empty list which returns null
*/
@Test
void combineWithAndEmptyList() {
final List<Specification<Object>> specList = Collections.emptyList();
assertThat(SpecificationsBuilder.combineWithAnd(specList)).isNull();
}
/**
* Test the combination of specs on an immutable list with one entry
*/
@Test
void combineWithAndSingleImmutableList() {
final Specification<Object> spec = (root, query, cb) -> cb.equal(root.get("field1"), "testValue");
final List<Specification<Object>> specList = Collections.singletonList(spec);
final Specification<Object> specifications = SpecificationsBuilder.combineWithAnd(specList);
assertThat(specifications).as("Specifications").isNotNull();
// mocks to call toPredicate on specifications
final CriteriaBuilder criteriaBuilder = mock(CriteriaBuilder.class);
final Path field1 = mock(Path.class);
final Predicate equalPredicate = mock(Predicate.class);
final CriteriaQuery<Object[]> query = mock(CriteriaQuery.class);
final Root<Object> root = mock(Root.class);
when(criteriaBuilder.equal(any(Expression.class), anyString())).thenReturn(equalPredicate);
when(root.get("field1")).thenReturn(field1);
final Predicate predicate = specifications.toPredicate(root, query, criteriaBuilder);
assertThat(predicate).isEqualTo(equalPredicate);
}
/**
* Test the combination of specs on a list with multiple entries
*/
@Test
void combineWithAndList() {
final Specification<Object> spec1 = (root, query, cb) -> cb.equal(root.get("field1"), "testValue1");
final Specification<Object> spec2 = (root, query, cb) -> cb.equal(root.get("field2"), "testValue2");
final List<Specification<Object>> specList = new ArrayList<>(2);
specList.add(spec1);
specList.add(spec2);
final Specification<Object> specifications = SpecificationsBuilder.combineWithAnd(specList);
assertThat(specifications).as("Specifications").isNotNull();
// mocks to call toPredicate on specifications
final CriteriaBuilder criteriaBuilder = mock(CriteriaBuilder.class);
final Path field1 = mock(Path.class);
final Path field2 = mock(Path.class);
final Predicate equalPredicate1 = mock(Predicate.class);
final Predicate equalPredicate2 = mock(Predicate.class);
final Predicate combinedPredicate = mock(Predicate.class);
final CriteriaQuery<Object[]> query = mock(CriteriaQuery.class);
final Root<Object> root = mock(Root.class);
when(criteriaBuilder.equal(any(Path.class), eq("testValue1"))).thenReturn(equalPredicate1);
when(criteriaBuilder.equal(any(Path.class), eq("testValue2"))).thenReturn(equalPredicate2);
when(criteriaBuilder.and(equalPredicate1, equalPredicate2)).thenReturn(combinedPredicate);
when(root.get("field1")).thenReturn(field1);
when(root.get("field2")).thenReturn(field2);
final Predicate predicate = specifications.toPredicate(root, query, criteriaBuilder);
assertThat(predicate).as("Combined predicate").isEqualTo(combinedPredicate);
}
}