ResourceCompare { public static <T> boolean equals(T left, T right) { ObjectMapper jsonMapper = Serialization.jsonMapper(); if (left == null && right == null) { return true; } else if (left == null) { return false; } else if (right == null) { return false; } Map<String, Object> leftJson = jsonMapper.convertValue(left, TYPE_REF); Map<String, Object> rightJson = jsonMapper.convertValue(right, TYPE_REF); if (left instanceof KubernetesList) { return compareKubernetesList(leftJson, rightJson); } else { return compareKubernetesResource(leftJson, rightJson); } } private  ResourceCompare(); static boolean equals(T left, T right); static boolean compareKubernetesList(Map<String, Object> leftJson, Map<String, Object> rightJson); static boolean compareKubernetesResource(Map<String, Object> leftJson, Map<String, Object> rightJson);  }
@Test void testResourceCompareEqualsTrue() { assertThat(ResourceCompare.equals(kubeList, kubeList), is(true)); }
@Test void testResourceCompareEqualsFalse() { final ReplicationController rc = new ReplicationControllerBuilder() .withNewMetadata().withName("repl1").withNamespace("test").endMetadata() .withNewSpec().withReplicas(2).endSpec() .build(); final KubernetesList kubeList2 = new KubernetesListBuilder(kubeList).withItems(pod, service, rc).build(); assertThat(ResourceCompare.equals(kubeList, kubeList2), is(false)); }
@Test void testPodResourceCompareEqualsTrueNoLabels() { Pod podNoLabels = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); assertThat(ResourceCompare.equals(pod, podNoLabels), is(true)); }
@Test void testPodResourceCompareEqualsTrueMatchingLabels() { Pod podWithLabels = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").withLabels(Collections.singletonMap("label", "value")).and().build(); assertThat(ResourceCompare.equals(pod, podWithLabels), is(true)); }
@Test void testPodResourceCompareEqualsFalseDifferentLabels() { Pod podWithLabels = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").withLabels(Collections.singletonMap("label", "another value")).and().build(); assertThat(ResourceCompare.equals(pod, podWithLabels), is(false)); }
@Test void testServiceDifferenceFromClusterAndAsObject() { Service serviceFromCluster = new ServiceBuilder() .withNewMetadata() .withCreationTimestamp("2020-07-27T10:36:33Z") .withName("my-service") .withNamespace("default") .withResourceVersion("202998") .withSelfLink("/api/v1/namespaces/default/services/my-service") .withUid("99fe3964-b53b-473f-b1d8-bdb0390d1634") .endMetadata() .withNewSpec() .withClusterIP("10.110.153.70") .addNewPort() .withPort(80) .withProtocol("TCP") .withTargetPort(new IntOrString("9376")) .endPort() .addToSelector(Collections.singletonMap("app", "MyApp")) .withType("ClusterIP") .endSpec() .build(); Service serviceAsObj = new ServiceBuilder() .withNewMetadata().withName("my-service").endMetadata() .withNewSpec() .addToSelector(Collections.singletonMap("app", "MyApp")) .addNewPort() .withPort(80) .withProtocol("TCP") .withTargetPort(new IntOrString("9376")) .endPort() .endSpec() .build(); boolean result = ResourceCompare.equals(serviceFromCluster, serviceAsObj); assertTrue(result); }
@Test void testEqualsWhenOneResourceIsNull() { Pod pod2 = new PodBuilder().withNewMetadata().withName("foo").endMetadata().build(); boolean result = ResourceCompare.equals(null, pod2); assertFalse(result); }
@Test void testEqualsWhenBothNull() { assertTrue(ResourceCompare.equals(null, null)); }