WillemJiang commented on a change in pull request #4894:
URL: https://github.com/apache/camel/pull/4894#discussion_r558776067



##########
File path: 
components/camel-huaweicloud-smn/src/test/java/org/apache/camel/component/huaweicloud/smn/PublishTemplatedMessageTest.java
##########
@@ -0,0 +1,118 @@
+package org.apache.camel.component.huaweicloud.smn;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.verification.LoggedRequest;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.huaweicloud.smn.models.ServiceKeys;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.exceptions.base.MockitoException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+
+public class PublishTemplatedMessageTest extends CamelTestSupport {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(PublishTemplatedMessageTest.class.getName());
+    private static int wiremockServerPort = 8080;

Review comment:
       It may cause some trouble in CI if the test port is used. 
   There is a port lookup util method in Camel that you can take a look.
   

##########
File path: 
components/camel-huaweicloud-smn/src/test/java/org/apache/camel/component/huaweicloud/smn/PublishTemplatedMessageTest.java
##########
@@ -0,0 +1,118 @@
+package org.apache.camel.component.huaweicloud.smn;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.verification.LoggedRequest;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.huaweicloud.smn.models.ServiceKeys;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.exceptions.base.MockitoException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+
+public class PublishTemplatedMessageTest extends CamelTestSupport {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(PublishTemplatedMessageTest.class.getName());
+    private static int wiremockServerPort = 8080;
+
+    TestConfiguration testConfiguration = new TestConfiguration();
+
+    WireMockServer wireMockServer;
+
+    @BindToRegistry("serviceKeys")
+    ServiceKeys serviceKeys = new 
ServiceKeys(testConfiguration.getProperty("authKey"), 
testConfiguration.getProperty("secretKey"));
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+
+        // populating tag values. user has to adjust the map entries according 
to the structure of their respective templates
+        Map<String, String> tags = new HashMap<>();
+        tags.put("name", "reji");
+        tags.put("phone", "1234567890");
+
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:publish_templated_message")
+                        .setProperty("CamelHwCloudSmnSubject", constant("This 
is my subjectline"))
+                        .setProperty("CamelHwCloudSmnTopic", 
constant(testConfiguration.getProperty("topic")))
+                        .setProperty("CamelHwCloudSmnMessageTtl", constant(60))
+                        .setProperty("CamelHwCloudSmnTemplateTags", 
constant(tags))
+                        .setProperty("CamelHwCloudSmnTemplateName", 
constant("hello-template"))
+                        
//.to("hwcloud-smn:publishMessageService?serviceKeys=#serviceKeys&operation=publishAsTemplatedMessage"+"&projectId="+testConfiguration.getProperty("projectId")+"&region="+testConfiguration.getProperty("region")+"&proxyHost=localhost&proxyPort=3128&ignoreSslVerification=true")
+                        
.to("hwcloud-smn:publishMessageService?serviceKeys=#serviceKeys&operation=publishAsTemplatedMessage"+"&projectId="+testConfiguration.getProperty("projectId")+"&region="+testConfiguration.getProperty("region")+"&ignoreSslVerification=true")
+                        .log("templated notification sent")
+                        .to("mock:publish_templated_message_result");
+            }
+        };
+    }
+
+    private void setupSimpleNotificationsUtilsMock() {
+        try {
+            Mockito.mockStatic(SimpleNotificationUtils.class);
+            
Mockito.when(SimpleNotificationUtils.resolveSmnServiceEndpoint("unit-test")).thenReturn("http://localhost:";
 + wiremockServerPort);
+        }catch (MockitoException e) {
+            LOGGER.info("Mock already registered. Using existing 
registration");

Review comment:
       I doubt about this Information.

##########
File path: 
components/camel-huaweicloud-smn/src/test/java/org/apache/camel/component/huaweicloud/smn/PublishTemplatedMessageTest.java
##########
@@ -0,0 +1,118 @@
+package org.apache.camel.component.huaweicloud.smn;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.verification.LoggedRequest;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.huaweicloud.smn.models.ServiceKeys;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.exceptions.base.MockitoException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+
+public class PublishTemplatedMessageTest extends CamelTestSupport {

Review comment:
       we can consider to reuse the WireMock setup code across 
PublishTemplatedMessageTest and PublishTextMessageTest.

##########
File path: 
components/camel-huaweicloud-smn/src/test/java/org/apache/camel/component/huaweicloud/smn/PublishTextMessageTest.java
##########
@@ -0,0 +1,108 @@
+package org.apache.camel.component.huaweicloud.smn;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.verification.LoggedRequest;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.exceptions.base.MockitoException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+
+public class PublishTextMessageTest extends CamelTestSupport {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(PublishTextMessageTest.class.getName());
+    private static int wiremockServerPort = 8080;
+
+    TestConfiguration testConfiguration = new TestConfiguration();
+
+    WireMockServer wireMockServer;
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:publish_text_message")
+                        .setProperty("CamelHwCloudSmnSubject", constant("Dummy 
Subject Line"))
+                        .setProperty("CamelHwCloudSmnTopic", 
constant(testConfiguration.getProperty("topic")))
+                        .setProperty("CamelHwCloudSmnMessageTtl", constant(60))
+                        
//.to("hwcloud-smn:publishMessageService?operation=publishAsTextMessage&authKey="+testConfiguration.getProperty("authKey")+"&secretKey="+testConfiguration.getProperty("secretKey")+"&projectId="+testConfiguration.getProperty("projectId")+"&region="+testConfiguration.getProperty("region")+"&proxyHost=localhost&proxyPort=3128&ignoreSslVerification=true")
+                        
.to("hwcloud-smn:publishMessageService?operation=publishAsTextMessage&authKey="+testConfiguration.getProperty("authKey")+"&secretKey="+testConfiguration.getProperty("secretKey")+"&projectId="+testConfiguration.getProperty("projectId")+"&region="+testConfiguration.getProperty("region")+"&ignoreSslVerification=true")
+                        .log("publish message successful")
+                        .to("log:LOG?showAll=true")
+                        .to("mock:publish_text_message_result");
+            }
+        };
+    }
+
+    @Test
+    public void testTextNotificationSend() throws Exception {
+
+        boolean isMockedServerTest = 
testConfiguration.getProperty("region").equals("unit-test");
+        if(isMockedServerTest) {
+            LOGGER.info("region is unit-test. Starting up wiremock stubs");
+            initWireMock();
+            setupSimpleNotificationsUtilsMock();
+        }
+
+        MockEndpoint mock = 
getMockEndpoint("mock:publish_text_message_result");
+        mock.expectedMinimumMessageCount(1);
+        template.sendBody("direct:publish_text_message", "message content");
+        Exchange responseExchange = mock.getExchanges().get(0);
+
+        mock.assertIsSatisfied();
+
+        
Assert.assertNotNull(responseExchange.getProperty("CamelSmnMesssageId"));
+        
Assert.assertNotNull(responseExchange.getProperty("CamelSmnRequestId"));
+        
Assert.assertTrue(responseExchange.getProperty("CamelSmnMesssageId").toString().length()
 > 0);
+        
Assert.assertTrue(responseExchange.getProperty("CamelSmnRequestId").toString().length()
 > 0);
+
+
+        if(isMockedServerTest) {
+            Assert.assertEquals("bf94b63a5dfb475994d3ac34664e24f2", 
responseExchange.getProperty("CamelSmnMesssageId"));
+            Assert.assertEquals("6a63a18b8bab40ffb71ebd9cb80d0085", 
responseExchange.getProperty("CamelSmnRequestId"));
+
+            LoggedRequest loggedRequest = 
TestUtils.retrieveTextNotificationRequest(getAllServeEvents());;
+            LOGGER.info("Verifying wiremock request");
+            
Assert.assertEquals("http://localhost:8080/v2/9071a38e7f6a4ba7b7bcbeb7d4ea6efc/notifications/topics/urn:smn:unit-test:9071a38e7f6a4ba7b7bcbeb7d4ea6efc:reji-test/publish";,
 loggedRequest.getAbsoluteUrl());
+            
Assert.assertEquals("eyJzdWJqZWN0IjoiRHVtbXkgU3ViamVjdCBMaW5lIiwibWVzc2FnZSI6Im1lc3NhZ2UgY29udGVudCIsInRpbWVfdG9fbGl2ZSI6IjYwIn0=",
 loggedRequest.getBodyAsBase64());
+            Assert.assertEquals("{\"subject\":\"Dummy Subject 
Line\",\"message\":\"message content\",\"time_to_live\":\"60\"}", 
loggedRequest.getBodyAsString());
+        }
+    }
+
+
+    private void setupSimpleNotificationsUtilsMock() {
+        Mockito.reset();
+        try {
+            Mockito.mockStatic(SimpleNotificationUtils.class);
+            
Mockito.when(SimpleNotificationUtils.resolveSmnServiceEndpoint("unit-test")).thenReturn("http://localhost:";
 + wiremockServerPort);

Review comment:
       It's not a good practice to mock the static utils method. 

##########
File path: 
components/camel-huaweicloud-smn/src/test/java/org/apache/camel/component/huaweicloud/smn/PublishTemplatedMessageTest.java
##########
@@ -0,0 +1,118 @@
+package org.apache.camel.component.huaweicloud.smn;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.verification.LoggedRequest;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.huaweicloud.smn.models.ServiceKeys;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.exceptions.base.MockitoException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+
+public class PublishTemplatedMessageTest extends CamelTestSupport {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(PublishTemplatedMessageTest.class.getName());
+    private static int wiremockServerPort = 8080;
+
+    TestConfiguration testConfiguration = new TestConfiguration();
+
+    WireMockServer wireMockServer;
+
+    @BindToRegistry("serviceKeys")
+    ServiceKeys serviceKeys = new 
ServiceKeys(testConfiguration.getProperty("authKey"), 
testConfiguration.getProperty("secretKey"));
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+
+        // populating tag values. user has to adjust the map entries according 
to the structure of their respective templates
+        Map<String, String> tags = new HashMap<>();
+        tags.put("name", "reji");
+        tags.put("phone", "1234567890");
+
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:publish_templated_message")
+                        .setProperty("CamelHwCloudSmnSubject", constant("This 
is my subjectline"))
+                        .setProperty("CamelHwCloudSmnTopic", 
constant(testConfiguration.getProperty("topic")))
+                        .setProperty("CamelHwCloudSmnMessageTtl", constant(60))
+                        .setProperty("CamelHwCloudSmnTemplateTags", 
constant(tags))
+                        .setProperty("CamelHwCloudSmnTemplateName", 
constant("hello-template"))
+                        
//.to("hwcloud-smn:publishMessageService?serviceKeys=#serviceKeys&operation=publishAsTemplatedMessage"+"&projectId="+testConfiguration.getProperty("projectId")+"&region="+testConfiguration.getProperty("region")+"&proxyHost=localhost&proxyPort=3128&ignoreSslVerification=true")
+                        
.to("hwcloud-smn:publishMessageService?serviceKeys=#serviceKeys&operation=publishAsTemplatedMessage"+"&projectId="+testConfiguration.getProperty("projectId")+"&region="+testConfiguration.getProperty("region")+"&ignoreSslVerification=true")
+                        .log("templated notification sent")
+                        .to("mock:publish_templated_message_result");
+            }
+        };
+    }
+
+    private void setupSimpleNotificationsUtilsMock() {
+        try {
+            Mockito.mockStatic(SimpleNotificationUtils.class);
+            
Mockito.when(SimpleNotificationUtils.resolveSmnServiceEndpoint("unit-test")).thenReturn("http://localhost:";
 + wiremockServerPort);
+        }catch (MockitoException e) {
+            LOGGER.info("Mock already registered. Using existing 
registration");
+        }
+    }
+
+    @Test
+    public void testTemplatedNotificationSend() throws Exception {
+        boolean isMockedServerTest = 
testConfiguration.getProperty("region").equals("unit-test");
+        if(isMockedServerTest) {
+            LOGGER.info("region is unit-test. Starting up wiremock stubs");
+            initWireMock();
+            setupSimpleNotificationsUtilsMock();
+        }
+
+        MockEndpoint mock = 
getMockEndpoint("mock:publish_templated_message_result");
+        mock.expectedMinimumMessageCount(1);
+        template.sendBody("direct:publish_templated_message",null);
+        Exchange responseExchange = mock.getExchanges().get(0);
+
+        mock.assertIsSatisfied();
+
+        
Assert.assertNotNull(responseExchange.getProperty("CamelSmnMesssageId"));
+        
Assert.assertNotNull(responseExchange.getProperty("CamelSmnRequestId"));
+        
Assert.assertTrue(responseExchange.getProperty("CamelSmnMesssageId").toString().length()
 > 0);
+        
Assert.assertTrue(responseExchange.getProperty("CamelSmnRequestId").toString().length()
 > 0);
+
+        if(isMockedServerTest) {
+            Assert.assertEquals("bf94b63a5dfb475994d3ac34664e24f2", 
responseExchange.getProperty("CamelSmnMesssageId"));
+            Assert.assertEquals("6a63a18b8bab40ffb71ebd9cb80d0085", 
responseExchange.getProperty("CamelSmnRequestId"));
+
+            LoggedRequest loggedRequest = 
TestUtils.retrieveTemplatedNotificationRequest(getAllServeEvents());
+            LOGGER.info("Verifying wiremock request");
+            
Assert.assertEquals("http://localhost:8080/v2/9071a38e7f6a4ba7b7bcbeb7d4ea6efc/notifications/topics/urn:smn:unit-test:9071a38e7f6a4ba7b7bcbeb7d4ea6efc:reji-test/publish";,
 loggedRequest.getAbsoluteUrl());
+            
Assert.assertEquals("eyJzdWJqZWN0IjoiVGhpcyBpcyBteSBzdWJqZWN0bGluZSIsIm1lc3NhZ2VfdGVtcGxhdGVfbmFtZSI6ImhlbGxvLXRlbXBsYXRlIiwidGFncyI6eyJwaG9uZSI6IjEyMzQ1Njc4OTAiLCJuYW1lIjoicmVqaSJ9LCJ0aW1lX3RvX2xpdmUiOiI2MCJ9",
 loggedRequest.getBodyAsBase64());
+            Assert.assertEquals("{\"subject\":\"This is my 
subjectline\",\"message_template_name\":\"hello-template\",\"tags\":{\"phone\":\"1234567890\",\"name\":\"reji\"},\"time_to_live\":\"60\"}",
 loggedRequest.getBodyAsString());
+        }
+    }
+
+    private void initWireMock() throws Exception {
+        try {
+            wireMockServer = new WireMockServer(wiremockServerPort);
+            wireMockServer.start();
+        }catch (Exception e) {
+            LOGGER.info("wiremock server already registered in test context. 
using the same");

Review comment:
       We should throw the exception beside print a log.

##########
File path: 
components/camel-huaweicloud-smn/src/main/java/org/apache/camel/component/huaweicloud/smn/SimpleNotificationUtils.java
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.huaweicloud.smn;
+
+import com.huaweicloud.sdk.core.region.Region;
+import com.huaweicloud.sdk.smn.v2.region.SmnRegion;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * utility functions for the component
+ */
+public class SimpleNotificationUtils {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SimpleNotificationUtils.class.getName());
+
+    /**
+     * resolves endpoint url for the given region
+     * @param region
+     * @return
+     */
+    public static String resolveSmnServiceEndpoint(String region) {

Review comment:
       As we didn't test this method due to mocking the static method,  could 
you explain why do you create this method?
   

##########
File path: 
components/camel-huaweicloud-smn/src/test/java/org/apache/camel/component/huaweicloud/smn/PublishTemplatedMessageTest.java
##########
@@ -0,0 +1,118 @@
+package org.apache.camel.component.huaweicloud.smn;

Review comment:
       Please add the License header on all the test java files.

##########
File path: 
components/camel-huaweicloud-smn/src/main/java/org/apache/camel/component/huaweicloud/smn/SimpleNotificationUtils.java
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.huaweicloud.smn;
+
+import com.huaweicloud.sdk.core.region.Region;
+import com.huaweicloud.sdk.smn.v2.region.SmnRegion;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * utility functions for the component
+ */
+public class SimpleNotificationUtils {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SimpleNotificationUtils.class.getName());
+
+    /**
+     * resolves endpoint url for the given region
+     * @param region
+     * @return
+     */
+    public static String resolveSmnServiceEndpoint(String region) {
+        if(region == null) {
+            return null;
+        }
+
+        String result = null;
+
+        try {
+            String formattedEndpointKey = formatEndpointKey(region);
+            result = ((Region) SmnRegion.class.getField(formattedEndpointKey)

Review comment:
       I'm not sure if we can still use reflection in Camel 3.0 to support 
camel-quarkus.
   Please double check it with the community.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to