Search in sources :

Example 1 with InvoiceLineCollection

use of org.folio.rest.jaxrs.model.InvoiceLineCollection in project mod-invoice by folio-org.

the class CreateInvoiceEventHandler method preparePayloadWithMappedInvoiceLines.

private void preparePayloadWithMappedInvoiceLines(DataImportEventPayload dataImportEventPayload) {
    if (dataImportEventPayload.getContext().get(INVOICE_LINES_KEY) != null) {
        List<InvoiceLine> invoiceLines = mapInvoiceLinesArrayToList(new JsonArray(dataImportEventPayload.getContext().get(INVOICE_LINES_KEY)));
        InvoiceLineCollection invoiceLineCollection = new InvoiceLineCollection().withInvoiceLines(invoiceLines).withTotalRecords(invoiceLines.size());
        dataImportEventPayload.getContext().put(INVOICE_LINES_KEY, Json.encode(invoiceLineCollection));
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection)

Example 2 with InvoiceLineCollection

use of org.folio.rest.jaxrs.model.InvoiceLineCollection in project mod-invoice by folio-org.

the class MockServer method handleGetInvoiceLines.

private void handleGetInvoiceLines(RoutingContext ctx) {
    logger.info("handleGetInvoiceLines got: {}?{}", ctx.request().path(), ctx.request().query());
    String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
    String queryParam = StringUtils.trimToEmpty(ctx.request().getParam(QUERY));
    addServerRqQuery(INVOICE_LINES, queryParam);
    if (queryParam.contains(BAD_QUERY)) {
        serverResponse(ctx, 400, APPLICATION_JSON, Response.Status.BAD_REQUEST.getReasonPhrase());
    } else if (queryParam.contains(ID_FOR_INTERNAL_SERVER_ERROR) || GET_INVOICE_LINES_ERROR_TENANT.equals(tenant)) {
        serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
    } else {
        Supplier<List<InvoiceLine>> getFromFile = () -> {
            try {
                return new JsonObject(getMockData(INVOICE_LINES_COLLECTION)).mapTo(InvoiceLineCollection.class).getInvoiceLines();
            } catch (IOException e) {
                return Collections.emptyList();
            }
        };
        String invoiceId = EMPTY;
        List<String> includedLineIds = Collections.emptyList();
        List<String> excludedLineIds = getRqRsEntries(HttpMethod.DELETE, INVOICE_LINES).stream().map(json -> json.getString(AbstractHelper.ID)).collect(toList());
        if (queryParam.contains(INVOICE_ID)) {
            Matcher matcher = Pattern.compile(".*" + INVOICE_ID + "==(\\S[^)]+).*").matcher(queryParam);
            invoiceId = matcher.find() ? matcher.group(1) : EMPTY;
            excludedLineIds.addAll(extractIdsFromQuery(queryParam, "<>"));
            logger.debug("Filtering lines by invoice id={} with id not IN {}", invoiceId, excludedLineIds);
        } else if (queryParam.startsWith("id==")) {
            includedLineIds = extractIdsFromQuery(queryParam);
            logger.debug("Filtering lines by id IN {}", includedLineIds);
        }
        InvoiceLineCollection invoiceLineCollection = new InvoiceLineCollection();
        List<InvoiceLine> invoiceLines = getMockEntries(INVOICE_LINES, InvoiceLine.class).orElseGet(getFromFile);
        invoiceLineCollection.setInvoiceLines(invoiceLines);
        Iterator<InvoiceLine> iterator = invoiceLines.iterator();
        while (iterator.hasNext()) {
            InvoiceLine invoiceLine = iterator.next();
            String id = invoiceLine.getId();
            if (excludedLineIds.contains(id) || (includedLineIds.isEmpty() ? !invoiceId.contains(invoiceLine.getInvoiceId()) : !includedLineIds.contains(id))) {
                iterator.remove();
            }
        }
        invoiceLineCollection.setTotalRecords(invoiceLineCollection.getInvoiceLines().size());
        JsonObject invoiceLinesJson = JsonObject.mapFrom(invoiceLineCollection);
        logger.info(invoiceLinesJson.encodePrettily());
        addServerRqRsData(HttpMethod.GET, INVOICE_LINES, invoiceLinesJson);
        serverResponse(ctx, 200, APPLICATION_JSON, invoiceLinesJson.encode());
    }
}
Also used : InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Matcher(java.util.regex.Matcher) Iterator(java.util.Iterator) JsonObject(io.vertx.core.json.JsonObject) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) IOException(java.io.IOException)

