ResourceFunctionFactory { public static ResourceFunction create(final URI xQueryLocation, final Set<Annotation> annotations) throws ExQueryException { final ResourceFunctionImpl resourceFunction = new ResourceFunctionImpl(); resourceFunction.setXQueryLocation(xQueryLocation); if(annotations == null || annotations.isEmpty()) { throw new ExQueryException("A Resource Function must have at least one RESTXQ Annotation"); } for(final Annotation annotation : annotations) { if(!isResourceFunctionAnnotation(annotation.getName())) { throw new ExQueryException("Annotation is not a valid EXQuery RESTXQ or Serialization Annotation"); } if(annotation instanceof PathAnnotation) { resourceFunction.setPathAnnotation((PathAnnotation)annotation); } else if(annotation instanceof HttpMethodAnnotation) { resourceFunction.getHttpMethodAnnotations().add((HttpMethodAnnotation)annotation); } else if(annotation instanceof ConsumesAnnotation) { resourceFunction.getConsumesAnnotations().add((ConsumesAnnotation)annotation); } else if(annotation instanceof ProducesAnnotation) { resourceFunction.getProducesAnnotations().add((ProducesAnnotation)annotation); } else if(annotation instanceof ParameterAnnotation) { resourceFunction.getParameterAnnotations().add((ParameterAnnotation)annotation); } else if(annotation instanceof SerializationAnnotation) { resourceFunction.getSerializationAnnotations().add((SerializationAnnotation)annotation); } } resourceFunction.setFunctionSignature(annotations.iterator().next().getFunctionSignature()); return resourceFunction; }  static boolean isResourceFunctionAnnotation(final QName annotationName); static ResourceFunction create(final URI xQueryLocation, final Set<Annotation> annotations);  }
@Test(expected=ExQueryException.class) public void create_failsForNonRESTXQAnnotation() throws URISyntaxException, ExQueryException { final QName badName = new QName("http: final Annotation mckAnnotation = mock(Annotation.class); when(mckAnnotation.getName()).thenReturn(badName); final Set<Annotation> annotations = new HashSet<Annotation>(); annotations.add(mckAnnotation); ResourceFunctionFactory.create(new URI("/fake.xquery"), annotations); }
@Test(expected=ExQueryException.class) public void create_fails_when_there_are_no_annotations() throws URISyntaxException, ExQueryException { final QName qnHttpMethodAnnotation = new QName(Namespace.ANNOTATION_NS, "GET"); final Set<Annotation> annotations = new HashSet<Annotation>(); final ResourceFunction resourceFunction = ResourceFunctionFactory.create(new URI("/some.xquery"), annotations); }
@Test public void create_succeedsForRESTXQAnnotations_With_PathAnnotation() throws URISyntaxException, ExQueryException { final QName qnPathAnnotation = new QName(Namespace.ANNOTATION_NS, "path"); final PathAnnotation mckPathAnnotation = mock(PathAnnotation.class); final Set<Annotation> annotations = new HashSet<Annotation>(); annotations.add(mckPathAnnotation); when(mckPathAnnotation.getName()).thenReturn(qnPathAnnotation); final ResourceFunction resourceFunction = ResourceFunctionFactory.create(new URI("/some.xquery"), annotations); assertNotNull(resourceFunction); assertEquals(mckPathAnnotation, resourceFunction.getPathAnnotation()); }