예제 #1
0
 /**
  * Creates new runtime resource model from the list of resources.
  *
  * @param resources List of all resource that should be base for the model.
  */
 public RuntimeResourceModel(List<Resource> resources) {
   this.runtimeResources = Lists.newArrayList();
   for (RuntimeResource.Builder builder : getRuntimeResources(resources)) {
     runtimeResources.add(builder.build(null));
   }
   Collections.sort(runtimeResources, RuntimeResource.COMPARATOR);
 }
예제 #2
0
 /** @see contact.service.ContactDao#findByTitle(java.lang.String) */
 @Override
 public ContactList findByTitle(String titlestr) {
   Query query = em.createQuery("SELECT c FROM Contact c WHERE LOWER(c.title) LIKE :title");
   query.setParameter("title", "%" + titlestr.toLowerCase() + "%");
   ContactList result = new ContactList(Lists.newArrayList(query.getResultList()));
   return result;
 }
예제 #3
0
 @Override
 public List<MediaType> getAcceptableMediaTypes() {
   return Lists.transform(
       getQualifiedAcceptableMediaTypes(),
       new Function<AcceptableMediaType, MediaType>() {
         @Override
         public MediaType apply(AcceptableMediaType input) {
           return input;
         }
       });
 }
예제 #4
0
  @Override
  public List<Locale> getAcceptableLanguages() {
    return Lists.transform(
        getQualifiedAcceptableLanguages(),
        new Function<AcceptableLanguageTag, Locale>() {

          @Override
          public Locale apply(AcceptableLanguageTag input) {
            return input.getAsLocale();
          }
        });
  }
 @Path("/bulk")
 @PUT
 public Response updateBulk(
     @Auth AuthModelT authModel, List<ModelT> models, @Context HttpServletRequest request)
     throws HttpCodeException {
   // TODO: implement transaction
   List<ModelT> updatedModels = Lists.newArrayList();
   for (ModelT model : models) {
     updatedModels.add(man.update(getAuthContext(authModel), model));
   }
   return res().success(res().modelData(updatedModels));
 }
예제 #6
0
  private List<RuntimeResource.Builder> getRuntimeResources(List<Resource> resources) {
    Map<String, List<Resource>> regexMap = Maps.newHashMap();

    for (Resource resource : resources) {
      String path = resource.getPath();
      String regex = null;
      if (path != null) {
        if (path.endsWith("/")) {
          path = path.substring(0, path.length() - 1);
        }
        regex = new PathTemplate(path).getPattern().getRegex();
      }

      List<Resource> listFromMap = regexMap.get(regex);
      if (listFromMap == null) {
        listFromMap = Lists.newArrayList();
        regexMap.put(regex, listFromMap);
      }
      listFromMap.add(resource);
    }

    List<RuntimeResource.Builder> runtimeResources = Lists.newArrayList();
    for (Map.Entry<String, List<Resource>> entry : regexMap.entrySet()) {
      final List<Resource> resourcesWithSameRegex = entry.getValue();

      List<Resource> childResources = Lists.newArrayList();
      for (final Resource res : resourcesWithSameRegex) {
        childResources.addAll(res.getChildResources());
      }

      List<RuntimeResource.Builder> childRuntimeResources = getRuntimeResources(childResources);
      runtimeResources.add(
          new RuntimeResource.Builder(
              resourcesWithSameRegex, childRuntimeResources, entry.getKey()));
    }
    return runtimeResources;
  }
  public CommandLineParameters clone() {
    CommandLineParameters ret;
    try {
      ret = (CommandLineParameters) super.clone();
    } catch (CloneNotSupportedException e) {
      return null;
    }

    if (this.routerIds != null) {
      ret.routerIds = Lists.newArrayList();
      ret.routerIds.addAll(this.routerIds);
    }

    return ret;
  }
 @Path("/bulk")
 @POST
 public Response postBulk(
     @Auth AuthModelT authModel, List<ModelT> models, @Context HttpServletRequest request)
     throws HttpCodeException {
   // TODO: Implement transaction
   List<ModelT> insertedModels = Lists.newArrayList();
   for (ModelT model : models) {
     insertedModels.add(man.insert(getAuthContext(authModel), model));
   }
   return res()
       .success(
           res()
               .listData(
                   ResultsSet.<ModelT>builder()
                       .results(insertedModels)
                       .limit(insertedModels.size())
                       .offset(0)
                       .total((long) insertedModels.size())
                       .build()));
 }
