Example usage for org.springframework.data.domain PageRequest of

List of usage examples for org.springframework.data.domain PageRequest of

Introduction

In this page you can find the example usage for org.springframework.data.domain PageRequest of.

Prototype

public static PageRequest of(int page, int size) 

Source Link

Document

Creates a new unsorted PageRequest .

Usage

From source file:org.eclipse.hawkbit.mgmt.rest.resource.MgmtTargetResourceTest.java

@Test
@Description("Ensures that the metadata creation through API is reflected by the repository.")
public void createMetadata() throws Exception {
    final String knownControllerId = "targetIdWithMetadata";
    testdataFactory.createTarget(knownControllerId);

    final String knownKey1 = "knownKey1";
    final String knownKey2 = "knownKey2";

    final String knownValue1 = "knownValue1";
    final String knownValue2 = "knownValue2";

    final JSONArray metaData1 = new JSONArray();
    metaData1.put(new JSONObject().put("key", knownKey1).put("value", knownValue1));
    metaData1.put(new JSONObject().put("key", knownKey2).put("value", knownValue2));

    mvc.perform(//from  w w  w .  ja  v  a2  s .  c  o m
            post("/rest/v1/targets/{targetId}/metadata", knownControllerId).accept(MediaType.APPLICATION_JSON)
                    .contentType(MediaType.APPLICATION_JSON).content(metaData1.toString()))
            .andDo(MockMvcResultPrinter.print()).andExpect(status().isCreated())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("[0]key", equalTo(knownKey1)))
            .andExpect(jsonPath("[0]value", equalTo(knownValue1)))
            .andExpect(jsonPath("[1]key", equalTo(knownKey2)))
            .andExpect(jsonPath("[1]value", equalTo(knownValue2)));

    final TargetMetadata metaKey1 = targetManagement.getMetaDataByControllerId(knownControllerId, knownKey1)
            .get();
    final TargetMetadata metaKey2 = targetManagement.getMetaDataByControllerId(knownControllerId, knownKey2)
            .get();

    assertThat(metaKey1.getValue()).isEqualTo(knownValue1);
    assertThat(metaKey2.getValue()).isEqualTo(knownValue2);

    // verify quota enforcement
    final int maxMetaData = quotaManagement.getMaxMetaDataEntriesPerTarget();

    final JSONArray metaData2 = new JSONArray();
    for (int i = 0; i < maxMetaData - metaData1.length() + 1; ++i) {
        metaData2.put(new JSONObject().put("key", knownKey1 + i).put("value", knownValue1 + i));
    }

    mvc.perform(
            post("/rest/v1/targets/{targetId}/metadata", knownControllerId).accept(MediaType.APPLICATION_JSON)
                    .contentType(MediaType.APPLICATION_JSON).content(metaData2.toString()))
            .andDo(MockMvcResultPrinter.print()).andExpect(status().isForbidden());

    // verify that the number of meta data entries has not changed
    // (we cannot use the PAGE constant here as it tries to sort by ID)
    assertThat(
            targetManagement.findMetaDataByControllerId(PageRequest.of(0, Integer.MAX_VALUE), knownControllerId)
                    .getTotalElements()).isEqualTo(metaData1.length());

}

From source file:org.eclipse.hawkbit.repository.jpa.DistributionSetManagementTest.java

@Test
@Description("Queries and loads the metadata related to a given software module.")
public void findAllDistributionSetMetadataByDsId() {
    // create a DS
    final DistributionSet ds1 = testdataFactory.createDistributionSet("testDs1");
    final DistributionSet ds2 = testdataFactory.createDistributionSet("testDs2");

    for (int index = 0; index < 10; index++) {
        createDistributionSetMetadata(ds1.getId(),
                new JpaDistributionSetMetadata("key" + index, ds1, "value" + index));
    }/*w ww .ja va  2 s  .  c o  m*/

    for (int index = 0; index < 8; index++) {
        createDistributionSetMetadata(ds2.getId(),
                new JpaDistributionSetMetadata("key" + index, ds2, "value" + index));
    }

    final Page<DistributionSetMetadata> metadataOfDs1 = distributionSetManagement
            .findMetaDataByDistributionSetId(PageRequest.of(0, 100), ds1.getId());

    final Page<DistributionSetMetadata> metadataOfDs2 = distributionSetManagement
            .findMetaDataByDistributionSetId(PageRequest.of(0, 100), ds2.getId());

    assertThat(metadataOfDs1.getNumberOfElements()).isEqualTo(10);
    assertThat(metadataOfDs1.getTotalElements()).isEqualTo(10);

    assertThat(metadataOfDs2.getNumberOfElements()).isEqualTo(8);
    assertThat(metadataOfDs2.getTotalElements()).isEqualTo(8);
}

From source file:org.eclipse.hawkbit.repository.jpa.SoftwareModuleManagementTest.java

