use of org.finos.legend.engine.language.pure.dsl.service.execution.ServiceVariable in project legend-sdlc by finos.
the class TestServiceExecutionGenerator method testServiceExecutionUsingServiceRunnerAPI.
@Test
public void testServiceExecutionUsingServiceRunnerAPI() throws Exception {
ClassLoader classLoader = generateAndCompile("org.finos", getAllServices());
try (JavaHelper.ThreadContextClassLoaderScope ignored = JavaHelper.withCurrentThreadContextClassLoader(classLoader)) {
// No Param Model To Model Service
ServiceRunner noParamServiceRunner = findServiceRunnerByPath("service::ModelToModelService");
assertServiceVariables(noParamServiceRunner);
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> noParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Collections.singletonList("a"))));
Assert.assertEquals("Unexpected number of parameters. Expected parameter size: 0, Passed parameter size: 1", e.getMessage());
String expectedOutput = "{\"builder\":{\"_type\":\"json\"},\"values\":{\"defects\":[],\"source\":{\"defects\":[],\"source\":{\"number\":1,\"record\":\"{\\\"firstName\\\":\\\"firstName 73\\\",\\\"lastName\\\":\\\"lastName 79\\\",\\\"age\\\":27}\"}," + "\"value\":{\"age\":27,\"firstName\":\"firstName 73\",\"lastName\":\"lastName 79\"}},\"value\":{\"age\":27,\"fullName\":\"firstName 73 lastName 79\"}}}";
Assert.assertEquals(OBJECT_MAPPER.readTree(expectedOutput), OBJECT_MAPPER.readTree(noParamServiceRunner.run(ServiceRunnerInput.newInstance())));
// Multi Param Model To Model Service
ServiceRunner multiParamServiceRunner = findServiceRunnerByPath("service::ModelToModelServiceWithMultipleParams");
assertServiceVariables(multiParamServiceRunner, new ServiceVariable("i_s", String.class, new Multiplicity(1, 1)), new ServiceVariable("i_i", Long.class, new Multiplicity(1, 1)), new ServiceVariable("i_f", Double.class, new Multiplicity(1, 1)), new ServiceVariable("i_dec", BigDecimal.class, new Multiplicity(1, 1)), new ServiceVariable("i_b", Boolean.class, new Multiplicity(1, 1)), new ServiceVariable("i_sd", LocalDate.class, new Multiplicity(1, 1)), new ServiceVariable("i_dt", ZonedDateTime.class, new Multiplicity(1, 1)), new ServiceVariable("i_d", Temporal.class, new Multiplicity(1, 1)), new ServiceVariable("i_oi", Long.class, new Multiplicity(0, 1)), new ServiceVariable("i_li", Long.class, new Multiplicity(0, null)));
IllegalArgumentException e1 = Assert.assertThrows(IllegalArgumentException.class, () -> multiParamServiceRunner.run(ServiceRunnerInput.newInstance()));
Assert.assertEquals("Unexpected number of parameters. Expected parameter size: 10, Passed parameter size: 0", e1.getMessage());
IllegalArgumentException e2 = Assert.assertThrows(IllegalArgumentException.class, () -> multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Arrays.asList("1", 2, 3.0))));
Assert.assertEquals("Unexpected number of parameters. Expected parameter size: 10, Passed parameter size: 3", e2.getMessage());
List<Object> args1 = Arrays.asList(1, 2, 3.0, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), 5, Arrays.asList(6, 7));
IllegalArgumentException e3 = Assert.assertThrows(IllegalArgumentException.class, () -> multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(args1)));
Assert.assertEquals("Invalid provided parameter(s): [Unable to process 'String' parameter, value: 1.]", e3.getMessage());
List<Object> args2 = Arrays.asList("1", null, 3.0, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), 5, Arrays.asList(6, 7));
IllegalArgumentException e4 = Assert.assertThrows(IllegalArgumentException.class, () -> multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(args2)));
Assert.assertEquals("Missing external parameter(s): i_i:Integer[1]", e4.getMessage());
List<Object> arg3 = Arrays.asList("1", 2, 3.0, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), 5, Arrays.asList(1, "hello"));
IllegalArgumentException e5 = Assert.assertThrows(IllegalArgumentException.class, () -> multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(arg3).withSerializationFormat(SerializationFormat.PURE)));
Assert.assertEquals("Invalid provided parameter(s): [Unable to process 'Integer' parameter, value: 'hello' is not parsable.]", e5.getMessage());
JsonNode expected = OBJECT_MAPPER.readTree("[{\"age\":22,\"fullName\":\"Peter Smith\"},{\"age\":23,\"fullName\":\"John Johnson\"}]");
Assert.assertEquals(expected, OBJECT_MAPPER.readTree(multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Arrays.asList("1", 2, 3.0, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), 5, Arrays.asList(6, 7))).withSerializationFormat(SerializationFormat.PURE))));
Assert.assertEquals(expected, OBJECT_MAPPER.readTree(multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Arrays.asList("1", 2L, 3.0D, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), null, Arrays.asList(6, 7))).withSerializationFormat(SerializationFormat.PURE))));
Assert.assertEquals(expected, OBJECT_MAPPER.readTree(multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Arrays.asList("1", 2L, 3.0f, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), null, Arrays.asList(6, 7))).withSerializationFormat(SerializationFormat.PURE))));
int x = 2;
float y = 2.0f;
Assert.assertEquals(expected, OBJECT_MAPPER.readTree(multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Arrays.asList("1", x, y, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), null, Arrays.asList(6, 7))).withSerializationFormat(SerializationFormat.PURE))));
Assert.assertEquals(expected, OBJECT_MAPPER.readTree(multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Arrays.asList("1", x, x, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), null, 7)).withSerializationFormat(SerializationFormat.PURE))));
Assert.assertEquals(expected, OBJECT_MAPPER.readTree(multiParamServiceRunner.run(ServiceRunnerInput.newInstance().withArgs(Arrays.asList("1", x, x, new BigDecimal("4.0"), true, LocalDate.now(), ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC")), LocalDateTime.now(), null, null)).withSerializationFormat(SerializationFormat.PURE))));
}
}
use of org.finos.legend.engine.language.pure.dsl.service.execution.ServiceVariable in project legend-sdlc by finos.
the class TestServiceExecutionGenerator method assertServiceVariables.
private static void assertServiceVariables(ServiceRunner runner, ServiceVariable... serviceVariables) {
List<ServiceVariable> parameters = runner.getServiceVariables();
Assert.assertEquals(serviceVariables.length, parameters.size());
for (int i = 0; i < serviceVariables.length; i++) {
ServiceVariable expected = serviceVariables[i];
ServiceVariable actual = parameters.get(i);
Assert.assertEquals(expected.getName(), actual.getName());
Assert.assertEquals(expected.getType(), actual.getType());
Assert.assertEquals(expected.isOptional(), actual.isOptional());
Assert.assertEquals(expected.isToMany(), actual.isToMany());
}
}
Aggregations