예제 #9
0
/**
 * Parent class for all tests written using Jersey test framework.
 *
 * @author Paul Sandoz (paul.sandoz at oracle.com)
 * @author Srinivas Bhimisetty
 * @author Pavel Bucek (pavel.bucek at oracle.com)
 * @author Michal Gajdos (michal.gajdos at oracle.com)
 */
@SuppressWarnings("UnusedDeclaration")
public abstract class JerseyTest {

  private static final Logger LOGGER = Logger.getLogger(JerseyTest.class.getName());
  /** Holds the default test container factory class to be used for running the tests. */
  private static Class<? extends TestContainerFactory> testContainerFactoryClass;
  /**
   * The test container factory which creates an instance of the test container on which the tests
   * would be run.
   */
  private TestContainerFactory testContainerFactory;
  /** The test container on which the tests would be run. */
  private final TestContainer tc;

  private final ApplicationHandler application;
  private final AtomicReference<Client> client = new AtomicReference<Client>(null);
  /**
   * JerseyTest property bag that can be used to configure the test behavior. These properties can
   * be overridden with a system property.
   */
  private final Map<String, String> propertyMap = Maps.newHashMap();
  /**
   * JerseyTest forced property bag that can be used to configure the test behavior. These property
   * cannot be overridden with a system property.
   */
  private final Map<String, String> forcedPropertyMap = Maps.newHashMap();

  private Handler logHandler;
  private final List<LogRecord> loggedStartupRecords = Lists.newArrayList();
  private final List<LogRecord> loggedRuntimeRecords = Lists.newArrayList();
  private final Map<Logger, Level> logLevelMap = Maps.newIdentityHashMap();

  /**
   * An extending class must implement the {@link #configure()} method to provide an application
   * descriptor.
   *
   * @throws TestContainerException if the default test container factory cannot be obtained, or the
   *     application descriptor is not supported by the test container factory.
   */
  public JerseyTest() throws TestContainerException {
    ResourceConfig config = getResourceConfig(configure());
    config.register(
        new ServiceFinderBinder<TestContainerFactory>(
            TestContainerFactory.class, null, RuntimeType.SERVER));

    if (isLogRecordingEnabled()) {
      registerLogHandler();
    }
    this.application = new ApplicationHandler(config);
    this.tc = getContainer(application, getTestContainerFactory());
    if (isLogRecordingEnabled()) {
      loggedStartupRecords.addAll(loggedRuntimeRecords);
      loggedRuntimeRecords.clear();
      unregisterLogHandler();
    }
  }

  /**
   * Construct a new instance with a test container factory.
   *
   * <p>An extending class must implement the {@link #configure()} method to provide an application
   * descriptor.
   *
   * @param testContainerFactory the test container factory to use for testing.
   * @throws TestContainerException if the application descriptor is not supported by the test
   *     container factory.
   */
  public JerseyTest(TestContainerFactory testContainerFactory) {
    setTestContainerFactory(testContainerFactory);

    ResourceConfig config = getResourceConfig(configure());
    config.register(
        new ServiceFinderBinder<TestContainerFactory>(
            TestContainerFactory.class, null, RuntimeType.SERVER));
    if (isLogRecordingEnabled()) {
      registerLogHandler();
    }
    this.application = new ApplicationHandler(config);
    this.tc = getContainer(application, testContainerFactory);
    if (isLogRecordingEnabled()) {
      loggedStartupRecords.addAll(loggedRuntimeRecords);
      loggedRuntimeRecords.clear();
      unregisterLogHandler();
    }
  }

