@BeforeClass
 public static void setup() {
   try {
     InitialContext ctx = TestUtil.initialContext();
     TestUtil.ensureCtx(ctx, "java:/comp/env");
     TestUtil.ensureCtx(ctx, "java:/comp/env/jdbc");
     ds = createInMemoryDatasource();
     ctx.bind(DB_JNDI_LOC, ds);
     System.out.println("DataSource created and bound to JNDI: " + DB_JNDI_LOC);
   } catch (Exception e) {
     e.printStackTrace();
     throw new RuntimeException(e);
   }
 }
Example #2
0
 /**
  * Resolves the logical endpoint into a real endpoint provided by the {@link IGatewayTestServer}.
  *
  * @param endpoint
  */
 protected String resolveEndpoint(String endpoint) {
   if ("api".equals(endpoint)) {
     return gatewayServer.getApiEndpoint();
   } else if ("gateway".equals(endpoint)) {
     return gatewayServer.getGatewayEndpoint();
   } else {
     return TestUtil.doPropertyReplacement(endpoint);
   }
 }
 /**
  * Creates an h2 file based datasource.
  *
  * @throws SQLException
  */
 private static BasicDataSource createFileDatasource(File outputDirectory) throws SQLException {
   TestUtil.setProperty("apiman.hibernate.dialect", "org.hibernate.dialect.H2Dialect");
   BasicDataSource ds = new BasicDataSource();
   ds.setDriverClassName(Driver.class.getName());
   ds.setUsername("sa");
   ds.setPassword("");
   ds.setUrl("jdbc:h2:" + outputDirectory.toString() + "/apiman-manager-api;MVCC=true");
   Connection connection = ds.getConnection();
   connection.close();
   System.out.println("DataSource created and bound to JNDI.");
   return ds;
 }
 /**
  * Creates an in-memory datasource.
  *
  * @throws SQLException
  */
 private static BasicDataSource createInMemoryDatasource() throws SQLException {
   TestUtil.setProperty("apiman.hibernate.dialect", "org.hibernate.dialect.H2Dialect");
   BasicDataSource ds = new BasicDataSource();
   ds.setDriverClassName(Driver.class.getName());
   ds.setUsername("sa");
   ds.setPassword("");
   ds.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
   Connection connection = ds.getConnection();
   connection.close();
   System.out.println("DataSource created and bound to JNDI.");
   return ds;
 }
Example #5
0
 /** Configure some proeprties. */
 private void configureSystemProperties() {
   GatewayRestTestSystemProperties annotation =
       getTestClass().getJavaClass().getAnnotation(GatewayRestTestSystemProperties.class);
   if (annotation != null) {
     String[] strings = annotation.value();
     for (int idx = 0; idx < strings.length; idx += 2) {
       String pname = strings[idx];
       String pval = strings[idx + 1];
       log("Setting system property \"{0}\" to \"{1}\".", pname, pval);
       if (System.getProperty(pname) == null) {
         resetSysProps.add(pname);
       }
       TestUtil.setProperty(pname, pval);
     }
   }
 }
Example #6
0
  /**
   * Loads the test plans.
   *
   * @param testClass
   * @throws InitializationError
   */
  private void loadTestPlans(Class<?> testClass) throws InitializationError {
    try {
      GatewayRestTestPlan annotation = testClass.getAnnotation(GatewayRestTestPlan.class);
      if (annotation == null) {
        throw new InitializationError("Missing @GatewayRestTestPlan annotation on test class.");
      } else {
        TestPlanInfo planInfo = new TestPlanInfo();
        planInfo.planPath = annotation.value();
        planInfo.name = new File(planInfo.planPath).getName();
        planInfo.plan = TestUtil.loadTestPlan(planInfo.planPath, testClass.getClassLoader());
        testPlans.add(planInfo);
      }
    } catch (Throwable e) {
      throw new InitializationError(e);
    }

    if (testPlans.isEmpty()) {
      throw new InitializationError(
          "No @GatewayRestTestPlan annotations found on test class: " + testClass);
    }
  }
  /** Stuff to do before the server is started. */
  protected void preStart() throws Exception {
    if (ManagerTestUtils.getTestType() == TestType.jpa) {
      TestUtil.setProperty("apiman.hibernate.hbm2ddl.auto", "create-drop");
      TestUtil.setProperty(
          "apiman.hibernate.connection.datasource", "java:/comp/env/jdbc/ApiManagerDS");
      try {
        InitialContext ctx = new InitialContext();
        ensureCtx(ctx, "java:/comp/env");
        ensureCtx(ctx, "java:/comp/env/jdbc");
        String dbOutputPath = System.getProperty("apiman.test.h2-output-dir", null);
        if (dbOutputPath != null) {
          ds = createFileDatasource(new File(dbOutputPath));
        } else {
          ds = createInMemoryDatasource();
        }
        ctx.bind("java:/comp/env/jdbc/ApiManagerDS", ds);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    if (ManagerTestUtils.getTestType() == TestType.es && node == null) {
      System.out.println("Creating the ES node.");
      File esHome = new File("target/es");
      String esHomeSP = System.getProperty("apiman.test.es-home", null);
      if (esHomeSP != null) {
        esHome = new File(esHomeSP);
      }
      if (esHome.isDirectory()) {
        FileUtils.deleteDirectory(esHome);
      }
      Builder settings = NodeBuilder.nodeBuilder().settings();
      settings.put("path.home", esHome.getAbsolutePath());
      settings.put("http.port", "6500-6600");
      settings.put("transport.tcp.port", "6600-6700");
      settings.put("discovery.zen.ping.multicast.enabled", false);

      String clusterName = System.getProperty("apiman.test.es-cluster-name", ES_CLUSTER_NAME);

      boolean isPersistent =
          "true".equals(System.getProperty("apiman.test.es-persistence", "false"));
      if (!isPersistent) {
        System.out.println("Creating non-persistent ES");
        settings
            .put("index.store.type", "memory")
            .put("gateway.type", "none")
            .put("index.number_of_shards", 1)
            .put("index.number_of_replicas", 1);
        node =
            NodeBuilder.nodeBuilder()
                .client(false)
                .clusterName(clusterName)
                .data(true)
                .local(true)
                .settings(settings)
                .build();
      } else {
        System.out.println("Creating *persistent* ES here: " + esHome);
        node =
            NodeBuilder.nodeBuilder()
                .client(false)
                .clusterName(clusterName)
                .data(true)
                .local(false)
                .settings(settings)
                .build();
      }

      System.out.println("Starting the ES node.");
      node.start();
      System.out.println("ES node was successfully started.");

      // TODO parameterize this
      String connectionUrl = "http://localhost:6500";
      JestClientFactory factory = new JestClientFactory();
      factory.setHttpClientConfig(
          new HttpClientConfig.Builder(connectionUrl).multiThreaded(true).build());
      client = factory.getObject();
      ES_CLIENT = client;
    }
  }