PageRenderTime 3817ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/spi-modules/content-spi-solr-search-impl/src/test/java/com/smartitengineering/cms/spi/impl/events/EventPublisherTest.java

https://github.com/SmartITEngineering/smart-cms
Java | 218 lines | 179 code | 17 blank | 22 comment | 0 complexity | 5e643b77e22a1f1c52fc742d499f8d91 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. *
  3. * This is a simple Content Management System (CMS)
  4. * Copyright (C) 2012 Imran M Yousuf (imyousuf@smartitengineering.com)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.smartitengineering.cms.spi.impl.events;
  20. import com.google.inject.AbstractModule;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import com.smartitengineering.cms.api.content.Content;
  24. import com.smartitengineering.cms.api.content.ContentId;
  25. import com.smartitengineering.cms.api.event.Event;
  26. import com.smartitengineering.cms.api.event.EventListener;
  27. import com.smartitengineering.cms.api.type.ContentType;
  28. import com.smartitengineering.cms.api.type.ContentTypeId;
  29. import com.smartitengineering.cms.api.workspace.Sequence;
  30. import com.smartitengineering.cms.api.workspace.SequenceId;
  31. import com.smartitengineering.cms.api.workspace.WorkspaceId;
  32. import com.smartitengineering.events.async.api.EventPublisher;
  33. import org.apache.commons.codec.binary.Base64;
  34. import org.apache.commons.codec.binary.StringUtils;
  35. import org.jmock.Expectations;
  36. import org.jmock.Mockery;
  37. import org.junit.Before;
  38. import org.junit.Test;
  39. /**
  40. *
  41. * @author imyousuf
  42. */
  43. public class EventPublisherTest {
  44. public static final String CONTENT_ID = "someId";
  45. public static final String WORKSPACE_NAME = "globalWorkspaceName";
  46. public static final String WORSPACE_NS = "globalWorkspaceNamespace";
  47. public static final String CONTENT_TYPE_NAME = "contentTypeName";
  48. public static final String CONTENT_TYPE_NS = "contentTypeNamespace";
  49. public static final String SEQUENCE_NAME = "globalWorkspaceName";
  50. private final Mockery mockery = new Mockery();
  51. private final EventPublisher mockPublisher = mockery.mock(EventPublisher.class);
  52. private Injector injector;
  53. @Before
  54. public void setup() {
  55. injector = Guice.createInjector(new EventPublicationListenerModule());
  56. }
  57. @Test
  58. public void testPublicationOfContent() {
  59. EventListener listener = injector.getInstance(EventListener.class);
  60. final Event mockEvent = mockery.mock(Event.class);
  61. final Content mockContent = mockery.mock(Content.class);
  62. final ContentId mockContentId = mockery.mock(ContentId.class);
  63. final WorkspaceId mockWorkspaceId = mockery.mock(WorkspaceId.class);
  64. final String msgContent = getContentMsg();
  65. mockery.checking(new Expectations() {
  66. {
  67. exactly(1).of(mockEvent).getEventSourceType();
  68. will(returnValue(Event.Type.CONTENT));
  69. exactly(1).of(mockEvent).getEventType();
  70. will(returnValue(Event.EventType.CREATE));
  71. exactly(1).of(mockEvent).getSource();
  72. will(returnValue(mockContent));
  73. exactly(1).of(mockContent).getContentId();
  74. will(returnValue(mockContentId));
  75. exactly(2).of(mockContentId).getWorkspaceId();
  76. will(returnValue(mockWorkspaceId));
  77. exactly(1).of(mockContentId).getId();
  78. will(returnValue(StringUtils.getBytesUtf8(CONTENT_ID)));
  79. exactly(1).of(mockWorkspaceId).getGlobalNamespace();
  80. will(returnValue(WORSPACE_NS));
  81. exactly(1).of(mockWorkspaceId).getName();
  82. will(returnValue(WORKSPACE_NAME));
  83. exactly(1).of(mockPublisher).publishEvent(with("text/plain"), with(msgContent));
  84. will(returnValue(Boolean.TRUE));
  85. }
  86. });
  87. listener.notify(mockEvent);
  88. mockery.assertIsSatisfied();
  89. }
  90. @Test
  91. public void testPublicationOfContentType() {
  92. EventListener listener = injector.getInstance(EventListener.class);
  93. final Event mockEvent = mockery.mock(Event.class);
  94. final ContentType mockContentType = mockery.mock(ContentType.class);
  95. final ContentTypeId mockContentTypeId = mockery.mock(ContentTypeId.class);
  96. final WorkspaceId mockWorkspaceId = mockery.mock(WorkspaceId.class);
  97. final String msgContent = getContentTypeMsg();
  98. mockery.checking(new Expectations() {
  99. {
  100. exactly(1).of(mockEvent).getEventSourceType();
  101. will(returnValue(Event.Type.CONTENT_TYPE));
  102. exactly(1).of(mockEvent).getEventType();
  103. will(returnValue(Event.EventType.CREATE));
  104. exactly(1).of(mockEvent).getSource();
  105. will(returnValue(mockContentType));
  106. exactly(1).of(mockContentType).getContentTypeID();
  107. will(returnValue(mockContentTypeId));
  108. exactly(2).of(mockContentTypeId).getWorkspace();
  109. will(returnValue(mockWorkspaceId));
  110. exactly(1).of(mockContentTypeId).getNamespace();
  111. will(returnValue(CONTENT_TYPE_NS));
  112. exactly(1).of(mockContentTypeId).getName();
  113. will(returnValue(CONTENT_TYPE_NAME));
  114. exactly(1).of(mockWorkspaceId).getGlobalNamespace();
  115. will(returnValue(WORSPACE_NS));
  116. exactly(1).of(mockWorkspaceId).getName();
  117. will(returnValue(WORKSPACE_NAME));
  118. exactly(1).of(mockPublisher).publishEvent(with("text/plain"), with(msgContent));
  119. will(returnValue(Boolean.TRUE));
  120. }
  121. });
  122. listener.notify(mockEvent);
  123. mockery.assertIsSatisfied();
  124. }
  125. @Test
  126. public void testPublicationOfSequence() {
  127. EventListener listener = injector.getInstance(EventListener.class);
  128. final Event mockEvent = mockery.mock(Event.class);
  129. final Sequence mockSequence = mockery.mock(Sequence.class);
  130. final SequenceId mockSequenceId = mockery.mock(SequenceId.class);
  131. final WorkspaceId mockWorkspaceId = mockery.mock(WorkspaceId.class);
  132. final String msgContent = getSequenceMsg();
  133. mockery.checking(new Expectations() {
  134. {
  135. exactly(1).of(mockEvent).getEventSourceType();
  136. will(returnValue(Event.Type.SEQUENCE));
  137. exactly(1).of(mockEvent).getEventType();
  138. will(returnValue(Event.EventType.CREATE));
  139. exactly(1).of(mockEvent).getSource();
  140. will(returnValue(mockSequence));
  141. exactly(1).of(mockSequence).getSequenceId();
  142. will(returnValue(mockSequenceId));
  143. exactly(2).of(mockSequenceId).getWorkspaceId();
  144. will(returnValue(mockWorkspaceId));
  145. exactly(1).of(mockSequenceId).getName();
  146. will(returnValue(SEQUENCE_NAME));
  147. exactly(1).of(mockWorkspaceId).getGlobalNamespace();
  148. will(returnValue(WORSPACE_NS));
  149. exactly(1).of(mockWorkspaceId).getName();
  150. will(returnValue(WORKSPACE_NAME));
  151. exactly(1).of(mockPublisher).publishEvent(with("text/plain"), with(msgContent));
  152. will(returnValue(Boolean.TRUE));
  153. }
  154. });
  155. listener.notify(mockEvent);
  156. mockery.assertIsSatisfied();
  157. }
  158. @Test
  159. public void testPublicationOfUnknown() {
  160. EventListener listener = injector.getInstance(EventListener.class);
  161. final Event mockEvent = mockery.mock(Event.class);
  162. mockery.checking(new Expectations() {
  163. {
  164. exactly(1).of(mockEvent).getEventSourceType();
  165. will(returnValue(Event.Type.REPRESENTATION));
  166. }
  167. });
  168. listener.notify(mockEvent);
  169. mockery.assertIsSatisfied();
  170. }
  171. private final class EventPublicationListenerModule extends AbstractModule {
  172. @Override
  173. protected void configure() {
  174. bind(EventPublisher.class).toInstance(mockPublisher);
  175. bind(EventListener.class).to(EventPublicationListener.class);
  176. }
  177. }
  178. public static String getContentMsg() {
  179. final byte[] contentId = StringUtils.getBytesUtf8(new StringBuilder(WORSPACE_NS).append('\n').append(WORKSPACE_NAME).
  180. append('\n').append(CONTENT_ID).toString());
  181. final String msgContent = new StringBuilder("CONTENT\nCREATE\n").append(Base64.encodeBase64URLSafeString(contentId)).
  182. toString();
  183. return msgContent;
  184. }
  185. public static String getContentTypeMsg() {
  186. final byte[] contentId = StringUtils.getBytesUtf8(new StringBuilder(WORSPACE_NS).append('\n').append(WORKSPACE_NAME).
  187. append('\n').append(CONTENT_TYPE_NS).append('\n').append(CONTENT_TYPE_NAME).toString());
  188. final String msgContent = new StringBuilder("CONTENT_TYPE\nCREATE\n").append(Base64.encodeBase64URLSafeString(
  189. contentId)).
  190. toString();
  191. return msgContent;
  192. }
  193. public static String getSequenceMsg() {
  194. final byte[] contentId = StringUtils.getBytesUtf8(new StringBuilder(WORSPACE_NS).append('\n').append(WORKSPACE_NAME).
  195. append('\n').append(SEQUENCE_NAME).toString());
  196. final String msgContent =
  197. new StringBuilder("SEQUENCE\nCREATE\n").append(Base64.encodeBase64URLSafeString(contentId)).
  198. toString();
  199. return msgContent;
  200. }
  201. }