  /**
   * Construct a new instance with an application descriptor that defines how the test container is
   * configured.
   *
   * @param jaxrsApplication an application describing how to configure the test container.
   * @throws TestContainerException if the default test container factory cannot be obtained, or the
   *     application descriptor is not supported by the test container factory.
   */
  public JerseyTest(Application jaxrsApplication) throws TestContainerException {
    ResourceConfig config = getResourceConfig(jaxrsApplication);
    config.register(
        new ServiceFinderBinder<TestContainerFactory>(
            TestContainerFactory.class, null, RuntimeType.SERVER));
    if (isLogRecordingEnabled()) {
      registerLogHandler();
    }
    this.application = new ApplicationHandler(config);
    this.tc = getContainer(application, getTestContainerFactory());
    if (isLogRecordingEnabled()) {
      loggedStartupRecords.addAll(loggedRuntimeRecords);
      loggedRuntimeRecords.clear();
      unregisterLogHandler();
    }
  }

  /**
   * Construct a new instance with an {@link Application} class.
   *
   * @param jaxrsApplicationClass an application describing how to configure the test container.
   * @throws TestContainerException if the default test container factory cannot be obtained, or the
   *     application descriptor is not supported by the test container factory.
   */
  public JerseyTest(Class<? extends Application> jaxrsApplicationClass)
      throws TestContainerException {
    ResourceConfig config = ResourceConfig.forApplicationClass(jaxrsApplicationClass);
    config.register(
        new ServiceFinderBinder<TestContainerFactory>(
            TestContainerFactory.class, null, RuntimeType.SERVER));
    if (isLogRecordingEnabled()) {
      registerLogHandler();
    }
    this.application = new ApplicationHandler(config);
    this.tc = getContainer(application, getTestContainerFactory());
    if (isLogRecordingEnabled()) {
      loggedStartupRecords.addAll(loggedRuntimeRecords);
      loggedRuntimeRecords.clear();
      unregisterLogHandler();
    }
  }

  private ResourceConfig getResourceConfig(Application app) {
    return ResourceConfig.forApplication(app);
  }

  /**
   * Programmatically enable a feature with a given name. Enabling of the feature may be overridden
   * via a system property.
   *
   * @param featureName name of the enabled feature.
   */
  protected final void enable(String featureName) {
    // TODO: perhaps we could reuse the resource config for the test properties?
    propertyMap.put(featureName, Boolean.TRUE.toString());
  }

  /**
   * Programmatically disable a feature with a given name. Disabling of the feature may be
   * overridden via a system property.
   *
   * @param featureName name of the disabled feature.
   */
  protected final void disable(String featureName) {
    propertyMap.put(featureName, Boolean.FALSE.toString());
  }

  /**
   * Programmatically force-enable a feature with a given name. Force-enabling of the feature cannot
   * be overridden via a system property. Use with care!
   *
   * @param featureName name of the force-enabled feature.
   */
  protected final void forceEnable(String featureName) {
    forcedPropertyMap.put(featureName, Boolean.TRUE.toString());
  }

  /**
   * Programmatically force-disable a feature with a given name. Force-disabling of the feature
   * cannot be overridden via a system property. Use with care!
   *
   * @param featureName name of the force-disabled feature.
   */
  protected final void forceDisable(String featureName) {
    forcedPropertyMap.put(featureName, Boolean.FALSE.toString());
  }

  /**
   * Programmatically set a value of a property with a given name. The property value may be
   * overridden via a system property.
   *
   * @param propertyName name of the property.
   * @param value property value.
   */
  protected final void set(String propertyName, Object value) {
    set(propertyName, value.toString());
  }

  /**
   * Programmatically set a value of a property with a given name. The property value may be
   * overridden via a system property.
   *
   * @param propertyName name of the property.
   * @param value property value.
   */
  protected final void set(String propertyName, String value) {
    propertyMap.put(propertyName, value);
  }

  /**
   * Programmatically force-set a value of a property with a given name. The force-set property
   * value cannot be overridden via a system property.
   *
   * @param propertyName name of the property.
   * @param value property value.
   */
  protected final void forceSet(String propertyName, String value) {
    forcedPropertyMap.put(propertyName, value);
  }