Example 3 with InvoiceLineCollection

use of org.folio.rest.jaxrs.model.InvoiceLineCollection in project mod-invoice by folio-org.

the class CreateInvoiceEventHandlerTest method shouldPublishDiErrorEventWhenPostInvoiceToStorageFailed.

@Test
public void shouldPublishDiErrorEventWhenPostInvoiceToStorageFailed() throws InterruptedException {
    // given
    ProfileSnapshotWrapper profileSnapshotWrapper = buildProfileSnapshotWrapper(jobProfile, actionProfile, mappingProfile);
    addMockEntry(JOB_PROFILE_SNAPSHOTS_MOCK, profileSnapshotWrapper);
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT)).withId(UUID.randomUUID().toString());
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(ERROR_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext);
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), ERROR_TENANT, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> kafkaRecord = new KeyValue<>("test-key", Json.encode(event));
    kafkaRecord.addHeader(RECORD_ID_HEADER, record.getId(), UTF_8);
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(kafkaRecord)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), ERROR_TENANT, DI_ERROR.value());
    List<String> observedValues = kafkaCluster.observeValues(ObserveKeyValues.on(topicToObserve, 1).with(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID).observeFor(30, TimeUnit.SECONDS).build());
    Event publishedEvent = Json.decodeValue(observedValues.get(0), Event.class);
    DataImportEventPayload eventPayload = Json.decodeValue(publishedEvent.getEventPayload(), DataImportEventPayload.class);
    assertEquals(DI_INVOICE_CREATED.value(), eventPayload.getEventsChain().get(eventPayload.getEventsChain().size() - 1));
    assertNotNull(eventPayload.getContext().get(ERROR_MSG_KEY));
    assertNotNull(eventPayload.getContext().get(INVOICE.value()));
    assertNotNull(eventPayload.getContext().get(INVOICE_LINES_KEY));
    assertNotNull(eventPayload.getContext().get(EDIFACT_INVOICE.value()));
    Record sourceRecord = Json.decodeValue(eventPayload.getContext().get(EDIFACT_INVOICE.value()), Record.class);
    assertNull(sourceRecord.getParsedRecord());
    assertNull(sourceRecord.getRawRecord());
    InvoiceLineCollection invoiceLineCollection = Json.decodeValue(eventPayload.getContext().get(INVOICE_LINES_KEY), InvoiceLineCollection.class);
    assertEquals(3, invoiceLineCollection.getTotalRecords());
    assertEquals(3, invoiceLineCollection.getInvoiceLines().size());
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) HashMap(java.util.HashMap) ParsedRecord(org.folio.ParsedRecord) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) DataImportEventPayload(org.folio.DataImportEventPayload) Event(org.folio.rest.jaxrs.model.Event) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Test(org.junit.jupiter.api.Test)

Example 4 with InvoiceLineCollection

use of org.folio.rest.jaxrs.model.InvoiceLineCollection in project mod-invoice by folio-org.

the class CreateInvoiceEventHandlerTest method shouldNotLinkInvoiceLinesToPoLinesWhenMultiplePoLinesAreMatchedByRefNumber.