@Test
@Description("Verfies that existing metadata can be deleted.")
public void deleteSoftwareModuleMetadata() {
    final String knownKey1 = "myKnownKey1";
    final String knownValue1 = "myKnownValue1";

    SoftwareModule ah = testdataFactory.createSoftwareModuleApp();

    softwareModuleManagement.createMetaData(
            entityFactory.softwareModuleMetadata().create(ah.getId()).key(knownKey1).value(knownValue1));

    ah = softwareModuleManagement.get(ah.getId()).get();

    assertThat(softwareModuleManagement.findMetaDataBySoftwareModuleId(PageRequest.of(0, 10), ah.getId())
            .getContent()).as("Contains the created metadata element")
                    .containsExactly(new JpaSoftwareModuleMetadata(knownKey1, ah, knownValue1));

    softwareModuleManagement.deleteMetaData(ah.getId(), knownKey1);
    assertThat(softwareModuleManagement.findMetaDataBySoftwareModuleId(PageRequest.of(0, 10), ah.getId())
            .getContent()).as("Metadata elemenets are").isEmpty();
}

From source file:org.eclipse.hawkbit.repository.jpa.SoftwareModuleManagementTest.java

@Test
@Description("Queries and loads the metadata related to a given software module.")
public void findAllSoftwareModuleMetadataBySwId() {

    final SoftwareModule sw1 = testdataFactory.createSoftwareModuleApp();
    final int metadataCountSw1 = 8;

    final SoftwareModule sw2 = testdataFactory.createSoftwareModuleOs();
    final int metadataCountSw2 = 10;

    for (int index = 0; index < metadataCountSw1; index++) {
        softwareModuleManagement.createMetaData(entityFactory.softwareModuleMetadata().create(sw1.getId())
                .key("key" + index).value("value" + index).targetVisible(true));
    }// w  w  w  .jav  a  2  s.co  m

    for (int index = 0; index < metadataCountSw2; index++) {
        softwareModuleManagement.createMetaData(entityFactory.softwareModuleMetadata().create(sw2.getId())
                .key("key" + index).value("value" + index).targetVisible(false));
    }

    Page<SoftwareModuleMetadata> metadataSw1 = softwareModuleManagement
            .findMetaDataBySoftwareModuleId(PageRequest.of(0, 100), sw1.getId());

    Page<SoftwareModuleMetadata> metadataSw2 = softwareModuleManagement
            .findMetaDataBySoftwareModuleId(PageRequest.of(0, 100), sw2.getId());

    assertThat(metadataSw1.getNumberOfElements()).isEqualTo(metadataCountSw1);
    assertThat(metadataSw1.getTotalElements()).isEqualTo(metadataCountSw1);

    assertThat(metadataSw2.getNumberOfElements()).isEqualTo(metadataCountSw2);
    assertThat(metadataSw2.getTotalElements()).isEqualTo(metadataCountSw2);

    metadataSw1 = softwareModuleManagement
            .findMetaDataBySoftwareModuleIdAndTargetVisible(PageRequest.of(0, 100), sw1.getId());

    metadataSw2 = softwareModuleManagement
            .findMetaDataBySoftwareModuleIdAndTargetVisible(PageRequest.of(0, 100), sw2.getId());

    assertThat(metadataSw1.getNumberOfElements()).isEqualTo(metadataCountSw1);
    assertThat(metadataSw1.getTotalElements()).isEqualTo(metadataCountSw1);

    assertThat(metadataSw2.getNumberOfElements()).isEqualTo(0);
    assertThat(metadataSw2.getTotalElements()).isEqualTo(0);
}

From source file:org.eclipse.hawkbit.repository.jpa.TargetManagementTest.java

@Test
@WithUser(allSpPermissions = true)//from   w w w .  j a  va2  s.c  om
@Description("Create multiple targets as bulk operation and delete them in bulk.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 101),
        @Expect(type = TargetUpdatedEvent.class, count = 100),
        @Expect(type = TargetDeletedEvent.class, count = 51) })
