Adjust to latest error handling updates in spec

If an application with high ranking is ultimately not gettable, or there
exist any other error that prevents it from being registered in the
JAX-RS runtime, the applications it is shadowing must remain in shadowed
state and not become eligible for registration in the runtime.


Project: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/commit/5172bf65
Tree: 
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/5172bf65
Diff: 
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/5172bf65

Branch: refs/heads/master
Commit: 5172bf656642828b874d3fe3a7ab45d61805cb58
Parents: a25250e
Author: Carlos Sierra <[email protected]>
Authored: Tue Nov 21 10:16:38 2017 +0100
Committer: Carlos Sierra <[email protected]>
Committed: Tue Nov 21 15:08:37 2017 +0100

----------------------------------------------------------------------
 jax-rs.itests/src/main/java/test/JaxrsTest.java | 46 +++++++++++++----
 .../jax/rs/whiteboard/internal/Whiteboard.java  | 54 ++++++++++----------
 2 files changed, 61 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/5172bf65/jax-rs.itests/src/main/java/test/JaxrsTest.java
----------------------------------------------------------------------
diff --git a/jax-rs.itests/src/main/java/test/JaxrsTest.java 
b/jax-rs.itests/src/main/java/test/JaxrsTest.java
index 427134e..62f19fc 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue;
 import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.*;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Dictionary;
 import java.util.Hashtable;
@@ -31,7 +32,10 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Function;
+import java.util.function.Predicate;
 
+import org.apache.aries.jax.rs.whiteboard.internal.Utils;
 import org.junit.After;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -42,6 +46,7 @@ import org.osgi.framework.ServiceRegistration;
 
 import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime;
 import org.osgi.service.jaxrs.runtime.dto.DTOConstants;
+import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO;
 import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO;
 import org.osgi.util.tracker.ServiceTracker;
 import test.types.TestAddon;
