MatrixParameterResolver implements ParameterResolver<MatrixParam> { @Override public Object resolve(org.everrest.core.Parameter parameter, ApplicationContext context) throws Exception { String param = matrixParam.value(); TypeProducer typeProducer = typeProducerFactory.createTypeProducer(parameter.getParameterClass(), parameter.getGenericType()); List<PathSegment> pathSegments = context.getUriInfo().getPathSegments(!parameter.isEncoded()); PathSegment pathSegment = Iterables.getLast(pathSegments, null); return typeProducer.createValue(param, pathSegment == null ? new MultivaluedHashMap<>() : pathSegment.getMatrixParameters(), parameter.getDefaultValue()); } MatrixParameterResolver(MatrixParam matrixParam, TypeProducerFactory typeProducerFactory); @Override Object resolve(org.everrest.core.Parameter parameter, ApplicationContext context);  }
@Test public void retrievesMatrixParamValueFromLastPathSegment() throws Exception { when(typeProducer.createValue("foo", matrixParameters, null)).thenReturn(matrixParameters.getFirst("foo")); Object resolvedMatrixParameter = matrixParameterResolver.resolve(parameter, applicationContext); assertEquals(matrixParameters.getFirst("foo"), resolvedMatrixParameter); }
@Test public void returnsNullWhenListOfPathSegmentsIsEmptyAndDefaultValueIsNull() throws Exception { when(applicationContext.getUriInfo().getPathSegments(true)).thenReturn(newArrayList()); Object resolvedMatrixParameter = matrixParameterResolver.resolve(parameter, applicationContext); assertNull(resolvedMatrixParameter); verify(typeProducer).createValue(eq("foo"), emptyMultivaluedMap(), (String)isNull()); }
@Test public void returnsDefaultValueWhenListOfPathSegmentsIsEmpty() throws Exception { when(parameter.getDefaultValue()).thenReturn("default value"); when(typeProducer.createValue(eq("foo"), emptyMultivaluedMap(), eq("default value"))).thenReturn("default value"); when(applicationContext.getUriInfo().getPathSegments(true)).thenReturn(newArrayList()); Object resolvedMatrixParameter = matrixParameterResolver.resolve(parameter, applicationContext); assertEquals("default value", resolvedMatrixParameter); }