@Test
public void shouldNotLinkInvoiceLinesToPoLinesWhenMultiplePoLinesAreMatchedByRefNumber() throws IOException, InterruptedException {
    // given
    PoLine poLine1 = Json.decodeValue(getMockData(String.format(MOCK_DATA_PATH_PATTERN, PO_LINES_MOCK_DATA_PATH, PO_LINE_ID_1)), PoLine.class);
    PoLine poLine3 = Json.decodeValue(getMockData(String.format(MOCK_DATA_PATH_PATTERN, PO_LINES_MOCK_DATA_PATH, PO_LINE_ID_3)), PoLine.class);
    when(mockOrderLinesRestClient.get(any(), any(RequestContext.class), eq(PoLineCollection.class))).thenReturn(CompletableFuture.completedFuture(new PoLineCollection())).thenReturn(CompletableFuture.completedFuture(new PoLineCollection().withPoLines(List.of(poLine1, poLine3))));
    ProfileSnapshotWrapper profileSnapshotWrapper = buildProfileSnapshotWrapper(jobProfile, actionProfile, mappingProfileWithPoLineSyntax);
    addMockEntry(JOB_PROFILE_SNAPSHOTS_MOCK, profileSnapshotWrapper);
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT)).withId(UUID.randomUUID().toString());
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext);
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> kafkaRecord = new KeyValue<>("test-key", Json.encode(event));
    kafkaRecord.addHeader(RECORD_ID_HEADER, record.getId(), UTF_8);
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(kafkaRecord)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, DI_COMPLETED.value());
    List<String> observedValues = kafkaCluster.observeValues(ObserveKeyValues.on(topicToObserve, 1).with(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID).observeFor(30, TimeUnit.SECONDS).build());
    Event obtainedEvent = Json.decodeValue(observedValues.get(0), Event.class);
    DataImportEventPayload eventPayload = Json.decodeValue(obtainedEvent.getEventPayload(), DataImportEventPayload.class);
    assertEquals(DI_INVOICE_CREATED.value(), eventPayload.getEventsChain().get(eventPayload.getEventsChain().size() - 1));
    assertNotNull(eventPayload.getContext().get(INVOICE_LINES_KEY));
    InvoiceLineCollection createdInvoiceLines = Json.decodeValue(eventPayload.getContext().get(INVOICE_LINES_KEY), InvoiceLineCollection.class);
    assertEquals(3, createdInvoiceLines.getTotalRecords());
    assertEquals(3, createdInvoiceLines.getInvoiceLines().size());
    assertNull(createdInvoiceLines.getInvoiceLines().get(0).getPoLineId());
    assertNull(createdInvoiceLines.getInvoiceLines().get(1).getPoLineId());
    assertNull(createdInvoiceLines.getInvoiceLines().get(2).getPoLineId());
    assertNotNull(createdInvoiceLines.getInvoiceLines().get(0).getDescription());
    assertNotNull(createdInvoiceLines.getInvoiceLines().get(1).getDescription());
    assertNotNull(createdInvoiceLines.getInvoiceLines().get(2).getDescription());
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) HashMap(java.util.HashMap) ParsedRecord(org.folio.ParsedRecord) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) DataImportEventPayload(org.folio.DataImportEventPayload) PoLineCollection(org.folio.rest.acq.model.orders.PoLineCollection) PoLine(org.folio.rest.acq.model.orders.PoLine) Event(org.folio.rest.jaxrs.model.Event) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Test(org.junit.jupiter.api.Test)

Example 5 with InvoiceLineCollection

use of org.folio.rest.jaxrs.model.InvoiceLineCollection in project mod-invoice by folio-org.

the class CreateInvoiceEventHandlerTest method shouldMatchPoLinesByPoLineNumberAndCreateInvoiceLinesWithDescriptionFromPoLines.

