|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. 2018 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.ibm.g11n.pipeline.tools.cli; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.junit.Assume.assumeNotNull; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.FileReader; |
| 24 | +import java.io.FileWriter; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.Reader; |
| 27 | +import java.io.Writer; |
| 28 | +import java.util.LinkedList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import com.google.gson.JsonElement; |
| 32 | +import com.google.gson.JsonObject; |
| 33 | +import com.google.gson.JsonParser; |
| 34 | + |
| 35 | +public class FakebrokerMgr { |
| 36 | + |
| 37 | + public static String FB_FILE = System.getProperty("GP_FAKEBROKER_FILE", "../test-fakebroker.json"); |
| 38 | + public static String GPCONFIG_FILE = System.getProperty("GP_CONFIG_FILE", "../test-gpconfig.json"); |
| 39 | + |
| 40 | + public static File getConfigFile() { |
| 41 | + final File gpconfig = new File(GPCONFIG_FILE); |
| 42 | + final File fbfile = new File(FB_FILE); |
| 43 | + if(!gpconfig.isFile()) { |
| 44 | + if(fbfile.isFile()) { |
| 45 | + try (final Reader reader = new FileReader(fbfile)) { |
| 46 | + JsonElement parse = new JsonParser().parse(reader); |
| 47 | + JsonObject o = parse.getAsJsonObject(); |
| 48 | + JsonObject creds = o.get("credentials").getAsJsonObject(); |
| 49 | + try (final Writer writer = new FileWriter(gpconfig)) { |
| 50 | + writer.write(creds.toString()); // ? |
| 51 | + } |
| 52 | + } catch (IOException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + System.err.println("Could not convert " + fbfile.getAbsolutePath() + " to " |
| 55 | + + gpconfig.getAbsolutePath()); |
| 56 | + return null; |
| 57 | + } |
| 58 | + // convert fbFile to gpconfig |
| 59 | + System.err.println("Converted " + fbfile.getAbsolutePath() + " to " |
| 60 | + + gpconfig.getAbsolutePath()); |
| 61 | + return gpconfig; |
| 62 | + } else { |
| 63 | + System.err.println("no fakebroker file:" + fbfile.getAbsolutePath()); |
| 64 | + return null; |
| 65 | + } |
| 66 | + } else { |
| 67 | + // System.out.println("OK: " + gpconfig.getAbsolutePath()); |
| 68 | + return gpconfig; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static void run(String... args) { |
| 73 | + final File gpconfig = getConfigFile(); |
| 74 | + assumeNotNull(gpconfig); // skip test if not set |
| 75 | + assertTrue("Usage: run(\"list\", …) - arg count " + args.length, args.length > 0); |
| 76 | + final List<String> newArgs = new LinkedList<String>(); |
| 77 | + final String verb = args[0]; |
| 78 | + newArgs.add(verb); // verb |
| 79 | + newArgs.add("-j"); |
| 80 | + newArgs.add(gpconfig.getAbsolutePath()); |
| 81 | + for (int i = 1; i < args.length; i++) { |
| 82 | + newArgs.add(args[i]); // add the rest of the args |
| 83 | + } |
| 84 | + String[] asArray = newArgs.toArray(new String[0]); |
| 85 | + System.out.println("GPCmd: " + asArray); |
| 86 | + GPCmd.main(asArray); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments