PodsVolumes { public void changePVCReferences( Collection<PodData> pods, String currentPVCName, String newPVCName) { pods.stream() .flatMap(p -> p.getSpec().getVolumes().stream()) .filter( v -> v.getPersistentVolumeClaim() != null && v.getPersistentVolumeClaim().getClaimName().equals(currentPVCName)) .forEach(v -> v.getPersistentVolumeClaim().setClaimName(newPVCName)); }  void changePVCReferences(
      Collection<PodData> pods, String currentPVCName, String newPVCName); void replacePVCVolumesWithCommon(Map<String, PodData> pods, String commonPVCName);  }
@Test public void shouldChangePVCReference() { podData .getSpec() .getVolumes() .add( new VolumeBuilder() .withName("userData") .withPersistentVolumeClaim( new PersistentVolumeClaimVolumeSourceBuilder() .withClaimName("userData") .build()) .build()); podsVolumes.changePVCReferences(ImmutableList.of(podData), "userData", "newPVCName"); assertEquals(podData.getSpec().getVolumes().size(), 1); Volume volume = podData.getSpec().getVolumes().get(0); assertEquals(volume.getPersistentVolumeClaim().getClaimName(), "newPVCName"); }
@Test public void shouldNotChangeNonMatchingVolumesChangePVCReference() { podData .getSpec() .getVolumes() .add( new VolumeBuilder() .withName("userData") .withPersistentVolumeClaim( new PersistentVolumeClaimVolumeSourceBuilder() .withClaimName("nonMatching") .build()) .build()); podsVolumes.changePVCReferences(ImmutableList.of(podData), "userData", "newPVCName"); assertEquals(podData.getSpec().getVolumes().size(), 1); Volume volume = podData.getSpec().getVolumes().get(0); assertEquals(volume.getPersistentVolumeClaim().getClaimName(), "nonMatching"); }