private String marshal() { String result = null; ByteArrayOutputStream out = null; try { JAXBContext jaxbContext = JAXBContext.newInstance(Mapping.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); out = new ByteArrayOutputStream(); Mapping mapping = new Mapping(); MappingAction action = new MappingAction(); action.setName("ReplaceCustomerId"); action.setActionClassName("com.apifest.example.ReplaceCustomerIdAction"); MappingAction action2 = new MappingAction(); action2.setName("AddSenderIdInBody"); action2.setActionClassName("com.apifest.example.AddSenderIdInBody"); List<MappingAction> actions = new ArrayList<MappingAction>(); actions.add(action); actions.add(action2); ActionsWrapper allActions = new ActionsWrapper(); allActions.setActions(actions); mapping.setActionsWrapper(allActions); MappingEndpoint endpoint = new MappingEndpoint(); MappingAction addAction = new MappingAction(); addAction.setName("ReplaceCustomerId"); endpoint.setAction(addAction); endpoint.setInternalEndpoint("/v0.1/customer/{customerId}"); endpoint.setExternalEndpoint("/v0.1/me"); EndpointsWrapper endpointWrapper = new EndpointsWrapper(); List<MappingEndpoint> endpoints = new ArrayList<MappingEndpoint>(); endpoints.add(endpoint); endpointWrapper.setEndpoints(endpoints); mapping.setEndpointsWrapper(endpointWrapper); marshaller.marshal(mapping, out); result = out.toString("UTF-8"); } catch (JAXBException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
@Test public void when_mapping_with_RE_construct_Pattern() throws Exception { // GIVEN MappingEndpoint endpoint = new MappingEndpoint(); endpoint.setExternalEndpoint("/v0.1/payments/{paymentId}"); endpoint.setInternalEndpoint("/v0.1/payments/{paymentId}"); endpoint.setVarExpression("\\d*"); endpoint.setVarName("paymentId"); // WHEN Pattern p = ConfigLoader.constructPattern(endpoint); // THEN assertEquals(p.toString(), "/v0.1/payments/(\\d*)$"); }