  /**
   * Check if the Jersey test boolean property (flag) has been set to {@code true}.
   *
   * @param propertyName name of the Jersey test boolean property.
   * @return {@code true} if the test property has been enabled, {@code false} otherwise.
   */
  protected final boolean isEnabled(String propertyName) {
    return Boolean.valueOf(getProperty(propertyName));
  }

  private String getProperty(String propertyName) {
    if (forcedPropertyMap.containsKey(propertyName)) {
      return forcedPropertyMap.get(propertyName);
    }

    final Properties systemProperties =
        AccessController.doPrivileged(PropertiesHelper.getSystemProperties());
    if (systemProperties.containsKey(propertyName)) {
      return systemProperties.getProperty(propertyName);
    }

    if (propertyMap.containsKey(propertyName)) {
      return propertyMap.get(propertyName);
    }

    return null;
  }

  /**
   * Return an JAX-RS application that defines how the application in the test container is
   * configured.
   *
   * <p>If a constructor is utilized that does not supply an application descriptor then this method
   * must be overridden to return an application descriptor, otherwise an {@link
   * UnsupportedOperationException} exception will be thrown.
   *
   * <p>If a constructor is utilized that does supply an application descriptor then this method
   * does not require to be overridden and will not be invoked.
   *
   * @return the application descriptor.
   */
  protected Application configure() {
    throw new UnsupportedOperationException(
        "The configure method must be implemented by the extending class");
  }

  /**
   * Sets the test container factory to to be used for testing.
   *
   * @param testContainerFactory the test container factory to to be used for testing.
   */
  protected final void setTestContainerFactory(TestContainerFactory testContainerFactory) {
    this.testContainerFactory = testContainerFactory;
  }

  /**
   * Returns an instance of {@link TestContainerFactory} class. This instance can be set by a
   * constructor ({@link #JerseyTest(org.glassfish.jersey.test.spi.TestContainerFactory)}, as an
   * application {@link Providers Provider} or the {@link TestContainerFactory} class can be set as
   * a {@value org.glassfish.jersey.test.TestProperties#CONTAINER_FACTORY} property.
   *
   * @return an instance of {@link TestContainerFactory} class.
   * @throws TestContainerException if the initialization of {@link TestContainerFactory} instance
   *     is not successful.
   */
  protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
    if (testContainerFactory == null) {
      if (testContainerFactoryClass == null) {

        final String tcfClassName = getProperty(TestProperties.CONTAINER_FACTORY);
        if ((tcfClassName == null)) {
          Set<TestContainerFactory> testContainerFactories =
              Providers.getProviders(application.getServiceLocator(), TestContainerFactory.class);

          if (testContainerFactories.size() >= 1) {
            // if default factory is present, use it.
            for (TestContainerFactory tcFactory : testContainerFactories) {

              if (tcFactory.getClass().getName().equals(TestProperties.DEFAULT_CONTAINER_FACTORY)) {
                LOGGER.log(
                    Level.CONFIG,
                    "Found multiple TestContainerFactory implementations, using default {0}",
                    tcFactory.getClass().getName());

                testContainerFactoryClass = tcFactory.getClass(); // is this necessary?
                return tcFactory;
              }
            }

            if (testContainerFactories.size() != 1) {
              LOGGER.log(
                  Level.WARNING,
                  "Found multiple TestContainerFactory implementations, using {0}",
                  testContainerFactories.iterator().next().getClass().getName());
            }

            testContainerFactoryClass = testContainerFactories.iterator().next().getClass();
            return testContainerFactories.iterator().next();
          }
        } else {
          final Class<Object> tfClass =
              AccessController.doPrivileged(ReflectionHelper.classForNamePA(tcfClassName, null));
          if (tfClass == null) {
            throw new TestContainerException(
                "The default test container factory class name, "
                    + tcfClassName
                    + ", cannot be loaded");
          }
          try {
            testContainerFactoryClass = tfClass.asSubclass(TestContainerFactory.class);
          } catch (ClassCastException ex) {
            throw new TestContainerException(
                "The default test container factory class, "
                    + tcfClassName
                    + ", is not an instance of TestContainerFactory",
                ex);
          }
        }
      }

      try {
        return testContainerFactoryClass.newInstance();
      } catch (Exception ex) {
        throw new TestContainerException(
            "The default test container factory, "
                + testContainerFactoryClass
                + ", could not be instantiated",
            ex);
      }
    }

