Search in sources :

Example 1 with DataSourceResolver

use of org.folio.gobi.DataSourceResolver in project mod-gobi by folio-org.

the class PostGobiOrdersHelperTest method testLookupDefaultOrderMappings.

@Test
public final void testLookupDefaultOrderMappings(TestContext context) {
    logger.info("Begin: Testing for Order Mappings to fetch default mappings if configuration Call fails");
    final Async async = context.async();
    final Vertx vertx = Vertx.vertx();
    final HttpServer server = vertx.createHttpServer();
    server.requestHandler(req -> {
        if (req.path().equals(PostGobiOrdersHelper.CONFIGURATION_ENDPOINT)) {
            req.response().setStatusCode(500).end("Unrecheable End point: " + req.path());
        }
    });
    int port = NetworkUtils.nextFreePort();
    server.listen(port, "localhost", ar -> {
        context.assertTrue(ar.succeeded());
        Map<String, String> okapiHeaders = new HashMap<>();
        okapiHeaders.put("X-Okapi-Url", "http://localhost:" + port);
        okapiHeaders.put("x-okapi-tenant", "testLookupOrderMappings");
        PostGobiOrdersHelper pgoh = new PostGobiOrdersHelper(GOBIIntegrationServiceResourceImpl.getHttpClient(okapiHeaders), null, okapiHeaders, vertx.getOrCreateContext());
        pgoh.lookupOrderMappings(OrderMappings.OrderType.fromValue("ListedElectronicMonograph")).thenAccept(map -> {
            context.assertNotNull(map);
            context.assertNotNull(map.get(Mapping.Field.CURRENCY));
            DataSourceResolver ds = map.get(Mapping.Field.CURRENCY);
            context.assertEquals("//ListPrice/Currency", ds.from);
            context.assertEquals("USD", ds.defValue);
            context.assertNotNull(map.get(Mapping.Field.LIST_UNIT_PRICE_ELECTRONIC));
            ds = map.get(Mapping.Field.LIST_UNIT_PRICE_ELECTRONIC);
            context.assertEquals("//ListPrice/Amount", ds.from);
            context.assertEquals("0", ds.defValue);
            try {
                Double result = (Double) ds.translation.apply(ds.defValue.toString()).get();
                context.assertEquals(0.0, result);
            } catch (Exception e) {
                logger.error("Failed to execute translation LIST_PRICE", e);
            }
            vertx.close(context.asyncAssertSuccess());
            async.complete();
        });
    });
}
Also used : HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) DataSourceResolver(org.folio.gobi.DataSourceResolver) Vertx(io.vertx.core.Vertx) GobiPurchaseOrderParserException(org.folio.gobi.exceptions.GobiPurchaseOrderParserException) HttpException(org.folio.gobi.exceptions.HttpException) CompletionException(java.util.concurrent.CompletionException) JAXBException(javax.xml.bind.JAXBException) Test(org.junit.Test)

Example 2 with DataSourceResolver

use of org.folio.gobi.DataSourceResolver in project mod-gobi by folio-org.

the class PostGobiOrdersHelperTest method testLookupOrderMappings.

@Test
public final void testLookupOrderMappings(TestContext context) {
    final Async async = context.async();
    final Vertx vertx = Vertx.vertx();
    final HttpServer server = vertx.createHttpServer();
    server.requestHandler(req -> {
        if (req.path().equals(PostGobiOrdersHelper.CONFIGURATION_ENDPOINT)) {
            req.response().setStatusCode(200).putHeader("content-type", "application/json").sendFile("ConfigData/success.json");
        } else {
            req.response().setStatusCode(500).end("Unexpected call: " + req.path());
        }
    });
    int port = NetworkUtils.nextFreePort();
    server.listen(port, "localhost", ar -> {
        context.assertTrue(ar.succeeded());
        Map<String, String> okapiHeaders = new HashMap<>();
        okapiHeaders.put("X-Okapi-Url", "http://localhost:" + port);
        okapiHeaders.put("x-okapi-tenant", "testLookupOrderMappings");
        PostGobiOrdersHelper pgoh = new PostGobiOrdersHelper(GOBIIntegrationServiceResourceImpl.getHttpClient(okapiHeaders), null, okapiHeaders, vertx.getOrCreateContext());
        pgoh.lookupOrderMappings(OrderMappings.OrderType.fromValue("ListedElectronicMonograph")).thenAccept(map -> {
            context.assertNotNull(map);
            context.assertNotNull(map.get(Mapping.Field.CURRENCY));
            DataSourceResolver ds = map.get(Mapping.Field.CURRENCY);
            context.assertEquals("//ListPrice/Currency", ds.from);
            context.assertEquals("USD", ds.defValue);
            context.assertNotNull(map.get(Mapping.Field.LIST_UNIT_PRICE_ELECTRONIC));
            ds = map.get(Mapping.Field.LIST_UNIT_PRICE_ELECTRONIC);
            context.assertEquals("//ListPrice/Amount", ds.from);
            context.assertEquals("0", ds.defValue);
            try {
                Double result = (Double) ds.translation.apply(ds.defValue.toString()).get();
                context.assertEquals(0.0, result);
            } catch (Exception e) {
                logger.error("Failed to execute translation LIST_PRICE", e);
            }
            context.assertNotNull((map.get(Mapping.Field.PO_LINE_ESTIMATED_PRICE)));
            ds = map.get(Mapping.Field.PO_LINE_ESTIMATED_PRICE);
            context.assertEquals("//NetPrice/Amount", ds.from);
            context.assertNotNull(ds.defValue);
            DataSourceResolver defVal = (DataSourceResolver) ds.defValue;
            context.assertEquals("//ListPrice/Amount//EstPrice", defVal.from);
            context.assertEquals("15.0", defVal.defValue);
            try {
                Double result = (Double) defVal.translation.apply(defVal.defValue.toString()).get();
                context.assertEquals(15.0, result);
            } catch (Exception e) {
                logger.error("Failed to execute translation for ESTIMATED_ PRICE with recursive default mapping", e);
            }
            vertx.close(context.asyncAssertSuccess());
            async.complete();
        });
    });
}
Also used : HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) DataSourceResolver(org.folio.gobi.DataSourceResolver) Vertx(io.vertx.core.Vertx) GobiPurchaseOrderParserException(org.folio.gobi.exceptions.GobiPurchaseOrderParserException) HttpException(org.folio.gobi.exceptions.HttpException) CompletionException(java.util.concurrent.CompletionException) JAXBException(javax.xml.bind.JAXBException) Test(org.junit.Test)

Aggregations

Vertx (io.vertx.core.Vertx)2 HttpServer (io.vertx.core.http.HttpServer)2 Async (io.vertx.ext.unit.Async)2 HashMap (java.util.HashMap)2 CompletionException (java.util.concurrent.CompletionException)2 JAXBException (javax.xml.bind.JAXBException)2 DataSourceResolver (org.folio.gobi.DataSourceResolver)2 GobiPurchaseOrderParserException (org.folio.gobi.exceptions.GobiPurchaseOrderParserException)2 HttpException (org.folio.gobi.exceptions.HttpException)2 Test (org.junit.Test)2