001/*- 002 * #%L 003 * Smile CDR - CDR 004 * %% 005 * Copyright (C) 2016 - 2025 Smile CDR, Inc. 006 * %% 007 * All rights reserved. 008 * #L% 009 */ 010package ca.cdr.test.util; 011 012import java.io.FileInputStream; 013import java.io.IOException; 014import java.io.InputStream; 015import java.util.HashMap; 016import java.util.Map; 017import java.util.Properties; 018 019/** 020 * Utility class for handling properties file operations in tests. 021 */ 022// Created by Claude Sonnet 4 023public class PropertiesTestUtil { 024 025 /** 026 * Loads a properties file and extracts port configurations organized by module type and module ID. 027 * 028 * @param thePropertiesLocation the path to the properties file. Can be a regular file path or a classpath resource prefixed with "classpath:" 029 * @return a map where the key is module type and the value is a map of module ID to port number 030 * @throws RuntimeException if the properties file cannot be read 031 */ 032 public static Map<String, Map<String, Integer>> loadPortsFromPropertiesFile(String thePropertiesLocation) { 033 Map<String, Map<String, Integer>> portsByModuleType = new HashMap<>(); 034 035 try (InputStream inputStream = getInputStream(thePropertiesLocation)) { 036 Properties properties = new Properties(); 037 properties.load(inputStream); 038 039 // Extract port values from keys containing ".port" 040 for (String key : properties.stringPropertyNames()) { 041 if (key.contains(".port")) { 042 String value = properties.getProperty(key); 043 try { 044 int portValue = Integer.parseInt(value.trim()); 045 046 // Extract Module ID and Module Type 047 if (key.startsWith("module.")) { 048 String[] parts = key.split("\\."); 049 if (parts.length >= 3) { 050 String moduleId = parts[1]; 051 052 // Get the module type from the properties 053 String moduleTypeKey = "module." + moduleId + ".type"; 054 String moduleType = properties.getProperty(moduleTypeKey); 055 056 if (moduleType != null) { 057 // Add to the map: moduleType -> (moduleId -> port) 058 portsByModuleType.computeIfAbsent(moduleType, k -> new HashMap<>()) 059 .put(moduleId, portValue); 060 } 061 } 062 } 063 } catch (NumberFormatException e) { 064 // Skip if the port value is not a valid integer 065 } 066 } 067 } 068 } catch (IOException e) { 069 throw new RuntimeException("Failed to read properties file: " + thePropertiesLocation, e); 070 } 071 072 return portsByModuleType; 073 } 074 075 private static InputStream getInputStream(String thePropertiesLocation) throws IOException { 076 if (thePropertiesLocation.startsWith("classpath:")) { 077 String classpathResource = thePropertiesLocation.substring("classpath:".length()); 078 InputStream resourceStream = PropertiesTestUtil.class.getClassLoader().getResourceAsStream(classpathResource); 079 if (resourceStream == null) { 080 throw new IOException("Classpath resource not found: " + classpathResource); 081 } 082 return resourceStream; 083 } else { 084 return new FileInputStream(thePropertiesLocation); 085 } 086 } 087}