    return testContainerFactory;
  }

  /**
   * Create a web resource whose URI refers to the base URI the Web application is deployed at.
   *
   * @return the created web resource
   */
  public WebTarget target() {
    return client().target(tc.getBaseUri());
  }

  /**
   * Create a web resource whose URI refers to the base URI the Web application is deployed at plus
   * the path specified in the argument.
   *
   * <p>This method is an equivalent of calling {@code target().path(path)}.
   *
   * @param path Relative path (from base URI) this target should point to.
   * @return the created web resource
   */
  public WebTarget target(String path) {
    return target().path(path);
  }

  /**
   * Get the client that is configured for this test.
   *
   * @return the configured client.
   */
  public Client client() {
    return client.get();
  }

  /**
   * Set up the test by invoking {@link TestContainer#start() } on the test container obtained from
   * the test container factory.
   *
   * @throws Exception if an exception is thrown during setting up the test environment.
   */
  @Before
  public void setUp() throws Exception {
    if (isLogRecordingEnabled()) {
      loggedRuntimeRecords.clear();
      registerLogHandler();
    }

    tc.start();
    Client old = client.getAndSet(getClient(tc, application));
    close(old);
  }

  /**
   * Tear down the test by invoking {@link TestContainer#stop() } on the test container obtained
   * from the test container factory.
   *
   * @throws Exception if an exception is thrown during tearing down the test environment.
   */
  @After
  public void tearDown() throws Exception {
    if (isLogRecordingEnabled()) {
      loggedRuntimeRecords.clear();
      unregisterLogHandler();
    }

    try {
      tc.stop();
    } finally {
      Client old = client.getAndSet(null);
      close(old);
    }
  }

  private TestContainer getContainer(ApplicationHandler application, TestContainerFactory tcf) {
    if (application == null) {
      throw new IllegalArgumentException("The application cannot be null");
    }

    return tcf.create(getBaseUri(), application);
  }

  /**
   * Creates an instance of {@link Client}.
   *
   * <p>Checks whether TestContainer provides ClientConfig instance and if not, empty new {@link
   * org.glassfish.jersey.client.ClientConfig} instance will be used to create new client instance.
   *
   * <p>This method is called exactly once when JerseyTest is created.
   *
   * @param tc instance of {@link TestContainer}
   * @param applicationHandler instance of {@link ApplicationHandler}
   * @return A Client instance.
   */
  protected Client getClient(TestContainer tc, ApplicationHandler applicationHandler) {
    ClientConfig cc = tc.getClientConfig();

    if (cc == null) {
      cc = new ClientConfig();
    }

    // check if logging is required
    if (isEnabled(TestProperties.LOG_TRAFFIC)) {
      cc.register(new LoggingFilter(LOGGER, isEnabled(TestProperties.DUMP_ENTITY)));
    }

    configureClient(cc);

    return ClientBuilder.newClient(cc);
  }

  /**
   * Can be overridden by subclasses to conveniently configure the client instance used by the test.
   *
   * <p>Default implementation of the method is "no-op".
   *
   * @param config Jersey test client configuration that can be modified before the client is
   *     created.
   */
  protected void configureClient(ClientConfig config) {
    // nothing
  }

  /**
   * Returns the base URI of the application.
   *
   * @return The base URI of the application
   */
  protected URI getBaseUri() {
    return UriBuilder.fromUri("http://localhost/").port(getPort()).build();
  }

  /**
   * Get the port to be used for test application deployments.
   *
   * @return The HTTP port of the URI
   */
  protected final int getPort() {
    final String value = getProperty(TestProperties.CONTAINER_PORT);
    if (value != null) {

      try {
        final int i = Integer.parseInt(value);
        if (i <= 0) {
          throw new NumberFormatException("Value not positive.");
        }
        return i;
      } catch (NumberFormatException e) {
        LOGGER.log(
            Level.CONFIG,
            "Value of "
                + TestProperties.CONTAINER_PORT
                + " property is not a valid positive integer ["
                + value
                + "]."
                + " Reverting to default ["
                + TestProperties.DEFAULT_CONTAINER_PORT
                + "].",
            e);
      }
    }
    return TestProperties.DEFAULT_CONTAINER_PORT;
  }

  /**
   * Get stored {@link LogRecord log records} if enabled by setting {@link
   * TestProperties#RECORD_LOG_LEVEL} or an empty list.
   *
   * @return list of log records or an empty list.
   */
  protected List<LogRecord> getLoggedRecords() {
    final List<LogRecord> logRecords = Lists.newArrayList();
    logRecords.addAll(loggedStartupRecords);
    logRecords.addAll(loggedRuntimeRecords);
    return logRecords;
  }

  /**
   * Get last stored {@link LogRecord log record} if enabled by setting {@link
   * TestProperties#RECORD_LOG_LEVEL} or {@code null}.
   *
   * @return last stored {@link LogRecord log record} or {@code null}.
   */
  protected LogRecord getLastLoggedRecord() {
    final List<LogRecord> loggedRecords = getLoggedRecords();
    return loggedRecords.isEmpty() ? null : loggedRecords.get(loggedRecords.size() - 1);
  }

  /**
   * Retrieves a list of root loggers.
   *
   * @return list of root loggers.
   */
  private Set<Logger> getRootLoggers() {
    final LogManager logManager = LogManager.getLogManager();
    final Enumeration<String> loggerNames = logManager.getLoggerNames();

    final Set<Logger> rootLoggers = Sets.newHashSet();

    while (loggerNames.hasMoreElements()) {
      Logger logger = logManager.getLogger(loggerNames.nextElement());
      if (logger != null) {
        while (logger.getParent() != null) {
          logger = logger.getParent();
        }
        rootLoggers.add(logger);
      }
    }

    return rootLoggers;
  }

  /** Register {@link Handler log handler} to the list of root loggers. */
  private void registerLogHandler() {
    final String recordLogLevel = getProperty(TestProperties.RECORD_LOG_LEVEL);
    final int recordLogLevelInt = Integer.valueOf(recordLogLevel);
    final Level level = Level.parse(recordLogLevel);

    logLevelMap.clear();

    for (final Logger root : getRootLoggers()) {
      logLevelMap.put(root, root.getLevel());

      if (root.getLevel().intValue() > recordLogLevelInt) {
        root.setLevel(level);
      }

      root.addHandler(getLogHandler());
    }
  }

  /** Un-register {@link Handler log handler} from the list of root loggers. */
  private void unregisterLogHandler() {
    for (final Logger root : getRootLoggers()) {
      root.setLevel(logLevelMap.get(root));
      root.removeHandler(getLogHandler());
    }
    logHandler = null;
  }

  /**
   * Return {@code true} if log recoding is enabled.
   *
   * @return {@code true} if log recoding is enabled, {@code false} otherwise.
   */
  private boolean isLogRecordingEnabled() {
    return getProperty(TestProperties.RECORD_LOG_LEVEL) != null;
  }

  /**
   * Retrieves {@link Handler log handler} capable of storing {@link LogRecord logged records}.
   *
   * @return log handler.
   */
  private Handler getLogHandler() {
    if (logHandler == null) {
      final Integer logLevel = Integer.valueOf(getProperty(TestProperties.RECORD_LOG_LEVEL));
      logHandler =
          new Handler() {

            @Override
            public void publish(LogRecord record) {
              final String loggerName = record.getLoggerName();

              if (record.getLevel().intValue() >= logLevel
                  && loggerName.startsWith("org.glassfish.jersey")
                  && !loggerName.startsWith("org.glassfish.jersey.test")) {
                loggedRuntimeRecords.add(record);
              }
            }

            @Override
            public void flush() {}

            @Override
            public void close() throws SecurityException {}
          };
    }
    return logHandler;
  }

  /**
   * Utility method that safely closes a response without throwing an exception.
   *
   * @param responses responses to close. Each response may be {@code null}.
   * @since 2.5
   */
  public final void close(final Response... responses) {
    if (responses == null || responses.length == 0) {
      return;
    }

    for (Response response : responses) {
      if (response == null) {
        continue;
      }
      try {
        response.close();
      } catch (Throwable t) {
        LOGGER.log(Level.WARNING, "Error closing a response.", t);
      }
    }
  }

  /**
   * Utility method that safely closes a client instance without throwing an exception.
   *
   * @param clients client instances to close. Each instance may be {@code null}.
   * @since 2.5
   */
  public final void close(final Client... clients) {
    if (clients == null || clients.length == 0) {
      return;
    }

    for (Client c : clients) {
      if (c == null) {
        continue;
      }
      try {
        c.close();
      } catch (Throwable t) {
        LOGGER.log(Level.WARNING, "Error closing a client instance.", t);
      }
    }
  }
}
예제 #10
0
 /**
  * Get stored {@link LogRecord log records} if enabled by setting {@link
  * TestProperties#RECORD_LOG_LEVEL} or an empty list.
  *
  * @return list of log records or an empty list.
  */
 protected List<LogRecord> getLoggedRecords() {
   final List<LogRecord> logRecords = Lists.newArrayList();
   logRecords.addAll(loggedStartupRecords);
   logRecords.addAll(loggedRuntimeRecords);
   return logRecords;
 }
