Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 140AF200BD4 for ; Wed, 16 Nov 2016 15:13:09 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 12EF9160B13; Wed, 16 Nov 2016 14:13:09 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 1A0FD160B03 for ; Wed, 16 Nov 2016 15:13:07 +0100 (CET) Received: (qmail 80974 invoked by uid 500); 16 Nov 2016 14:13:07 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 80947 invoked by uid 99); 16 Nov 2016 14:13:07 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 16 Nov 2016 14:13:07 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E1800E00C7; Wed, 16 Nov 2016 14:13:06 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lburgazzoli@apache.org To: commits@camel.apache.org Date: Wed, 16 Nov 2016 14:13:06 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/3] camel git commit: CAMEL-10482: ServiceNow : add an option to set inbound and outbound models archived-at: Wed, 16 Nov 2016 14:13:09 -0000 Repository: camel Updated Branches: refs/heads/camel-2.18.x 57dfcaf29 -> 7aecfac39 http://git-wip-us.apache.org/repos/asf/camel/blob/6fa418de/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/releases/helsinki/HelsinkiServiceNowTableProcessor.java ---------------------------------------------------------------------- diff --git a/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/releases/helsinki/HelsinkiServiceNowTableProcessor.java b/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/releases/helsinki/HelsinkiServiceNowTableProcessor.java index ee912ae..5390121 100644 --- a/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/releases/helsinki/HelsinkiServiceNowTableProcessor.java +++ b/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/releases/helsinki/HelsinkiServiceNowTableProcessor.java @@ -52,7 +52,7 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { private void retrieveRecord(Exchange exchange) throws Exception { final Message in = exchange.getIn(); final String tableName = in.getHeader(ServiceNowParams.PARAM_TABLE_NAME.getHeader(), config.getTable(), String.class); - final Class model = getModel(in, tableName); + final Class responseModel = getResponseModel(in, tableName); final String sysId = in.getHeader(ServiceNowParams.PARAM_SYS_ID.getHeader(), String.class); Response response = ObjectHelper.isEmpty(sysId) @@ -82,7 +82,7 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { .query(ServiceNowParams.SYSPARM_VIEW, in) .invoke(HttpMethod.GET); - setBodyAndHeaders(exchange.getIn(), model, response); + setBodyAndHeaders(exchange.getIn(), responseModel, response); } /* @@ -92,10 +92,11 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { private void createRecord(Exchange exchange) throws Exception { final Message in = exchange.getIn(); final String tableName = in.getHeader(ServiceNowParams.PARAM_TABLE_NAME.getHeader(), config.getTable(), String.class); - final Class model = getModel(in, tableName); + final Class requestModel = getRequestModel(in, tableName); + final Class responseModel = getResponseModel(in, tableName); final String sysId = in.getHeader(ServiceNowParams.PARAM_SYS_ID.getHeader(), String.class); - validateBody(in, model); + validateBody(in, requestModel); Response response = client.reset() .types(MediaType.APPLICATION_JSON_TYPE) .path("now") @@ -109,7 +110,7 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { .query(ServiceNowParams.SYSPARM_VIEW, in) .invoke(HttpMethod.POST, in.getMandatoryBody()); - setBodyAndHeaders(exchange.getIn(), model, response); + setBodyAndHeaders(exchange.getIn(), responseModel, response); } /* @@ -119,10 +120,11 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { private void modifyRecord(Exchange exchange) throws Exception { final Message in = exchange.getIn(); final String tableName = in.getHeader(ServiceNowParams.PARAM_TABLE_NAME.getHeader(), config.getTable(), String.class); - final Class model = getModel(in, tableName); + final Class requestModel = getRequestModel(in, tableName); + final Class responseModel = getResponseModel(in, tableName); final String sysId = in.getHeader(ServiceNowParams.PARAM_SYS_ID.getHeader(), String.class); - validateBody(in, model); + validateBody(in, requestModel); Response response = client.reset() .types(MediaType.APPLICATION_JSON_TYPE) .path("now") @@ -137,7 +139,7 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { .query(ServiceNowParams.SYSPARM_VIEW, in) .invoke(HttpMethod.PUT, in.getMandatoryBody()); - setBodyAndHeaders(exchange.getIn(), model, response); + setBodyAndHeaders(exchange.getIn(), responseModel, response); } /* @@ -147,7 +149,7 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { private void deleteRecord(Exchange exchange) throws Exception { final Message in = exchange.getIn(); final String tableName = in.getHeader(ServiceNowParams.PARAM_TABLE_NAME.getHeader(), config.getTable(), String.class); - final Class model = getModel(in, tableName); + final Class responseModel = getResponseModel(in, tableName); final String sysId = in.getHeader(ServiceNowParams.PARAM_SYS_ID.getHeader(), String.class); Response response = client.reset() @@ -158,7 +160,7 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { .path(ObjectHelper.notNull(sysId, "sysId")) .invoke(HttpMethod.DELETE, null); - setBodyAndHeaders(exchange.getIn(), model, response); + setBodyAndHeaders(exchange.getIn(), responseModel, response); } /* @@ -168,10 +170,11 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { private void updateRecord(Exchange exchange) throws Exception { final Message in = exchange.getIn(); final String tableName = in.getHeader(ServiceNowParams.PARAM_TABLE_NAME.getHeader(), config.getTable(), String.class); - final Class model = getModel(in, tableName); + final Class requestModel = getRequestModel(in, tableName); + final Class responseModel = getResponseModel(in, tableName); final String sysId = in.getHeader(ServiceNowParams.PARAM_SYS_ID.getHeader(), String.class); - validateBody(in, model); + validateBody(in, requestModel); Response response = client.reset() .types(MediaType.APPLICATION_JSON_TYPE) .path("now") @@ -186,6 +189,6 @@ class HelsinkiServiceNowTableProcessor extends AbstractServiceNowProcessor { .query(ServiceNowParams.SYSPARM_VIEW, in) .invoke("PATCH", in.getMandatoryBody()); - setBodyAndHeaders(exchange.getIn(), model, response); + setBodyAndHeaders(exchange.getIn(), responseModel, response); } } http://git-wip-us.apache.org/repos/asf/camel/blob/6fa418de/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowImportSetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowImportSetTest.java b/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowImportSetTest.java new file mode 100644 index 0000000..098965c --- /dev/null +++ b/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowImportSetTest.java @@ -0,0 +1,137 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.servicenow; + +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.camel.Message; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Ignore; +import org.junit.Test; + +/** + * To set-up ServiceNow for this tests: + * + * 1. Create a new table with + * - id has to be set to u_imp_incidents (name does not matter) + * - inherit from sys_import_set_row + * + * 2. Add a new field to u_imp_incidents + * - name short_description + * - id should be automatically set to u_short_description + * + * 3. Create a new Transform Map + * - source table u_imp_incidents + * - target table incidents + * - Perform auto mapping, if it does not work map each field one by one + */ +@Ignore +public class ServiceNowImportSetTest extends ServiceNowTestSupport { + + @Test + public void testIncidentImport() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:servicenow"); + + mock.reset(); + mock.expectedMessageCount(1); + + IncidentImportRequest incident = new IncidentImportRequest(); + incident.shortDescription = "test"; + + template().sendBodyAndHeaders( + "direct:servicenow", + incident, + new KVBuilder() + .put(ServiceNowConstants.RESOURCE, ServiceNowConstants.RESOURCE_IMPORT) + .put(ServiceNowConstants.ACTION, ServiceNowConstants.ACTION_CREATE) + .put(ServiceNowConstants.REQUEST_MODEL, IncidentImportRequest.class) + .put(ServiceNowConstants.RESPONSE_MODEL, IncidentImportResponse.class) + .put(ServiceNowParams.PARAM_TABLE_NAME, "u_imp_incident") + .build() + ); + + mock.assertIsSatisfied(); + + Message in = mock.getExchanges().get(0).getIn(); + + // Meta data + Map meta = in.getHeader(ServiceNowConstants.RESPONSE_META, Map.class); + assertNotNull(meta); + assertEquals("u_imp_incident", meta.get("staging_table")); + + // Incidents + List responses = in.getBody(List.class); + assertNotNull(responses); + assertEquals(1, responses.size()); + assertEquals("inserted", responses.get(0).status); + assertEquals("imp_incidents", responses.get(0).transformMap); + assertEquals("incident", responses.get(0).table); + } + + // ************************************************************************* + // + // ************************************************************************* + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("direct:servicenow") + .to("servicenow:{{env:SERVICENOW_INSTANCE}}" + + "?userName={{env:SERVICENOW_USERNAME}}" + + "&password={{env:SERVICENOW_PASSWORD}}") + .to("log:org.apache.camel.component.servicenow?level=INFO&showAll=true") + .to("mock:servicenow"); + } + }; + } + + // ************************************************************************* + // + // ************************************************************************* + + @JsonIgnoreProperties(ignoreUnknown = true) + @JsonInclude(JsonInclude.Include.NON_NULL) + private static final class IncidentImportRequest { + @JsonProperty("u_short_description") + public String shortDescription; + } + + @JsonIgnoreProperties(ignoreUnknown = true) + @JsonInclude(JsonInclude.Include.NON_NULL) + private static final class IncidentImportResponse { + @JsonProperty("transform_map") + public String transformMap; + @JsonProperty("table") + public String table; + @JsonProperty("display_name") + public String displayName; + @JsonProperty("display_value") + public String displayValue; + @JsonProperty("record_link") + public String recordLink; + @JsonProperty("status") + public String status; + @JsonProperty("sys_id") + public String sysId; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/6fa418de/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java b/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java index 1f831c2..cf98c8e 100644 --- a/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java +++ b/components/camel-servicenow/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java @@ -14,13 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.camel.component.servicenow; import java.util.UUID; +import com.fasterxml.jackson.databind.JsonNode; import org.apache.camel.CamelExecutionException; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.servicenow.model.Incident; import org.junit.Test; public class ServiceNowTest extends ServiceNowTestSupport { @@ -89,6 +91,38 @@ public class ServiceNowTest extends ServiceNowTestSupport { } } + @Test + public void testRequestResponseModels() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:servicenow"); + + mock.reset(); + mock.expectedMessageCount(1); + + Incident incident = new Incident(); + incident.setDescription("my incident"); + incident.setShortDescription("An incident"); + incident.setSeverity(1); + incident.setImpact(1); + + template().sendBodyAndHeaders( + "direct:servicenow", + incident, + new KVBuilder() + .put(ServiceNowConstants.RESOURCE, ServiceNowConstants.RESOURCE_TABLE) + .put(ServiceNowConstants.ACTION, ServiceNowConstants.ACTION_CREATE) + .put(ServiceNowConstants.REQUEST_MODEL, Incident.class) + .put(ServiceNowConstants.RESPONSE_MODEL, JsonNode.class) + .put(ServiceNowParams.PARAM_TABLE_NAME, "incident") + .build() + ); + + mock.assertIsSatisfied(); + + Object body = mock.getExchanges().get(0).getIn().getBody(); + assertNotNull(body); + assertTrue(body instanceof JsonNode); + } + // ************************************************************************* // // ************************************************************************* @@ -100,10 +134,7 @@ public class ServiceNowTest extends ServiceNowTestSupport { from("direct:servicenow") .to("servicenow:{{env:SERVICENOW_INSTANCE}}" + "?userName={{env:SERVICENOW_USERNAME}}" - + "&password={{env:SERVICENOW_PASSWORD}}" - //+ "&oauthClientId={{env:SERVICENOW_OAUTH2_CLIENT_ID}}" - //+ "&oauthClientSecret={{env:SERVICENOW_OAUTH2_CLIENT_SECRET}}" - + "&model.incident=org.apache.camel.component.servicenow.model.Incident") + + "&password={{env:SERVICENOW_PASSWORD}}") .to("log:org.apache.camel.component.servicenow?level=INFO&showAll=true") .to("mock:servicenow"); }