Skip to content

Commit 605cb6b

Browse files
authored
Merge pull request #19 from Sandro642/feature/schematic
Feature: enhance ConnectorAPI with null check for route retrieval and…
2 parents 5dafbf0 + 6f6924b commit 605cb6b

4 files changed

Lines changed: 73 additions & 74 deletions

File tree

src/main/java/fr/sandro642/github/ConnectorAPI.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ public static void initialize(ResourceType resourceType, Class<? extends Enum<?>
6262
storeAndRetrieve.store.put(storeAndRetrieve.URL_KEY, baseUrl);
6363
}
6464

65-
ConnectorAPI.routes.putAll(yamlUtils.getRoutes());
66-
}
67-
68-
/**
69-
* Retourne une instance de JobGetInfos pour les opérations API
70-
*/
71-
public static JobGetInfos JobGetInfos() {
72-
return new JobGetInfos();
65+
// FIX: Vérifier que getRoutes() ne retourne pas null avant putAll
66+
Map<String, String> yamlRoutes = yamlUtils.getRoutes();
67+
if (yamlRoutes != null) {
68+
ConnectorAPI.routes.putAll(yamlRoutes);
69+
}
7370
}
7471

7572
public static String getRoute(String routeName) {
@@ -84,6 +81,13 @@ public static String getRoute(Enum<?> routeEnum) {
8481
return getRoute(routeEnum.name().toLowerCase());
8582
}
8683

84+
/**
85+
* Retourne une instance de JobGetInfos pour les opérations API
86+
*/
87+
public static JobGetInfos JobGetInfos() {
88+
return new JobGetInfos();
89+
}
90+
8791
/**
8892
* Retourne l'instance du Logger
8993
*/

src/main/java/fr/sandro642/github/utils/ConvertEnum.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static Map<Enum<?>, String> convert(Class<? extends Enum<?>> enumClass) {
1616
map.put((Enum<?>) routeImport, routeImport.route());
1717
}
1818
}
19+
1920
return map;
2021
}
2122
}

src/main/java/fr/sandro642/github/utils/YamlUtils.java

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,6 @@ public Boolean isSchemaEnabled() {
5858
}
5959
}
6060