예제 #11
0
 public Builder() {
   this.knownIdentities = Lists.newArrayList();
 }
  // Application integration
  @GET
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  public Response getHealthMeasureHistory(/*
	 * @QueryParam("before") String before,
	 *
	 * @QueryParam("after") String after
	 */ ) {
    System.out.println("Request #6: GET /person/{id}/{measureType}");
    List<HealthMeasureHistory> measureHistory = this.getHMhistoryByUserAndType(id, measureType);
    // if (before == null || after == null) {
    // System.out
    // .println("GET person/{personID}/{measureType} aka request 6");
    // if (measureHistory == null)
    // throw new RuntimeException(
    // "Get: HealthMeasureHistory of person with " + id
    // + " for measuretype of type" + measureTypeId
    // + " not found");
    // return measureHistory;
    // } else {
    // Date Dbefore, Dafter;
    // try {
    // long numb_before = Long.parseLong(before);
    // long numb_after = Long.parseLong(after);
    //
    // Dbefore = new Date(numb_before);
    // Dafter = new Date(numb_after);
    // } catch (Exception e) {
    // e.printStackTrace();
    // throw new RuntimeException(
    // "Get: HealthMeasureHistory of person with "
    // + id
    // + " for measuretype of type"
    // + measureTypeId
    // + "into selected date interval"
    // +
    // " not possible, probably you passed invalid before and after arguments");
    // }
    // System.out
    // .println("GET person/{personID}/{measureType}?before={beforeDate}&after={afterDate} aka
    // request 11");
    // if (Dbefore.compareTo(Dafter) <= 0) {
    // throw new RuntimeException(
    // "Get: HealthMeasureHistory of person with "
    // + id
    // + " for measuretype of type"
    // + measureTypeId
    // + "into selected date interval"
    // +
    // " not possible, probably you passed after param bigger than before");
    // }
    // for (HealthMeasureHistory HMh : measureHistory) {
    // if (HMh.getTimestamp().compareTo(Dbefore) > 0
    // || HMh.getTimestamp().compareTo(Dafter) < 0) {
    // measureHistory.remove(HMh);
    // System.out.println("removing a date from the set \n"
    // + HMh.toString());
    // }
    // }
    // // bug here
    // return measureHistory;
    //
    // }
    // System.out.println("GET person/{personID}/{measureType} aka request 6");
    if (!measureHistory.isEmpty()) {
      System.out.println("we got something");
    } else {
      return Response.status(404)
          .entity(
              "Get: HealthMeasureHistory for person with "
                  + id
                  + " and measuretype "
                  + measureType
                  + " not found")
          .build();
    }
    GenericEntity<List<HealthMeasureHistory>> entity =
        new GenericEntity<List<HealthMeasureHistory>>(Lists.newArrayList(measureHistory)) {};
    return Response.ok(entity).build();
  }