@Test
public void shouldMatchPoLinesByPoLineNumberAndCreateInvoiceLinesWithDescriptionFromPoLines() throws IOException, InterruptedException {
    // given
    PoLine poLine1 = Json.decodeValue(getMockData(String.format(MOCK_DATA_PATH_PATTERN, PO_LINES_MOCK_DATA_PATH, PO_LINE_ID_1)), PoLine.class);
    PoLine poLine3 = Json.decodeValue(getMockData(String.format(MOCK_DATA_PATH_PATTERN, PO_LINES_MOCK_DATA_PATH, PO_LINE_ID_3)), PoLine.class);
    PoLineCollection poLineCollection = new PoLineCollection().withPoLines(List.of(poLine1, poLine3));
    when(mockOrderLinesRestClient.get(any(), any(RequestContext.class), eq(PoLineCollection.class))).thenReturn(CompletableFuture.completedFuture(poLineCollection));
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(EDIFACT_PARSED_CONTENT)).withId(UUID.randomUUID().toString());
    ProfileSnapshotWrapper profileSnapshotWrapper = buildProfileSnapshotWrapper(jobProfile, actionProfile, mappingProfileWithPoLineSyntax);
    addMockEntry(JOB_PROFILE_SNAPSHOTS_MOCK, profileSnapshotWrapper);
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(EDIFACT_INVOICE.value(), Json.encode(record));
    payloadContext.put(JOB_PROFILE_SNAPSHOT_ID_KEY, profileSnapshotWrapper.getId());
    payloadContext.put(DATA_IMPORT_PAYLOAD_OKAPI_PERMISSIONS, Json.encode(Collections.singletonList(AcqDesiredPermissions.ASSIGN.getPermission())));
    payloadContext.put(DATA_IMPORT_PAYLOAD_OKAPI_USER_ID, USER_ID);
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_EDIFACT_RECORD_CREATED.value()).withTenant(DI_POST_INVOICE_LINES_SUCCESS_TENANT).withOkapiUrl(OKAPI_URL).withToken(TOKEN).withContext(payloadContext);
    String topic = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, dataImportEventPayload.getEventType());
    Event event = new Event().withEventPayload(Json.encode(dataImportEventPayload));
    KeyValue<String, String> kafkaRecord = new KeyValue<>("test-key", Json.encode(event));
    kafkaRecord.addHeader(RECORD_ID_HEADER, record.getId(), UTF_8);
    SendKeyValues<String, String> request = SendKeyValues.to(topic, Collections.singletonList(kafkaRecord)).useDefaults();
    // when
    kafkaCluster.send(request);
    // then
    String topicToObserve = KafkaTopicNameHelper.formatTopicName(KAFKA_ENV_VALUE, getDefaultNameSpace(), DI_POST_INVOICE_LINES_SUCCESS_TENANT, DI_COMPLETED.value());
    List<String> observedValues = kafkaCluster.observeValues(ObserveKeyValues.on(topicToObserve, 1).with(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID).observeFor(30, TimeUnit.SECONDS).build());
    Event obtainedEvent = Json.decodeValue(observedValues.get(0), Event.class);
    DataImportEventPayload eventPayload = Json.decodeValue(obtainedEvent.getEventPayload(), DataImportEventPayload.class);
    assertEquals(DI_INVOICE_CREATED.value(), eventPayload.getEventsChain().get(eventPayload.getEventsChain().size() - 1));
    assertNotNull(eventPayload.getContext().get(INVOICE.value()));
    Invoice createdInvoice = Json.decodeValue(eventPayload.getContext().get(INVOICE.value()), Invoice.class);
    assertNotNull(eventPayload.getContext().get(INVOICE_LINES_KEY));
    InvoiceLineCollection createdInvoiceLines = Json.decodeValue(eventPayload.getContext().get(INVOICE_LINES_KEY), InvoiceLineCollection.class);
    assertEquals(3, createdInvoiceLines.getTotalRecords());
    assertEquals(3, createdInvoiceLines.getInvoiceLines().size());
    createdInvoiceLines.getInvoiceLines().forEach(invLine -> assertEquals(createdInvoice.getId(), invLine.getInvoiceId()));
    assertEquals(poLine1.getId(), createdInvoiceLines.getInvoiceLines().get(0).getPoLineId());
    assertEquals(poLine3.getId(), createdInvoiceLines.getInvoiceLines().get(2).getPoLineId());
    assertNull(createdInvoiceLines.getInvoiceLines().get(1).getPoLineId());
    assertEquals(poLine1.getTitleOrPackage(), createdInvoiceLines.getInvoiceLines().get(0).getDescription());
    assertEquals(poLine3.getTitleOrPackage(), createdInvoiceLines.getInvoiceLines().get(2).getDescription());
    assertEquals("ACI MATERIALS JOURNAL - ONLINE   -", createdInvoiceLines.getInvoiceLines().get(1).getDescription());
}
Also used : KeyValue(net.mguenther.kafka.junit.KeyValue) Invoice(org.folio.rest.jaxrs.model.Invoice) HashMap(java.util.HashMap) ParsedRecord(org.folio.ParsedRecord) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) DataImportEventPayload(org.folio.DataImportEventPayload) PoLineCollection(org.folio.rest.acq.model.orders.PoLineCollection) PoLine(org.folio.rest.acq.model.orders.PoLine) Event(org.folio.rest.jaxrs.model.Event) ParsedRecord(org.folio.ParsedRecord) Record(org.folio.Record) RequestContext(org.folio.rest.core.models.RequestContext) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Test(org.junit.jupiter.api.Test)

Aggregations

InvoiceLineCollection (org.folio.rest.jaxrs.model.InvoiceLineCollection)13 HashMap (java.util.HashMap)10 Test (org.junit.jupiter.api.Test)10 DataImportEventPayload (org.folio.DataImportEventPayload)9 ParsedRecord (org.folio.ParsedRecord)9 Record (org.folio.Record)9 KeyValue (net.mguenther.kafka.junit.KeyValue)8 Event (org.folio.rest.jaxrs.model.Event)8 ProfileSnapshotWrapper (org.folio.rest.jaxrs.model.ProfileSnapshotWrapper)8 Invoice (org.folio.rest.jaxrs.model.Invoice)7 PoLine (org.folio.rest.acq.model.orders.PoLine)6 PoLineCollection (org.folio.rest.acq.model.orders.PoLineCollection)6 RequestContext (org.folio.rest.core.models.RequestContext)4 JsonArray (io.vertx.core.json.JsonArray)3 JsonObject (io.vertx.core.json.JsonObject)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)3 Vertx (io.vertx.core.Vertx)2 IOException (java.io.IOException)2