NestedFieldsWriterInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException, WebApplicationException { NestedFieldsContext nestedFieldsContext = NestedFieldsContextThreadLocal.getNestedFieldsContext(); if (nestedFieldsContext == null) { writerInterceptorContext.proceed(); return; } try { _setFieldValues( writerInterceptorContext.getEntity(), nestedFieldsContext.getFieldNames(), nestedFieldsContext); } catch (Exception exception) { _log.error(exception.getMessage(), exception); throw new WebApplicationException(exception); } writerInterceptorContext.proceed(); } NestedFieldsWriterInterceptor(BundleContext bundleContext); protected  NestedFieldsWriterInterceptor(
		BundleContext bundleContext,
		NestedFieldServiceTrackerCustomizer
			nestedFieldServiceTrackerCustomizer); @Override void aroundWriteTo(WriterInterceptorContext writerInterceptorContext); void destroy();  }
@Test public void testGetNestedFieldsForMultipleItems() throws Exception { Product product1 = _toProduct(1L, null); Product product2 = _toProduct(2L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( Arrays.asList(product1, product2) ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest() ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Arrays.asList("productOptions", "skus"), new MessageImpl(), new MultivaluedHashMap<>(), "v1.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Sku[] skus = product1.getSkus(); Assert.assertEquals(Arrays.toString(skus), 4, skus.length); ProductOption[] productOptions = product1.getProductOptions(); Assert.assertEquals( Arrays.toString(productOptions), 3, productOptions.length); }
@Test public void testGetNestedFieldsForSingleItem() throws Exception { Product product = _toProduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest() ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Arrays.asList("productOptions", "skus"), new MessageImpl(), _getPathParameters(), "v1.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Sku[] skus = product.getSkus(); Assert.assertEquals(Arrays.toString(skus), 4, skus.length); ProductOption[] productOptions = product.getProductOptions(); Assert.assertEquals( Arrays.toString(productOptions), 3, productOptions.length); }
@Test public void testGetNestedFieldsWithDeeplyNestedFields() throws Exception { Product product1 = _toProduct(1L, null); Product product2 = _toProduct(2L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( Arrays.asList(product1, product2) ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest() ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Arrays.asList( "productOptions", "productOptions.productOptionValues"), new MessageImpl(), new MultivaluedHashMap<>(), "v1.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); ProductOption[] productOptions = product1.getProductOptions(); Assert.assertEquals( Arrays.toString(productOptions), 3, productOptions.length); ProductOptionValue[] productOptionValues = productOptions[0].getProductOptionValues(); Assert.assertEquals( Arrays.toString(productOptionValues), 3, productOptionValues.length); productOptionValues = productOptions[1].getProductOptionValues(); Assert.assertEquals( Arrays.toString(productOptionValues), 2, productOptionValues.length); productOptionValues = productOptions[2].getProductOptionValues(); Assert.assertEquals( Arrays.toString(productOptionValues), 0, productOptionValues.length); }
@Test public void testGetNestedFieldsWithNestedFieldId() throws Exception { Product product = _toProduct(1L, "externalCode"); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest() ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("categories"), new MessageImpl(), _getPathParameters(), "v1.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Category[] categories = product.getCategories(); Assert.assertEquals(Arrays.toString(categories), 3, categories.length); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("categories"), new MessageImpl(), _getPathParameters(), "v2.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); categories = product.getCategories(); Assert.assertEquals(Arrays.toString(categories), 2, categories.length); }
@Test public void testGetNestedFieldsWithNonexistendFieldName() throws Exception { Product product = _toProduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.emptyList(), new MessageImpl(), _getPathParameters(), "v1.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Sku[] skus = product.getSkus(); Assert.assertNull(skus); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("nonexistent"), new MessageImpl(), _getPathParameters(), "v1.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); skus = product.getSkus(); Assert.assertNull(skus); }
@Test public void testGetNestedFieldsWithoutContextAnnotation() throws Exception { Product product = _toProduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest("skus") ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Arrays.asList("productOptions", "skus"), new MessageImpl(), _getPathParameters(), "v1.0", new MultivaluedHashMap<>())); Assert.assertNull(_productResource_v1_0_Impl.contextThemeDisplay); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Assert.assertNotNull(_productResource_v1_0_Impl.contextThemeDisplay); }
@Test public void testGetNestedFieldsWithoutOverridingMethod() throws IOException { Product product = _toProduct(1L, "externalCode"); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest("externalCode") ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); MultivaluedHashMap<String, String> queryParameters = new MultivaluedHashMap<String, String>() { { putSingle("externalCode.AcceptLanguage", "es_ES"); } }; NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("externalCode"), new MessageImpl(), _getPathParameters(), "v1.0", queryParameters)); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Assert.assertEquals("codigoExterno", product.getExternalCode()); }
@Test public void testGetNestedFieldsWithPagination() throws Exception { Product product = _toProduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest( "skus", "page", String.valueOf(1), "pageSize", String.valueOf(2)) ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("skus"), new MessageImpl(), _getPathParameters(), "v1.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Sku[] skus = product.getSkus(); Assert.assertEquals(Arrays.toString(skus), 2, skus.length); }
@Test public void testGetNestedFieldsWithQueryParameter() throws IOException { Product product = _toProduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest("productOptions") ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); MultivaluedHashMap<String, String> queryParameters = new MultivaluedHashMap<String, String>() { { putSingle( "productOptions.createDate", "2019-02-19T08:03:11.763Z"); putSingle("productOptions.name", "test2"); } }; NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("productOptions"), new MessageImpl(), _getPathParameters(), "v1.0", queryParameters)); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); ProductOption[] productOptions = product.getProductOptions(); Assert.assertEquals( Arrays.toString(productOptions), 1, productOptions.length); ProductOption productOption = productOptions[0]; Assert.assertEquals("test2", productOption.getName()); }
@Test public void testGetNestedFieldsWithResourceVersioning() throws Exception { Product product = _toProduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest() ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("skus"), new MessageImpl(), _getPathParameters(), "v2.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Sku[] skus = product.getSkus(); Assert.assertEquals(Arrays.toString(skus), 6, skus.length); }
@Test public void testGetNestedFieldsWithSubitem() throws Exception { Subproduct subproduct = _toSubproduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( subproduct ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest() ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Collections.singletonList("skus"), new MessageImpl(), _getPathParameters(), "v2.0", new MultivaluedHashMap<>())); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Sku[] skus = subproduct.getSkus(); Assert.assertEquals(Arrays.toString(skus), 6, skus.length); }
@Test public void testInjectResourceContexts() throws Exception { Product product = _toProduct(1L, null); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( product ); Mockito.doReturn( new NestedFieldsHttpServletRequestWrapperTest. MockHttpServletRequest("skus") ).when( _nestedFieldServiceTrackerCustomizer ).getHttpServletRequest( Mockito.any(Message.class) ); NestedFieldsContextThreadLocal.setNestedFieldsContext( new NestedFieldsContext( Arrays.asList("productOptions", "skus"), new MessageImpl(), _getPathParameters(), "v1.0", new MultivaluedHashMap<>())); Assert.assertNull(_productResource_v1_0_Impl.themeDisplay); _nestedFieldsWriterInterceptor.aroundWriteTo(_writerInterceptorContext); Assert.assertNotNull(_productResource_v1_0_Impl.themeDisplay); }