61-
/**
62-
* Récupère une route spécifique depuis le fichier YAML
63-
* @param routeName
64-
* @return
65-
*/
66-
public String getRoute(String routeName) {
67-
68-
String yamlFilePath = ConnectorAPI.StoreAndRetrieve().store.get(ConnectorAPI.StoreAndRetrieve().FILE_LOCATION_KEY) + "/infos.yml";
69-
70-
try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) {
71-
Yaml yaml = new Yaml();
72-
Map<String, Object> yamlData = yaml.load(inputStream);
73-
74-
// Récupérer la map "routes"
75-
Map<String, Object> routes = (Map<String, Object>) yamlData.get("routes");
76-
if (routes != null) {
77-
return (String) routes.get(routeName);
78-
}
79-
return null;
80-
} catch (Exception ex) {
81-
return null;
82-
}
83-
}
84-
8561
/**
8662
* Récupère toutes les routes définies dans le fichier YAML
8763
* @return une map contenant les routes, ou null en cas d'erreur
@@ -94,6 +70,8 @@ public Map<String,String> getRoutes() {
9470
Yaml yaml = new Yaml();
9571
Map<String, Object> yamlData = yaml.load(inputStream);
9672

73+
System.out.println("Récupération des routes " + yamlData.get("routes"));
74+
9775
// Récupérer la map "routes"
9876
return (Map<String, String>) yamlData.get("routes");
9977
} catch (Exception ex) {
@@ -120,47 +98,46 @@ public void generateTemplateIfNotExists(ResourceType type, Map<Enum<?>, String>
12098
}
12199

122100
File file = new File(basePath, "infos.yml");
123-
if (!file.exists()) {
124-
125-
StringBuilder template = new StringBuilder(
126-
"# properties Connector API By Sandro642\n\n" +
127-
"urlPath: \"http://localhost:8080/api\"\n\n" +
128-
"routes:\n" +
129-
" #info: \"/info/version\"\n" +
130-
" #ping: \"/ping\"\n" +
131-
" #status: \"/status\"\n\n");
132-
133-
134-
for (Map.Entry<Enum<?>, String> entry : routes.entrySet()) {
135-
template.append(" ")
136-
.append(entry.getKey().name().toLowerCase())
137-
.append(": \"")
138-
.append(entry.getValue())
139-
.append("\"\n");
140-
}
141101

142-
template.append("\nschema:\n" +
143-
" #Activer la création de schéma ?\n" +
144-
" enable: false\n\n" +
145-
" #Schéma par défaut:\n" +
146-
" #\tmsg : string\n" +
147-
" #\terr: boolean\n" +
148-
" #\tcode: integer\n" +
149-
" #\tdata: Map<String, Object>\n\n" +
150-
" #Composants à créer exemple, je vais créer plusieurs composant:\n" +
151-
" # msg : str\n" +
152-
" # status : bln\n" +
153-
" # code : int\n" +
154-
" # data_string-object : map / string pour une chaine de caractère et\n" +
155-
" # / object pour la récupération de n'importe\n" +
156-
" # / quel type de variable.\n" +
157-
" # Grâce à cela vous pourrez les appeler pour récupérer vos propres valeurs\n" +
158-
" # par rapport à votre schéma réponse API\n");
159-
try (FileWriter writer = new FileWriter(file)) {
160-
writer.write(template.toString());
161-
} catch (IOException e) {
162-
throw new RuntimeException("Erreur lors de la création du template infos.yml", e);
163-
}
102+
StringBuilder template = new StringBuilder(
103+
"# properties Connector API By Sandro642\n\n" +
104+
"urlPath: \"http://localhost:8080/api\"\n\n" +
105+
"routes:\n" +
106+
" #info: \"/info/version\"\n" +
107+
" #ping: \"/ping\"\n" +
108+
" #status: \"/status\"\n\n");
109+
110+
for (Map.Entry<Enum<?>, String> entry : routes.entrySet()) {
111+
template.append(" ")
112+
.append(entry.getKey().name().toLowerCase())
113+
.append(": \"")
114+
.append(entry.getValue())
115+
.append("\"\n");
116+
}
117+
118+
template.append("\nschema:\n" +
119+
" #Activer la création de schéma ?\n" +
120+
" enable: false\n\n" +
121+
" #Schéma par défaut:\n" +
122+
" #\tmsg : string\n" +
123+
" #\terr: boolean\n" +
124+
" #\tcode: integer\n" +
125+
" #\tdata: Map<String, Object>\n\n" +
126+
" #Composants à créer exemple, je vais créer plusieurs composant:\n" +
127+
" # msg : str\n" +
128+
" # status : bln\n" +
129+
" # code : int\n" +
130+
" # data_string-object : map / string pour une chaine de caractère et\n" +
131+
" # / object pour la récupération de n'importe\n" +
132+
" # / quel type de variable.\n" +
133+
" #\n" +
134+
" # Grâce à cela vous pourrez les appeler pour récupérer vos propres valeurs\n" +
135+
" # par rapport à votre schéma réponse API\n");
136+
137+
try (FileWriter writer = new FileWriter(file)) {
138+
writer.write(template.toString());
139+
} catch (IOException e) {
140+
throw new RuntimeException("Erreur lors de la création du template infos.yml", e);
164141
}
165142
}
166143
}

src/test/java/fr/sandro642/github/test/Main.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import fr.sandro642.github.jobs.misc.MethodType;
77
import fr.sandro642.github.jobs.misc.ResourceType;
88
import fr.sandro642.github.jobs.misc.VersionType;
9+
import fr.sandro642.github.utils.ConvertEnum;
910
import org.junit.jupiter.api.Test;
1011

1112
/**
@@ -17,19 +18,35 @@
1718

1819
public class Main {
1920

21+
public enum TestRoutes implements ConvertEnum.RouteImport {
22+
VERSION("/api/mcas/info/version"),
23+
INFO("/api/mcas/info/info");
24+
25+
String route;
26+
27+
TestRoutes(String route) {
28+
this.route = route;
29+
}
30+
31+
@Override
32+
public String route() {
33+
return route;
34+
}
35+
}
36+
2037
@Test
2138
public void initializeCAPI() {
2239
ConnectorAPI.initialize(ResourceType.TEST_RESOURCES);
2340
}
2441

2542

2643
public static void main(String[] args) {
27-
ConnectorAPI.initialize(ResourceType.TEST_RESOURCES);
44+
ConnectorAPI.initialize(ResourceType.TEST_RESOURCES, TestRoutes.class);
2845

2946
try {
3047
// Exemple d'utilisation comme demandé
3148
ApiResponse<Void> response = ConnectorAPI.JobGetInfos()
32-
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, "info")
49+
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION)
3350
.getResponse();
3451

3552
System.out.println("Data: " + response.getData());

0 commit comments

Comments
 (0)