Search in sources :

Example 1 with HarvestRuntimeException

use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.

the class GsonTest method testReferenceAdapterDto.

@Test
void testReferenceAdapterDto() {
    ReferenceDtoAdapter referenceDtoAdapter = new ReferenceDtoAdapter();
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    // create a token for Reference<String>, which does not have a corresponding
    // ReferenceDTO class
    TypeToken<?> token = TypeToken.getParameterized(Reference.class, String.class);
    HarvestRuntimeException e = assertThrows(HarvestRuntimeException.class, () -> referenceDtoAdapter.create(gson, token));
    assertThat(e.getCause()).isOfAnyClassIn(ClassNotFoundException.class);
}
Also used : HarvestRuntimeException(ch.aaap.harvestclient.exception.HarvestRuntimeException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) HarvestTest(ch.aaap.harvestclient.HarvestTest) Test(org.junit.jupiter.api.Test)

Example 2 with HarvestRuntimeException

use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.

the class TestSetupUtil method loadConfiguration.

/**
 * Try to load a local conf file first, and override values with environment
 * variables if they are set.
 *
 * @param name
 *            the name of the configuration to load. Allowed characters are the
 *            same as for environment variables (A-Z, a-z, 0-9, _)
 * @return the configuration with env overrides applied
 */
private static Config loadConfiguration(String name) {
    // check for allowed characters
    if (name.matches("^[A-Za-z0-9_]]")) {
        throw new HarvestRuntimeException("Configuration name " + name + " contains invalid characters");
    }
    Config config = ConfigFactory.load(name);
    String harvestAccountId = System.getenv("HARVEST_ACCOUNT_ID_" + name.toUpperCase());
    String harvestAuthToken = System.getenv("HARVEST_AUTH_TOKEN_" + name.toUpperCase());
    if (harvestAccountId != null) {
        config = config.withValue("harvest.auth.accountId", ConfigValueFactory.fromAnyRef(harvestAccountId));
    }
    if (harvestAuthToken != null) {
        config = config.withValue("harvest.auth.token", ConfigValueFactory.fromAnyRef(harvestAuthToken));
    }
    return config;
}
Also used : HarvestRuntimeException(ch.aaap.harvestclient.exception.HarvestRuntimeException) Config(com.typesafe.config.Config)

Example 3 with HarvestRuntimeException

use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.

the class ExpensesApiImpl method attachReceipt.

@Override
public Expense attachReceipt(Reference<Expense> expenseReference, InputStream inputStream, String fileName) throws IOException {
    Path filePath = null;
    try {
        Path tempDirectory = Files.createTempDirectory("harvest-client");
        filePath = tempDirectory.resolve(fileName);
        if (!filePath.toAbsolutePath().startsWith(tempDirectory)) {
            throw new HarvestRuntimeException("Invalid filename: " + fileName);
        }
        Files.copy(inputStream, filePath);
        return attachReceipt(expenseReference, filePath.toFile());
    } finally {
        if (filePath != null) {
            Files.delete(filePath);
        }
    }
}
Also used : Path(java.nio.file.Path) HarvestRuntimeException(ch.aaap.harvestclient.exception.HarvestRuntimeException)

Example 4 with HarvestRuntimeException

use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.

the class ReferenceDtoAdapter method create.

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<? super T> rawType = type.getRawType();
    log.trace("Called create with {}", type);
    if (rawType != Reference.class) {
        return null;
    }
    Class<?> elementClass = parseReferenceType(type);
    String simpleName = elementClass.getSimpleName();
    String expectedClassName = simpleName + DTO_SUFFIX;
    try {
        Class<?> dtoClass = Class.forName(BaseReferenceDto.class.getPackage().getName() + "." + expectedClassName);
        log.trace("Got Dto class {}", dtoClass);
        TypeAdapter<?> delegate = gson.getAdapter(dtoClass);
        log.trace("Returning type adapter for {}", type);
        return new TypeAdapter<T>() {

            @Override
            public void write(JsonWriter out, T value) throws IOException {
                if (value == null) {
                    out.nullValue();
                    return;
                }
                Reference reference = (Reference) value;
                out.value(reference.getId());
            }

            @Override
            @SuppressWarnings("unchecked")
            public T read(JsonReader in) throws IOException {
                return (T) delegate.read(in);
            }
        };
    } catch (ClassNotFoundException e) {
        throw new HarvestRuntimeException("Could not find ReferenceDto. Reference<" + simpleName + "> needs a class named " + expectedClassName + " to existing in the same package as ReferenceDto", e);
    }
}
Also used : HarvestRuntimeException(ch.aaap.harvestclient.exception.HarvestRuntimeException) Reference(ch.aaap.harvestclient.domain.reference.Reference) TypeAdapter(com.google.gson.TypeAdapter) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter)

Aggregations

HarvestRuntimeException (ch.aaap.harvestclient.exception.HarvestRuntimeException)4 HarvestTest (ch.aaap.harvestclient.HarvestTest)1 Reference (ch.aaap.harvestclient.domain.reference.Reference)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeAdapter (com.google.gson.TypeAdapter)1 JsonReader (com.google.gson.stream.JsonReader)1 JsonWriter (com.google.gson.stream.JsonWriter)1 Config (com.typesafe.config.Config)1 Path (java.nio.file.Path)1 Test (org.junit.jupiter.api.Test)1