ServiceLoader implements BundleEntityLoader { @Override public void load(Bundle bundle, Element element) { final Element service = getSingleChildElement(getSingleChildElement(element, RESOURCE), SERVICE); final Element serviceDetails = getSingleChildElement(service, SERVICE_DETAIL); final String id = serviceDetails.getAttribute(ATTRIBUTE_ID); final String folderId = serviceDetails.getAttribute(ATTRIBUTE_FOLDER_ID); Element nameElement = getSingleChildElement(serviceDetails, NAME); final String name = CharacterBlacklistUtil.filterAndReplace(nameElement.getTextContent()); Element servicePropertiesElement = getSingleChildElement(serviceDetails, PROPERTIES); Map<String, Object> allProperties = BuilderUtils.mapPropertiesElements(servicePropertiesElement, PROPERTIES); Map<String, Object> properties = new HashMap<>(); Service serviceEntity = new Service(); Folder parentFolder = getFolder(bundle, folderId); serviceEntity.setName(name); serviceEntity.setId(id); serviceEntity.setPath(getPath(parentFolder, name)); serviceEntity.setParentFolder(parentFolder); serviceEntity.setServiceDetailsElement(serviceDetails); populateServiceEntity(service, serviceEntity, allProperties, properties); boolean isSoapService = isNotBlank(serviceEntity.getSoapVersion()); Element serviceMappingsElement = getSingleChildElement(serviceEntity.getServiceDetailsElement(), SERVICE_MAPPINGS); Element httpMappingElement = getSingleChildElement(serviceMappingsElement, HTTP_MAPPING); if(isSoapService) { try { Element urlPatternElement = getSingleChildElement(httpMappingElement, URL_PATTERN); serviceEntity.setUrl(urlPatternElement.getTextContent()); } catch (BundleLoadException e) { } } else { Element urlPatternElement = getSingleChildElement(httpMappingElement, URL_PATTERN); serviceEntity.setUrl(urlPatternElement.getTextContent()); } Element verbsElement = getSingleChildElement(httpMappingElement, VERBS); NodeList verbs = verbsElement.getElementsByTagName(VERB); Set<String> httpMethods = new HashSet<>(); for (Node verb : nodeList(verbs)) { httpMethods.add(verb.getTextContent()); } serviceEntity.setHttpMethods(httpMethods); serviceEntity.setProperties(properties); Map<String, Service> bundleService = bundle.getServices(); if (bundleService.containsKey(serviceEntity.getPath())) { String duplicatePathName = handleDuplicatePathName(bundleService, serviceEntity); serviceEntity.setName(duplicatePathName.substring(duplicatePathName.lastIndexOf('/') + 1)); serviceEntity.setPath(duplicatePathName); } bundleService.put(serviceEntity.getPath(), serviceEntity); }  @Override void load(Bundle bundle, Element element); @Override String getEntityType();  }
@Test void load() { ServiceLoader loader = new ServiceLoader(); Bundle bundle = new Bundle(); Folder folder = new Folder(); folder.setId(TEST_FOLDER); folder.setName(TEST_FOLDER); folder.setPath(TEST_FOLDER); bundle.getFolders().put(TEST_FOLDER, folder); loader.load(bundle, createServiceXml(DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, false, false, false, false)); assertFalse(bundle.getServices().isEmpty()); assertEquals(1, bundle.getServices().size()); Service service = bundle.getServices().get(TEST_FOLDER + "/service"); assertNotNull(service); assertEquals("service", service.getName()); assertEquals("/service", service.getUrl()); assertEquals("id", service.getId()); assertNotNull(service.getServiceDetailsElement()); assertFalse(service.getHttpMethods().isEmpty()); assertEquals(4, service.getHttpMethods().size()); assertTrue(service.getHttpMethods().containsAll(Arrays.asList("GET", "POST", "PUT", "DELETE"))); assertNotNull(service.getParentFolder()); assertEquals("folder", service.getParentFolder().getId()); assertEquals("policy", service.getPolicy()); Map<String, Object> expected = new HashMap<>(); expected.put("prop", "value"); expected.put("ENV.prop", null); assertPropertiesContent(expected, service.getProperties()); }
@Test void loadDuplicateNameService() { ServiceLoader loader = new ServiceLoader(); Bundle bundle = new Bundle(); Folder folder = new Folder(); folder.setId(TEST_FOLDER); folder.setName(TEST_FOLDER); folder.setPath(TEST_FOLDER); bundle.getFolders().put(TEST_FOLDER, folder); System.setProperty(ServiceAndPolicyLoaderUtil.HANDLE_DUPLICATE_NAMES, "true"); loader.load(bundle, createServiceXml(DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, false, false, false, false)); loader.load(bundle, createServiceXml(DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, true, false, false, false)); System.clearProperty(ServiceAndPolicyLoaderUtil.HANDLE_DUPLICATE_NAMES); assertFalse(bundle.getServices().isEmpty()); assertEquals(2, bundle.getServices().size()); Service service = bundle.getServices().get(TEST_FOLDER + "/service"); assertNotNull(service); Service duplicateService = bundle.getServices().get(TEST_FOLDER + "/service (2)"); assertNotNull(duplicateService); assertEquals("service", service.getName()); assertEquals("id", service.getId()); assertEquals("service (2)", duplicateService.getName()); assertEquals("soapId", duplicateService.getId()); assertTrue(service.getHttpMethods().containsAll(Arrays.asList("GET", "POST", "PUT", "DELETE"))); assertEquals(service.getParentFolder(), duplicateService.getParentFolder()); }
@Test void loadDuplicateServiceWithPropDisabled() { ServiceLoader loader = new ServiceLoader(); Bundle bundle = new Bundle(); Folder folder = new Folder(); folder.setId(TEST_FOLDER); folder.setName(TEST_FOLDER); folder.setPath(TEST_FOLDER); bundle.getFolders().put(TEST_FOLDER, folder); loader.load(bundle, createServiceXml(DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, false, false, false, false)); assertThrows(BundleLoadException.class, () -> loader.load(bundle, createServiceXml(DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, false, false, false, false)) ); }
@Test void soapServiceLoad() { ServiceLoader loader = new ServiceLoader(); Bundle bundle = new Bundle(); Folder folder = new Folder(); folder.setId(TEST_FOLDER); folder.setName(TEST_FOLDER); folder.setPath(TEST_FOLDER); bundle.getFolders().put(TEST_FOLDER, folder); loader.load(bundle, createServiceXml( DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, true, false, false, false)); assertFalse(bundle.getServices().isEmpty()); assertEquals(1, bundle.getServices().size()); Service service = bundle.getServices().get(TEST_FOLDER + "/service"); assertNotNull(service); assertEquals("service", service.getName()); assertEquals("/soap-service", service.getUrl()); assertEquals("soapId", service.getId()); assertNotNull(service.getServiceDetailsElement()); assertFalse(service.getHttpMethods().isEmpty()); assertEquals(4, service.getHttpMethods().size()); assertTrue(service.getHttpMethods().containsAll(Arrays.asList("GET", "POST", "PUT", "DELETE"))); assertNotNull(service.getParentFolder()); assertEquals("folder", service.getParentFolder().getId()); assertTrue(CollectionUtils.isNotEmpty(service.getSoapResources())); SoapResource soapResource = service.getSoapResources().iterator().next(); assertEquals("wsdl file", soapResource.getContent()); assertEquals("1.1", service.getSoapVersion()); assertTrue(service.isWssProcessingEnabled()); Map<String, Object> expected = new HashMap<>(); expected.put("prop", "value"); expected.put("ENV.prop", null); assertPropertiesContent(expected, service.getProperties()); }
@Test void soapServiceLoad_invalidbundle_throwsException() { assertThrows(BundleLoadException.class, () -> { ServiceLoader loader = new ServiceLoader(); Bundle bundle = new Bundle(); loader.load(bundle, createServiceXml( DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, true, false, true, false)); }); assertThrows(BundleLoadException.class, () -> { ServiceLoader loader = new ServiceLoader(); Bundle bundle = new Bundle(); loader.load(bundle, createServiceXml( DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, true, true, false, false)); }); assertThrows(BundleLoadException.class, () -> { ServiceLoader loader = new ServiceLoader(); Bundle bundle = new Bundle(); loader.load(bundle, createServiceXml( DocumentTools.INSTANCE.getDocumentBuilder().newDocument(), true, true, false, false, true)); }); }