cziesman commented on code in PR #13447:
URL: https://github.com/apache/camel/pull/13447#discussion_r1521762957


##########
core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java:
##########
@@ -258,7 +773,55 @@ protected <T extends Endpoint> T 
resolveMandatoryEndpoint(String uri, Class<T> e
      * @return     the mandatory mock endpoint or an exception is thrown if it 
could not be resolved
      */
     protected MockEndpoint getMockEndpoint(String uri) {
-        return resolveMandatoryEndpoint(uri, MockEndpoint.class);
+        return getMockEndpoint(uri, true);
+    }
+
+    /**
+     * Resolves the {@link MockEndpoint} using a URI of the form 
<code>mock:someName</code>, optionally creating it if
+     * it does not exist. This implementation will lookup existing mock 
endpoints and match on the mock queue name, eg
+     * mock:foo and mock:foo?retainFirst=5 would match as the queue name is 
foo.
+     *
+     * @param  uri                     the URI which typically starts with 
"mock:" and has some name
+     * @param  create                  whether or not to allow the endpoint to 
be created if it doesn't exist
+     * @return                         the mock endpoint or an {@link 
NoSuchEndpointException} is thrown if it could not
+     *                                 be resolved
+     * @throws NoSuchEndpointException is the mock endpoint does not exist
+     */
+    protected MockEndpoint getMockEndpoint(String uri, boolean create) throws 
NoSuchEndpointException {
+        // look for existing mock endpoints that have the same queue name, and
+        // to
+        // do that we need to normalize uri and strip out query parameters and
+        // whatnot
+        String n;
+        try {
+            n = URISupport.normalizeUri(uri);
+        } catch (URISyntaxException e) {
+            throw RuntimeCamelException.wrapRuntimeException(e);
+        }
+        // strip query
+        final String target = StringHelper.before(n, "?", n);
+
+        // lookup endpoints in registry and try to find it
+        MockEndpoint found = (MockEndpoint) 
context.getEndpointRegistry().values().stream()
+                .filter(e -> e instanceof MockEndpoint).filter(e -> {
+                    String t = e.getEndpointUri();
+                    // strip query
+                    int idx2 = t.indexOf('?');
+                    if (idx2 != -1) {
+                        t = t.substring(0, idx2);
+                    }
+                    return t.equals(target);
+                }).findFirst().orElse(null);
+
+        if (found != null) {
+            return found;
+        }
+
+        if (create) {
+            return resolveMandatoryEndpoint(uri, MockEndpoint.class);
+        } else {
+            throw new NoSuchEndpointException(String.format("MockEndpoint %s 
does not exist.", uri));
+        }

Review Comment:
   Per my conversation with Claus, both classes are needed since `came-core` 
cannot use `camel-test` due to circular dependencies, and `ContextTestSupport` 
is not intended for use outside of `camel-core`'. As a result, 
`CamelContextSupport` and `ContextTestSupport` will contain duplicated code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to