From 37f6dd33dea45baf9b8cc337c8a5fbf9bd2b51b9 Mon Sep 17 00:00:00 2001 From: desoliture Date: Sun, 6 Mar 2022 14:12:00 +0200 Subject: [PATCH] tests refactoring fix not removing differentTenant at test teardown, add new corresponding tests and refactor existing ones --- .../server/controller/AbstractWebTest.java | 12 +- .../controller/BaseAlarmControllerTest.java | 124 +++++++++++++++--- 2 files changed, 112 insertions(+), 24 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java b/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java index c6a45b4790..cc561e31a8 100644 --- a/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java @@ -261,6 +261,10 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest { protected void loginDifferentTenant() throws Exception { loginSysAdmin(); + if (savedDifferentTenant != null) { + deleteDifferentTenant(); + } + Tenant tenant = new Tenant(); tenant.setTitle("Different tenant"); savedDifferentTenant = doPost("/api/tenant", tenant, Tenant.class); @@ -289,9 +293,11 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest { } protected void deleteDifferentTenant() throws Exception { - loginSysAdmin(); - doDelete("/api/tenant/" + savedDifferentTenant.getId().getId().toString()) - .andExpect(status().isOk()); + if (savedDifferentTenant != null) { + loginSysAdmin(); + doDelete("/api/tenant/" + savedDifferentTenant.getId().getId().toString()) + .andExpect(status().isOk()); + } } protected User createUserAndLogin(User user, String password) throws Exception { diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java index 833f5a13e4..ce9b6916fc 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseAlarmControllerTest.java @@ -15,6 +15,7 @@ */ package org.thingsboard.server.controller; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -27,6 +28,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public abstract class BaseAlarmControllerTest extends AbstractControllerTest { + public static final String TEST_ALARM_TYPE = "Test"; + protected Device customerDevice; @Before @@ -44,17 +47,30 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { logout(); } + @After + public void teardown() throws Exception { + loginSysAdmin(); + deleteDifferentTenant(); + } + @Test - public void testCreateAlarm() throws Exception { + public void testCreateAlarmViaCustomer() throws Exception { loginCustomerUser(); - createAlarm(); + createAlarm(TEST_ALARM_TYPE); + logout(); + } + + @Test + public void testCreateAlarmViaTenant() throws Exception { + loginTenantAdmin(); + createAlarm(TEST_ALARM_TYPE); logout(); } @Test - public void testUpdateAlarm() throws Exception { + public void testUpdateAlarmViaCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); alarm.setSeverity(AlarmSeverity.MAJOR); Alarm updatedAlarm = doPost("/api/alarm", alarm, Alarm.class); Assert.assertNotNull(updatedAlarm); @@ -63,9 +79,30 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { } @Test - public void testUpdateAlarmViaAnotherCustomer() throws Exception { + public void testUpdateAlarmViaTenant() throws Exception { + loginTenantAdmin(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); + alarm.setSeverity(AlarmSeverity.MAJOR); + Alarm updatedAlarm = doPost("/api/alarm", alarm, Alarm.class); + Assert.assertNotNull(updatedAlarm); + Assert.assertEquals(AlarmSeverity.MAJOR, updatedAlarm.getSeverity()); + logout(); + } + + @Test + public void testUpdateAlarmViaDifferentTenant() throws Exception { + loginTenantAdmin(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); + alarm.setSeverity(AlarmSeverity.MAJOR); + loginDifferentTenant(); + doPost("/api/alarm", alarm).andExpect(status().isForbidden()); + logout(); + } + + @Test + public void testUpdateAlarmViaDifferentCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); loginDifferentCustomer(); alarm.setSeverity(AlarmSeverity.MAJOR); doPost("/api/alarm", alarm).andExpect(status().isForbidden()); @@ -73,27 +110,43 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { } @Test - public void testDeleteAlarm() throws Exception { + public void testDeleteAlarmViaCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); + doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isOk()); + logout(); + } + + @Test + public void testDeleteAlarmViaTenant() throws Exception { + loginTenantAdmin(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isOk()); logout(); } @Test - public void testDeleteAlarmVieAnotherCustomer() throws Exception { + public void testDeleteAlarmViaDifferentTenant() throws Exception { + loginTenantAdmin(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); + loginDifferentTenant(); + doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden()); + logout(); + } + + @Test + public void testDeleteAlarmViaAnotherCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); loginDifferentCustomer(); - alarm.setSeverity(AlarmSeverity.MAJOR); doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden()); logout(); } @Test - public void testClearAlarm() throws Exception { + public void testClearAlarmViaCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isOk()); Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class); Assert.assertNotNull(foundAlarm); @@ -102,9 +155,20 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { } @Test - public void testAcknowledgeAlarm() throws Exception { + public void testClearAlarmViaTenant() throws Exception { + loginTenantAdmin(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); + doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isOk()); + Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class); + Assert.assertNotNull(foundAlarm); + Assert.assertEquals(AlarmStatus.CLEARED_UNACK, foundAlarm.getStatus()); + logout(); + } + + @Test + public void testAcknowledgeAlarmViaCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isOk()); Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class); Assert.assertNotNull(foundAlarm); @@ -113,31 +177,49 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest { } @Test - public void testClearAlarmViaAnotherCustomer() throws Exception { + public void testClearAlarmViaDifferentCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); loginDifferentCustomer(); doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden()); logout(); } @Test - public void testAcknowledgeAlarmViaAnotherCustomer() throws Exception { + public void testClearAlarmViaDifferentTenant() throws Exception { + loginTenantAdmin(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); + loginDifferentTenant(); + doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden()); + logout(); + } + + @Test + public void testAcknowledgeAlarmViaDifferentCustomer() throws Exception { loginCustomerUser(); - Alarm alarm = createAlarm(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); loginDifferentCustomer(); doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isForbidden()); logout(); } - private Alarm createAlarm() throws Exception { + @Test + public void testAcknowledgeAlarmViaDifferentTenant() throws Exception { + loginTenantAdmin(); + Alarm alarm = createAlarm(TEST_ALARM_TYPE); + loginDifferentTenant(); + doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isForbidden()); + logout(); + } + + private Alarm createAlarm(String type) throws Exception { Alarm alarm = Alarm.builder() .tenantId(tenantId) .customerId(customerId) .originator(customerDevice.getId()) .status(AlarmStatus.ACTIVE_UNACK) .severity(AlarmSeverity.CRITICAL) - .type("Test") + .type(type) .build(); alarm = doPost("/api/alarm", alarm, Alarm.class);