You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/01/24 15:12:50 UTC
[incubator-servicecomb-saga] 04/07: SCB-237 added alpha controller
to query events
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git
commit e5ffbb25c53d5d8b472ab0454edf2f5f5a9b2e6a
Author: seanyinx <se...@huawei.com>
AuthorDate: Tue Jan 23 18:39:30 2018 +0800
SCB-237 added alpha controller to query events
Signed-off-by: seanyinx <se...@huawei.com>
---
.../servicecomb/saga/alpha/core/TxEvent.java | 13 ++++
alpha/alpha-server/pom.xml | 4 ++
.../saga/alpha/server/AlphaEventController.java | 59 +++++++++++++++
.../alpha/server/AlphaEventControllerTest.java | 83 ++++++++++++++++++++++
4 files changed, 159 insertions(+)
diff --git a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java
index b654689..1364cb7 100644
--- a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java
+++ b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java
@@ -43,6 +43,19 @@ public class TxEvent {
private TxEvent() {
}
+ public TxEvent(TxEvent event) {
+ this(event.surrogateId,
+ event.serviceName,
+ event.instanceId,
+ event.creationTime,
+ event.globalTxId,
+ event.localTxId,
+ event.parentTxId,
+ event.type,
+ event.compensationMethod,
+ event.payloads);
+ }
+
public TxEvent(
String serviceName,
String instanceId,
diff --git a/alpha/alpha-server/pom.xml b/alpha/alpha-server/pom.xml
index 9efdf78..54f9cef 100644
--- a/alpha/alpha-server/pom.xml
+++ b/alpha/alpha-server/pom.xml
@@ -87,6 +87,10 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaEventController.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaEventController.java
new file mode 100644
index 0000000..0f6e139
--- /dev/null
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaEventController.java
@@ -0,0 +1,59 @@
+/*
+ * 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.servicecomb.saga.alpha.server;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.servicecomb.saga.alpha.core.TxEvent;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+
+@Controller
+@RequestMapping("/")
+class AlphaEventController {
+
+ private final TxEventEnvelopeRepository eventRepository;
+
+ AlphaEventController(TxEventEnvelopeRepository eventRepository) {
+ this.eventRepository = eventRepository;
+ }
+
+ @GetMapping(value = "/events")
+ ResponseEntity<Collection<TxEventVo>> events() {
+ Iterable<TxEvent> events = eventRepository.findAll();
+
+ List<TxEventVo> eventVos = new LinkedList<>();
+ events.forEach(event -> eventVos.add(new TxEventVo(event)));
+
+ return ResponseEntity.ok(eventVos);
+ }
+
+ @JsonAutoDetect(fieldVisibility = Visibility.ANY)
+ private static class TxEventVo extends TxEvent {
+ private TxEventVo(TxEvent event) {
+ super(event);
+ }
+ }
+}
diff --git a/alpha/alpha-server/src/test/java/org/apache/servicecomb/saga/alpha/server/AlphaEventControllerTest.java b/alpha/alpha-server/src/test/java/org/apache/servicecomb/saga/alpha/server/AlphaEventControllerTest.java
new file mode 100644
index 0000000..a479e2b
--- /dev/null
+++ b/alpha/alpha-server/src/test/java/org/apache/servicecomb/saga/alpha/server/AlphaEventControllerTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.servicecomb.saga.alpha.server;
+
+import static com.seanyinx.github.unit.scaffolding.Randomness.uniquify;
+import static java.util.Collections.singletonList;
+import static org.apache.servicecomb.saga.common.EventType.TxStartedEvent;
+import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
+import static org.hamcrest.core.Is.is;
+import static org.mockito.Mockito.when;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import java.util.Date;
+import java.util.UUID;
+
+import org.apache.servicecomb.saga.alpha.core.TxEvent;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@RunWith(SpringRunner.class)
+@WebMvcTest(AlphaEventController.class)
+public class AlphaEventControllerTest {
+ private final TxEvent someEvent = someEvent();
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @MockBean
+ private TxEventEnvelopeRepository eventRepository;
+
+ @Before
+ public void setUp() throws Exception {
+ when(eventRepository.findAll()).thenReturn(singletonList(someEvent));
+ }
+
+ @Test
+ public void retrievesEventsFromRepo() throws Exception {
+ mockMvc.perform(get("/events"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$", hasSize(1)))
+ .andExpect(jsonPath("$.[0].globalTxId", is(someEvent.globalTxId())))
+ .andExpect(jsonPath("$.[0].localTxId", is(someEvent.localTxId())));
+ }
+
+ private TxEvent someEvent() {
+ return new TxEvent(
+ uniquify("serviceName"),
+ uniquify("instanceId"),
+ new Date(),
+ uniquify("globalTxId"),
+ uniquify("localTxId"),
+ UUID.randomUUID().toString(),
+ TxStartedEvent.name(),
+ this.getClass().getCanonicalName(),
+ uniquify("blah").getBytes());
+ }
+}
--
To stop receiving notification emails like this one, please contact
ningjiang@apache.org.