예제 #13
0
/** @author Michal Gajdos */
public class JsonProcessingResourceTest extends JerseyTest {

  private static final List<JsonObject> documents = Lists.newArrayList();

  static {
    documents.add(
        Json.createObjectBuilder()
            .add("name", "Jersey")
            .add("site", "http://jersey.java.net")
            .build());
    documents.add(
        Json.createObjectBuilder()
            .add("age", 33)
            .add("phone", "158158158")
            .add("name", "Foo")
            .build());
    documents.add(
        Json.createObjectBuilder()
            .add("name", "JSON-P")
            .add("site", "http://jsonp.java.net")
            .build());
  }

  @Override
  protected Application configure() {
    return new MyApplication();
  }

  @Override
  protected URI getBaseUri() {
    return UriBuilder.fromUri(super.getBaseUri()).path("json-processing-webapp").build();
  }

  @Test
  public void testStoreGetRemoveDocument() throws Exception {
    final JsonObject document = documents.get(0);

    // Store.
    final Response response =
        target("document").request(MediaType.APPLICATION_JSON).post(Entity.json(document));
    assertEquals(200, response.getStatus());

    final List<JsonNumber> ids = response.readEntity(JsonArray.class).getValuesAs(JsonNumber.class);
    assertEquals(1, ids.size());

    // Get.
    final String id = ids.get(0).toString();
    final WebTarget documentTarget = target("document").path(id);
    final JsonObject storedDocument =
        documentTarget.request(MediaType.APPLICATION_JSON).get(JsonObject.class);
    assertEquals(document, storedDocument);

    // Remove.
    final JsonObject removedDocument =
        documentTarget.request(MediaType.APPLICATION_JSON).delete(JsonObject.class);
    assertEquals(document, removedDocument);

    // Get.
    final Response errorResponse = documentTarget.request(MediaType.APPLICATION_JSON).get();
    assertEquals(204, errorResponse.getStatus());
  }

