/**
 * Copyright © 2016-2022 The Thingsboard Authors
 *
 * Licensed 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.thingsboard.server.service.apiusage;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.thingsboard.rule.engine.api.MailService;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.data.ApiUsageState;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.msg.queue.ServiceType;
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
import org.thingsboard.server.dao.tenant.TenantService;
import org.thingsboard.server.dao.timeseries.TimeseriesService;
import org.thingsboard.server.dao.usagerecord.ApiUsageStateService;
import org.thingsboard.server.queue.discovery.PartitionService;
import org.thingsboard.server.queue.scheduler.SchedulerComponent;
import org.thingsboard.server.service.executors.DbCallbackExecutorService;

import java.util.UUID;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;

@RunWith(MockitoJUnitRunner.class)
public class DefaultTbApiUsageStateServiceTest {

    @Mock
    TenantService tenantService;
    @Mock
    TimeseriesService tsService;
    @Mock
    TbClusterService clusterService;
    @Mock
    PartitionService partitionService;
    @Mock
    TenantApiUsageState tenantUsageStateMock;
    @Mock
    ApiUsageStateService apiUsageStateService;
    @Mock
    TbTenantProfileCache tenantProfileCache;
    @Mock
    MailService mailService;
    @Mock
    DbCallbackExecutorService dbExecutor;

    TenantId tenantId = TenantId.fromUUID(UUID.fromString("00797a3b-7aeb-4b5b-b57a-c2a810d0f112"));

    DefaultTbApiUsageStateService service;

    @Before
    public void setUp() {
        service = spy(new DefaultTbApiUsageStateService(clusterService, partitionService, tenantService, tsService, apiUsageStateService, tenantProfileCache, mailService, dbExecutor));
    }

    @Test
    public void givenTenantIdFromEntityStatesMap_whenGetApiUsageState() {
        service.myUsageStates.put(tenantId, tenantUsageStateMock);
        ApiUsageState tenantUsageState = service.getApiUsageState(tenantId);
        assertThat(tenantUsageState, is(tenantUsageStateMock.getApiUsageState()));
        Mockito.verify(service, never()).getOrFetchState(tenantId, tenantId);
    }

    @Test
    public void givenTenantIdWithoutTenantStateInMap_whenGetState_thenGetOrFetchState() {
        TopicPartitionInfo tpi = Mockito.mock(TopicPartitionInfo.class);
        Mockito.when(tpi.isMyPartition()).thenReturn(true);
        willReturn(tpi).given(partitionService).resolve(ServiceType.TB_CORE, tenantId, tenantId);
        service.myUsageStates.clear();
        willReturn(tenantUsageStateMock).given(service).getOrFetchState(tenantId, tenantId);
        ApiUsageState tenantUsageState = service.getApiUsageState(tenantId);
        assertThat(tenantUsageState, is(tenantUsageStateMock.getApiUsageState()));
        assertThat(true, is(service.getPartitionedEntities(tpi).contains(tenantId)));
        Mockito.verify(service, times(1)).getOrFetchState(tenantId, tenantId);
    }

}
@@ -3235,800 +3235,5 @@
  %7D%0A%0A
-    @Test%0A    public void givenTenantIdWithoutTenantStateInMap_whenGetState_thenGetOrFetchState() %7B%0A        TopicPartitionInfo tpi = Mockito.mock(TopicPartitionInfo.class);%0A        Mockito.when(tpi.isMyPartition()).thenReturn(true);%0A        willReturn(tpi).given(partitionService).resolve(ServiceType.TB_CORE, tenantId, tenantId);%0A        service.myUsageStates.clear();%0A        willReturn(tenantUsageStateMock).given(service).getOrFetchState(tenantId, tenantId);%0A        ApiUsageState tenantUsageState = service.getApiUsageState(tenantId);%0A        assertThat(tenantUsageState, is(tenantUsageStateMock.getApiUsageState()));%0A        assertThat(true, is(service.getPartitionedEntities(tpi).contains(tenantId)));%0A        Mockito.verify(service, times(1)).getOrFetchState(tenantId, tenantId);%0A    %7D%0A%0A
 %7D

Remove incomplete test