public void bulkTargetCreationAndDelete() throws Exception {
    final String myCtrlID = "myCtrlID";
    List<Target> firstList = testdataFactory.createTargets(100, myCtrlID, "first description");

    final Target extra = testdataFactory.createTarget("myCtrlID-00081XX");

    final Iterable<JpaTarget> allFound = targetRepository.findAll();

    assertThat(Long.valueOf(firstList.size())).as("List size of targets")
            .isEqualTo(firstList.spliterator().getExactSizeIfKnown());
    assertThat(Long.valueOf(firstList.size() + 1)).as("LastModifiedAt compared with saved lastModifiedAt")
            .isEqualTo(allFound.spliterator().getExactSizeIfKnown());

    // change the objects and save to again to trigger a change on
    // lastModifiedAt
    firstList = firstList.stream()
            .map(t -> targetManagement.update(
                    entityFactory.target().update(t.getControllerId()).name(t.getName().concat("\tchanged"))))
            .collect(Collectors.toList());

    // verify that all entries are found
    _founds: for (final Target foundTarget : allFound) {
        for (final Target changedTarget : firstList) {
            if (changedTarget.getControllerId().equals(foundTarget.getControllerId())) {
                assertThat(changedTarget.getDescription())
                        .as("Description of changed target compared with description saved target")
                        .isEqualTo(foundTarget.getDescription());
                assertThat(changedTarget.getName())
                        .as("Name of changed target starts with name of saved target")
                        .startsWith(foundTarget.getName());
                assertThat(changedTarget.getName()).as("Name of changed target ends with 'changed'")
                        .endsWith("changed");
                assertThat(changedTarget.getCreatedAt()).as("CreatedAt compared with saved createdAt")
                        .isEqualTo(foundTarget.getCreatedAt());
                assertThat(changedTarget.getLastModifiedAt()).as("LastModifiedAt compared with saved createdAt")
                        .isNotEqualTo(changedTarget.getCreatedAt());
                continue _founds;
            }
        }

        if (!foundTarget.getControllerId().equals(extra.getControllerId())) {
            fail("The controllerId of the found target is not equal to the controllerId of the saved target");
        }
    }

    targetManagement.deleteByControllerID(extra.getControllerId());

    final int numberToDelete = 50;
    final Collection<Target> targetsToDelete = firstList.subList(0, numberToDelete);
    final Target[] deletedTargets = Iterables.toArray(targetsToDelete, Target.class);
    final List<Long> targetsIdsToDelete = targetsToDelete.stream().map(Target::getId)
            .collect(Collectors.toList());

    targetManagement.delete(targetsIdsToDelete);

    final List<Target> targetsLeft = targetManagement.findAll(PageRequest.of(0, 200)).getContent();
    assertThat(firstList.spliterator().getExactSizeIfKnown() - numberToDelete).as("Size of split list")
            .isEqualTo(targetsLeft.spliterator().getExactSizeIfKnown());

    assertThat(targetsLeft).as("Not all undeleted found").doesNotContain(deletedTargets);
}

From source file:org.eclipse.hawkbit.repository.jpa.TargetManagementTest.java

@Test
@Description("Queries and loads the metadata related to a given target.")
public void findAllTargetMetadataByControllerId() {
    // create targets
    final Target target1 = createTargetWithMetadata("target1", 10);
    final Target target2 = createTargetWithMetadata("target2", 8);

    final Page<TargetMetadata> metadataOfTarget1 = targetManagement
            .findMetaDataByControllerId(PageRequest.of(0, 100), target1.getControllerId());

    final Page<TargetMetadata> metadataOfTarget2 = targetManagement
            .findMetaDataByControllerId(PageRequest.of(0, 100), target2.getControllerId());

    assertThat(metadataOfTarget1.getNumberOfElements()).isEqualTo(10);
    assertThat(metadataOfTarget1.getTotalElements()).isEqualTo(10);

    assertThat(metadataOfTarget2.getNumberOfElements()).isEqualTo(8);
    assertThat(metadataOfTarget2.getTotalElements()).isEqualTo(8);
}

From source file:org.eclipse.hawkbit.repository.test.util.TestdataFactory.java

/**
 * Append {@link ActionStatus} to all {@link Action}s of given
 * {@link Target}s.//from  ww w .  ja  v  a  2  s  .c om
 * 
 * @param targets
 *            to add {@link ActionStatus}
 * @param status
 *            to add
 * @param msgs
 *            to add
 * 
 * @return updated {@link Action}.
 */
public List<Action> sendUpdateActionStatusToTargets(final Collection<Target> targets, final Status status,
        final Collection<String> msgs) {
    final List<Action> result = new ArrayList<>();
    for (final Target target : targets) {
        final List<Action> findByTarget = deploymentManagement
                .findActionsByTarget(target.getControllerId(), PageRequest.of(0, 400)).getContent();
        for (final Action action : findByTarget) {
            result.add(sendUpdateActionStatusToTarget(status, action, msgs));
        }
    }
    return result;
}

From source file:org.ligoj.app.plugin.km.confluence.ConfluencePluginResource.java

/**
 * Find the spaces matching to the given criteria. Look into space key, and
 * space name.// w  w  w . j a  v  a2s .co m
 * 
 * @param node
 *            the node to be tested with given parameters.
 * @param criteria
 *            the search criteria.
 * @return Matching spaces, ordered by space name, not the the key.
 */
@GET
@Path("{node}/{criteria}")
@Consumes(MediaType.APPLICATION_JSON)
public List<Space> findAllByName(@PathParam("node") final String node,
        @PathParam("criteria") final String criteria) throws IOException {
    // Check the node exists
    if (nodeRepository.findOneVisible(node, securityHelper.getLogin()) == null) {
        return Collections.emptyList();
    }

    // Get the target node parameters
    final Map<String, String> parameters = pvResource.getNodeParameters(node);
    final List<Space> result = new ArrayList<>();
    int start = 0;
    // Limit the result to 10, and search with a page size of 100
    while (addAllByName(parameters, criteria, result, start) && result.size() < 10) {
        start += 100;
    }

    return inMemoryPagination.newPage(result, PageRequest.of(0, 10)).getContent();
}