  private JsonArray getDocumentJsonArray() {
    final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
    for (final JsonObject document : documents) {
      arrayBuilder.add(document);
    }
    return arrayBuilder.build();
  }

  @Test
  public void testStoreDocuments() throws Exception {
    final Response response =
        target("document/multiple")
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(getDocumentJsonArray()));

    assertEquals(200, response.getStatus());

    final List<JsonNumber> ids = response.readEntity(JsonArray.class).getValuesAs(JsonNumber.class);
    assertEquals(documents.size(), ids.size());

    // Remove All.
    target("document").request().delete();
  }

  @Test
  public void testFilterDocuments() throws Exception {
    // Store documents.
    target("document/multiple")
        .request(MediaType.APPLICATION_JSON_TYPE)
        .post(Entity.json(getDocumentJsonArray()));

    // Filter.
    JsonArray filter = Json.createArrayBuilder().add("site").build();
    JsonArray filtered =
        target("document/filter")
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(filter), JsonArray.class);

    checkFilteredDocuments(filtered, 2, "site");

    filter = Json.createArrayBuilder().add("site").add("age").build();
    filtered =
        target("document/filter")
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(filter), JsonArray.class);

    checkFilteredDocuments(filtered, 3, "site", "age");

    // Remove All.
    target("document").request().delete();
  }

  private void checkFilteredDocuments(
      final JsonArray filtered, final int size, final String... properties) {
    assertEquals(size, filtered.size());

    final HashSet<String> strings = Sets.newHashSet(properties);
    for (final JsonObject document : filtered.getValuesAs(JsonObject.class)) {
      for (final String property : document.keySet()) {
        assertTrue(strings.contains(property));
      }
    }
  }
}