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_endpoint_contains_two_variables_replace_them_all() throws Exception { // GIVEN ConfigLoader.loadMappingsConfig(false); // WHEN MappingEndpoint endpoint = ConfigLoader.getConfig().get(0).getMappingEndpoint("/v0.1/contacts/mobile/support", "GET"); // THEN assertEquals(endpoint.getInternalEndpoint(), "/contacts/mobile/support"); }
@Test public void when_endpoint_contains_RE_return_it_from_RE_mappings() throws Exception { // GIVEN ConfigLoader.loadMappingsConfig(false); // WHEN MappingEndpoint endpoint = ConfigLoader.getConfig().get(0).getMappingEndpoint("/v0.1/payments/12345", "GET"); // THEN assertEquals(endpoint.getInternalEndpoint(), "/payments/12345"); }
@Test public void when_mapping_list_contains_method_and_uri_return_that_mapping_endpoint() throws Exception { // GIVEN ConfigLoader.loadMappingsConfig(false); // WHEN MappingEndpoint meEndpoint = ConfigLoader.getConfig().get(0).getMappingEndpoint("/v0.1/me", "GET"); // THEN assertEquals(meEndpoint.getExternalEndpoint(), "/v0.1/me"); assertEquals(meEndpoint.getMethod(), "GET"); }
@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*)$"); }
@Test public void when_load_read_mapping() throws Exception { // WHEN ConfigLoader.loadMappingsConfig(false); // THEN List<MappingConfig> config = ConfigLoader.getConfig(); Map<String, String> actions = config.get(0).getActions(); assertEquals(actions.get("ReplaceCustomerId"), "com.apifest.example.ReplaceCustomerIdAction"); assertEquals(actions.get("AddSenderIdInBody"), "com.apifest.example.AddSenderIdInBody"); MappingEndpoint endpoint = config.get(0).getMappingEndpoint("/v0.1/me", "GET"); assertEquals(endpoint.getInternalEndpoint(), "/customer/{customerId}"); assertEquals(endpoint.getAction().getName(), "ReplaceCustomerId"); }
@Test public void when_actionClassname_is_null_get_className_from_actions_config() throws Exception { // GIVEN ConfigLoader.loadMappingsConfig(false); MappingAction mappingAction = new MappingAction(); mappingAction.setName("ReplaceCustomerId"); MappingEndpoint endpoint = new MappingEndpoint(); endpoint.setAction(mappingAction); ConfigLoader.jarClassLoader = mock(URLClassLoader.class); doReturn(ReplaceCustomerIdAction.class) .when(ConfigLoader.jarClassLoader) .loadClass(ReplaceCustomerIdAction.class.getCanonicalName()); // WHEN BasicAction action = ConfigLoader.getConfig().get(0).getAction(mappingAction); // THEN assertTrue(action instanceof ReplaceCustomerIdAction); }