Search in sources :

Example 1 with Reference

use of ch.aaap.harvestclient.domain.reference.Reference in project harvest-client by 3AP-AG.

the class ExpensesApiUpdateTest method changeAll.

@Test
void changeAll() {
    String name = "test Expense";
    boolean billableByDefault = false;
    double defaultHourlyRate = 220.2;
    boolean defaultAddToFutureProject = true;
    boolean active = false;
    ExpenseUpdateInfo changes = ImmutableExpenseUpdateInfo.builder().project(project).expenseCategory(expenseCategory).totalCost(55.).spentDate(LocalDate.of(2001, 1, 1)).build();
    Expense updatedExpense = expensesApi.update(expense, changes);
    assertThat(updatedExpense).usingComparatorForType(Comparator.comparing(Reference::getId), Reference.class).isEqualToComparingOnlyGivenFields(changes, "project", "expenseCategory", "totalCost", "spentDate");
}
Also used : Reference(ch.aaap.harvestclient.domain.reference.Reference) ImmutableExpenseUpdateInfo(ch.aaap.harvestclient.domain.param.ImmutableExpenseUpdateInfo) ExpenseUpdateInfo(ch.aaap.harvestclient.domain.param.ExpenseUpdateInfo) HarvestTest(ch.aaap.harvestclient.HarvestTest) Test(org.junit.jupiter.api.Test)

Example 2 with Reference

use of ch.aaap.harvestclient.domain.reference.Reference in project harvest-client by 3AP-AG.

the class ExpensesApiCreateTest method create.

@Test
void create() {
    Expense creationInfo = ImmutableExpense.builder().project(project).expenseCategory(expenseCategory).spentDate(LocalDate.now()).user(user).totalCost(22.).build();
    expense = expensesApi.create(creationInfo);
    assertThat(expense).usingComparatorForType(Comparator.comparing(Reference::getId), Reference.class).isEqualToIgnoringNullFields(creationInfo);
    ExpenseCategoryReferenceDto expenseCategoryDto = (ExpenseCategoryReferenceDto) expense.getExpenseCategory();
    ExpenseCategory expectedCategory = harvest.expenseCategories().get(expenseCategory);
    assertThat(expenseCategoryDto).isEqualToComparingOnlyGivenFields(expectedCategory, "unitName", "unitPrice");
}
Also used : ExpenseCategoryReferenceDto(ch.aaap.harvestclient.domain.reference.dto.ExpenseCategoryReferenceDto) Reference(ch.aaap.harvestclient.domain.reference.Reference) HarvestTest(ch.aaap.harvestclient.HarvestTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Reference

use of ch.aaap.harvestclient.domain.reference.Reference 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

Reference (ch.aaap.harvestclient.domain.reference.Reference)3 HarvestTest (ch.aaap.harvestclient.HarvestTest)2 Test (org.junit.jupiter.api.Test)2 ExpenseUpdateInfo (ch.aaap.harvestclient.domain.param.ExpenseUpdateInfo)1 ImmutableExpenseUpdateInfo (ch.aaap.harvestclient.domain.param.ImmutableExpenseUpdateInfo)1 ExpenseCategoryReferenceDto (ch.aaap.harvestclient.domain.reference.dto.ExpenseCategoryReferenceDto)1 HarvestRuntimeException (ch.aaap.harvestclient.exception.HarvestRuntimeException)1 TypeAdapter (com.google.gson.TypeAdapter)1 JsonReader (com.google.gson.stream.JsonReader)1 JsonWriter (com.google.gson.stream.JsonWriter)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1