@@ -913,16 +918,16 @@ public class JaxrsTest extends TestHelper {
         ServiceRegistration<Application> ungettableServiceRegistration =
             registerUngettableApplication("service.ranking", 1);
 
-        assertEquals(1, getRuntimeDTO().applicationDTOs.length);
-        assertEquals(1, getRuntimeDTO().failedApplicationDTOs.length);
+        assertEquals(0, getRuntimeDTO().applicationDTOs.length);
+        assertEquals(2, getRuntimeDTO().failedApplicationDTOs.length);
 
-        assertEquals(
-            DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE,
-            getRuntimeDTO().failedApplicationDTOs[0].failureReason);
+        assertThatInRuntime(
+            FAILED_APPLICATIONS,
+            fa -> fa.serviceId == getServiceId(ungettableServiceRegistration) 
&&
+                DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE ==
+                fa.failureReason);
 
-        assertEquals(
-            "Hello application",
-            webTarget.request().get().readEntity(String.class));
+        assertEquals(404, webTarget.request().get().getStatus());
 
         serviceRegistration.unregister();
 
@@ -963,7 +968,7 @@ public class JaxrsTest extends TestHelper {
         assertEquals(1, getRuntimeDTO().failedApplicationDTOs.length);
 
         assertEquals(
-            DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE,
+            DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE,
             getRuntimeDTO().failedApplicationDTOs[0].failureReason);
 
         assertEquals(
@@ -1375,9 +1380,30 @@ public class JaxrsTest extends TestHelper {
 
         assertEquals(0, runtimeDTO.failedExtensionDTOs.length);
     }
+    private static Function<RuntimeDTO, FailedApplicationDTO[]>
+        FAILED_APPLICATIONS = r -> r.failedApplicationDTOs;
     private Collection<ServiceRegistration<?>> _registrations =
         new ArrayList<>();
 
+    private static long getServiceId(ServiceRegistration propertyHolder) {
+        return (long)propertyHolder.getReference().getProperty("service.id");
+    }
+
+    private void assertFailedApplication(long serviceId, int reason) {
+        assertTrue(Arrays.stream(
+            getRuntimeDTO().failedApplicationDTOs
+        ).anyMatch(
+            fa -> fa.serviceId == serviceId && fa.failureReason == reason
+        ));
+    }
+
+    private <T> void assertThatInRuntime(
+        Function<RuntimeDTO, T[]> getter, Predicate<T> predicate) {
+
+        assertTrue(
+            Arrays.stream(getter.apply(getRuntimeDTO())).anyMatch(predicate));
+    }
+
     private JaxRSServiceRuntime getJaxRSServiceRuntime()
         throws InterruptedException {
 
@@ -1539,7 +1565,6 @@ public class JaxrsTest extends TestHelper {
         return serviceRegistration;
     }
 
-
     private ServiceRegistration<?> registerInvalidExtension(
         String name, Object... keyValues) {
 
@@ -1642,5 +1667,4 @@ public class JaxrsTest extends TestHelper {
 
         return serviceRegistration;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/5172bf65/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
----------------------------------------------------------------------
diff --git 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
index 0bfc5bf..22b136e 100644
--- 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
+++ 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
@@ -118,16 +118,10 @@ public class Whiteboard {
                 DynamicFeature.class.getName()
         )));
     static final String DEFAULT_NAME = ".default";
-    private static final Function<ServiceTuple<Application>, String>
-        APPLICATION_BASE =
-        ((Function<ServiceTuple<Application>, 
CachingServiceReference<Application>>)
-            ServiceTuple::getCachingServiceReference).andThen(
-                sr -> getApplicationBase(sr::getProperty));
-    private static final Function<ServiceTuple<Application>, String>
-        APPLICATION_NAME =
-        ((Function<ServiceTuple<Application>, 
CachingServiceReference<Application>>)
-            ServiceTuple::getCachingServiceReference).andThen(
-            sr -> getApplicationName(sr::getProperty));
+    private static final Function<CachingServiceReference<Application>, String>
+        APPLICATION_BASE = sr -> getApplicationBase(sr::getProperty);
+    private static final Function<CachingServiceReference<Application>, String>
+        APPLICATION_NAME = sr -> getApplicationName(sr::getProperty);
 
     private final AriesJaxRSServiceRuntime _runtime;
     private final Map<String, ?> _configurationMap;
@@ -221,29 +215,33 @@ public class Whiteboard {
     }
 
     private OSGi<?> applications() {
-        OSGi<ServiceTuple<Application>> gettableAplicationForWhiteboard =
-            onlyGettables(
-                countChanges(
-                    getApplicationsForWhiteboard(), _counter).
-                flatMap(
-                    sr -> waitForApplicationDependencies(sr, just(sr))),
-                _runtime::addNotGettableApplication,
-                _runtime::removeNotGettableApplication);
-
-        OSGi<ServiceTuple<Application>> highestRankedPerName = highestPer(
-            APPLICATION_NAME, gettableAplicationForWhiteboard,
-            t -> 
_runtime.addClashingApplication(t.getCachingServiceReference()),
-            t -> 
_runtime.removeClashingApplication(t.getCachingServiceReference())
+        OSGi<CachingServiceReference<Application>> applicationsForWhiteboard =
+            countChanges(
+                getApplicationsForWhiteboard(), _counter).
+            flatMap(
+                sr -> waitForApplicationDependencies(sr, just(sr)));
+
+        OSGi<CachingServiceReference<Application>> highestRankedPerName =
+            highestPer(
+                APPLICATION_NAME, applicationsForWhiteboard,
+                _runtime::addClashingApplication,
+                _runtime::removeClashingApplication
         );
 
-        OSGi<ServiceTuple<Application>> highestRankedPerPath = highestPer(
-            APPLICATION_BASE, highestRankedPerName,
-            t -> 
_runtime.addShadowedApplication(t.getCachingServiceReference()),
-            t -> 
_runtime.removeShadowedApplication(t.getCachingServiceReference())
+        OSGi<CachingServiceReference<Application>> highestRankedPerPath =
+            highestPer(
+                APPLICATION_BASE, highestRankedPerName,
+                _runtime::addShadowedApplication,
+                _runtime::removeShadowedApplication
         );
 
+        OSGi<ServiceTuple<Application>> highestGettables = onlyGettables(
+            highestRankedPerPath,
+            _runtime::addNotGettableApplication,
+            _runtime::removeNotGettableApplication);
+
         return
-            highestRankedPerPath.recoverWith(
+            highestGettables.recoverWith(
                 (t, e) ->
                     just(t).map(
                         ServiceTuple::getCachingServiceReference

Reply via email to