You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppStateImplTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.application;
  21. import org.junit.Test;
  22. import org.sonar.process.ProcessId;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.verify;
  26. import static org.mockito.Mockito.verifyNoMoreInteractions;
  27. public class AppStateImplTest {
  28. private AppStateListener listener = mock(AppStateListener.class);
  29. private AppStateImpl underTest = new AppStateImpl();
  30. @Test
  31. public void get_and_set_operational_flag() {
  32. assertThat(underTest.isOperational(ProcessId.COMPUTE_ENGINE, true)).isFalse();
  33. assertThat(underTest.isOperational(ProcessId.ELASTICSEARCH, true)).isFalse();
  34. assertThat(underTest.isOperational(ProcessId.WEB_SERVER, true)).isFalse();
  35. underTest.setOperational(ProcessId.ELASTICSEARCH);
  36. assertThat(underTest.isOperational(ProcessId.COMPUTE_ENGINE, true)).isFalse();
  37. assertThat(underTest.isOperational(ProcessId.ELASTICSEARCH, true)).isTrue();
  38. assertThat(underTest.isOperational(ProcessId.WEB_SERVER, true)).isFalse();
  39. // only local mode is supported. App state = local state
  40. assertThat(underTest.isOperational(ProcessId.COMPUTE_ENGINE, false)).isFalse();
  41. assertThat(underTest.isOperational(ProcessId.ELASTICSEARCH, false)).isTrue();
  42. assertThat(underTest.isOperational(ProcessId.WEB_SERVER, false)).isFalse();
  43. }
  44. @Test
  45. public void notify_listeners_when_a_process_becomes_operational() {
  46. underTest.addListener(listener);
  47. underTest.setOperational(ProcessId.ELASTICSEARCH);
  48. verify(listener).onAppStateOperational(ProcessId.ELASTICSEARCH);
  49. verifyNoMoreInteractions(listener);
  50. }
  51. @Test
  52. public void tryToLockWebLeader_returns_true_if_first_call() {
  53. assertThat(underTest.tryToLockWebLeader()).isTrue();
  54. // next calls return false
  55. assertThat(underTest.tryToLockWebLeader()).isFalse();
  56. assertThat(underTest.tryToLockWebLeader()).isFalse();
  57. }
  58. @Test
  59. public void reset_initializes_all_flags() {
  60. underTest.setOperational(ProcessId.ELASTICSEARCH);
  61. assertThat(underTest.tryToLockWebLeader()).isTrue();
  62. underTest.reset();
  63. assertThat(underTest.isOperational(ProcessId.ELASTICSEARCH, true)).isFalse();
  64. assertThat(underTest.isOperational(ProcessId.COMPUTE_ENGINE, true)).isFalse();
  65. assertThat(underTest.isOperational(ProcessId.WEB_SERVER, true)).isFalse();
  66. assertThat(underTest.tryToLockWebLeader()).isTrue();
  67. }
  68. }