Skip to content

Commit

Permalink
Fix fabric8io#2354: Fix NullPointerException in ResourceCompare when …
Browse files Browse the repository at this point in the history
…no resource is returned from fromServer.get()

Somehow resource was being returned as `null` from `fromServer.get()` call due to no
namespace being provided. Added a case of Null items in `ResourceCompare#equals`
  • Loading branch information
rohanKanojia committed Aug 3, 2020
1 parent 50c1e8d commit dbdcafc
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@
#### Bugs
* Fix #2373: Unable to create a Template on OCP3
* Fix #2316: Cannot load resource from stream without apiVersion
* Fix #2354: Fix NullPointerException in ResourceCompare when no resource is returned from fromServer.get()

#### Improvements
* Fix #2331: Fixed documentation for namespaced informer for all custom types implementing `Namespaced` interface
Expand Down
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.knative.test;

import io.fabric8.knative.client.KnativeClient;
import io.fabric8.knative.mock.KnativeServer;
import io.fabric8.knative.serving.v1.Route;
import io.fabric8.knative.serving.v1.RouteBuilder;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.net.HttpURLConnection;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@EnableRuleMigrationSupport
class RouteTest {
@Rule
public KnativeServer server = new KnativeServer();

@Test
void testCreateOrReplace() {
// Given
Route route = new RouteBuilder()
.withNewMetadata()
.withName("helloworld-nodejs-red-blue1")
.withNamespace("test")
.endMetadata()
.withNewSpec()
.addNewTraffic()
.withConfigurationName("greeter")
.withPercent(100L)
.endTraffic()
.endSpec()
.build();
server.expect().post().withPath("/apis/serving.knative.dev/v1/namespaces/test/routes")
.andReturn(HttpURLConnection.HTTP_CONFLICT, route)
.once();
server.expect().get().withPath("/apis/serving.knative.dev/v1/namespaces/test/routes/helloworld-nodejs-red-blue1")
.andReturn(HttpURLConnection.HTTP_OK, route)
.times(2);
server.expect().put().withPath("/apis/serving.knative.dev/v1/namespaces/test/routes/helloworld-nodejs-red-blue1")
.andReturn(HttpURLConnection.HTTP_OK, route)
.once();
KnativeClient kn = server.getKnativeClient();

// When
route = kn.routes().createOrReplaceWithNew()
.withNewMetadata()
.withName("helloworld-nodejs-red-blue1")
.addToAnnotations("foo", "bar")
.withNamespace("test")
.endMetadata()
.withNewSpec()
.addNewTraffic()
.withConfigurationName("greeter")
.withPercent(100L)
.endTraffic()
.endSpec()
.done();

// Then
assertNotNull(route);
}
}
Expand Up @@ -29,12 +29,12 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

@EnableRuleMigrationSupport
public class RouteTest {
class RouteCrudTest {
@Rule
public KnativeServer server = new KnativeServer(true, true);

@Test
public void shouldReturnEmptyList() {
void shouldReturnEmptyList() {
// Given
KnativeClient client = server.getKnativeClient();

Expand All @@ -47,7 +47,7 @@ public void shouldReturnEmptyList() {
}

@Test
public void shouldListAndGetRoute() {
void shouldListAndGetRoute() {
// Given
KnativeClient client = server.getKnativeClient();
Route Route2 = new RouteBuilder().withNewMetadata().withName("Route2").endMetadata()
Expand All @@ -71,7 +71,7 @@ public void shouldListAndGetRoute() {
}

@Test
public void shouldDeleteARoute() {
void shouldDeleteARoute() {
// Given
KnativeClient client = server.getKnativeClient();
Route route3 = new RouteBuilder().withNewMetadata().withName("route3").endMetadata().build();
Expand Down
Expand Up @@ -48,6 +48,13 @@ private 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);

Expand Down
Expand Up @@ -17,13 +17,16 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.api.model.ReplicationControllerBuilder;
import io.fabric8.kubernetes.api.model.Service;
Expand All @@ -32,6 +35,7 @@
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;

public class ResourceCompareTest {

Expand Down Expand Up @@ -130,4 +134,20 @@ void testServiceDifferenceFromClusterAndAsObject() {
assertTrue(result);
}

@Test
void testEqualsWhenOneResourceIsNull() {
// Given
Pod pod2 = new PodBuilder().withNewMetadata().withName("foo").endMetadata().build();

// When
boolean result = ResourceCompare.equals(null, pod2);

// Then
assertFalse(result);
}

@Test
void testEqualsWhenBothNull() {
assertTrue(ResourceCompare.equals(null, null));
}
}
Expand Up @@ -36,7 +36,7 @@ public class RouteCrudTest {
public OpenShiftServer server = new OpenShiftServer(true, true);

@Test
public void testCrud() {
void testCrud() {
OpenShiftClient client = server.getOpenshiftClient();

Route route1 = new RouteBuilder().withNewMetadata().withName("route1")
Expand Down

0 comments on commit dbdcafc

Please sign in to comment.