/*
 * 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.kafka.streams.state.internals.metrics;

import org.apache.kafka.common.metrics.Sensor;
import org.apache.kafka.common.metrics.Sensor.RecordingLevel;
import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Map;

import static org.apache.kafka.common.utils.Utils.mkEntry;
import static org.apache.kafka.common.utils.Utils.mkMap;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class NamedCacheMetricsTest {

    private static final String THREAD_ID = "test-thread";
    private static final String TASK_ID = "test-task";
    private static final String STORE_NAME = "storeName";
    private static final String HIT_RATIO_AVG_DESCRIPTION = "The average cache hit ratio";
    private static final String HIT_RATIO_MIN_DESCRIPTION = "The minimum cache hit ratio";
    private static final String HIT_RATIO_MAX_DESCRIPTION = "The maximum cache hit ratio";

    private final StreamsMetricsImpl streamsMetrics = mock(StreamsMetricsImpl.class);
    private final Sensor expectedSensor = mock(Sensor.class);
    private final Map<String, String> tagMap = mkMap(mkEntry("key", "value"));

    @Test
    public void shouldGetHitRatioSensorWithBuiltInMetricsVersionCurrent() {
        final String hitRatio = "hit-ratio";
        when(streamsMetrics.cacheLevelSensor(THREAD_ID, TASK_ID, STORE_NAME, hitRatio, RecordingLevel.DEBUG)).thenReturn(expectedSensor);
        when(streamsMetrics.cacheLevelTagMap(THREAD_ID, TASK_ID, STORE_NAME)).thenReturn(tagMap);
        StreamsMetricsImpl.addAvgAndMinAndMaxToSensor(
            expectedSensor,
            StreamsMetricsImpl.CACHE_LEVEL_GROUP,
            tagMap,
            hitRatio,
            HIT_RATIO_AVG_DESCRIPTION,
            HIT_RATIO_MIN_DESCRIPTION,
            HIT_RATIO_MAX_DESCRIPTION);

        final Sensor sensor = NamedCacheMetrics.hitRatioSensor(streamsMetrics, THREAD_ID, TASK_ID, STORE_NAME);

        assertThat(sensor, is(expectedSensor));
    }

}

@@ -1040,16 +1040,17 @@
 csImpl;%0A
+%0A
 import o
@@ -1063,39 +1063,32 @@
 it.Test;%0Aimport 
-static 
 org.mockito.Mock
@@ -1091,56 +1091,16 @@
 Mock
-ito.mock;%0Aimport static org.mockito.Mockito.when
+edStatic
 ;%0A%0Ai
@@ -1333,16 +1333,142 @@
 ertThat;
+%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.mockStatic;%0Aimport static org.mockito.Mockito.when;
 %0A%0Apublic
@@ -2540,23 +2540,318 @@
 agMap);%0A
+%0A
        
+ try (final MockedStatic%3CStreamsMetricsImpl%3E streamsMetricsStaticMock = mockStatic(StreamsMetricsImpl.class)) %7B%0A            final Sensor sensor = NamedCacheMetrics.hitRatioSensor(streamsMetrics, THREAD_ID, TASK_ID, STORE_NAME);%0A            streamsMetricsStaticMock.verify(%0A                () -%3E
  Streams
@@ -2902,16 +2902,24 @@
         
+    
+    
 expected
@@ -2926,16 +2926,24 @@
 Sensor,%0A
+        
         
@@ -2996,16 +2996,24 @@
         
+   
+     
 tagMap,%0A
@@ -3024,16 +3024,24 @@
         
+   
+     
 hitRatio
@@ -3046,32 +3046,40 @@
 io,%0A            
+  
+      
 HIT_RATIO_AVG_DE
@@ -3093,32 +3093,40 @@
 ON,%0A            
+  
+      
 HIT_RATIO_MIN_DE
@@ -3140,32 +3140,40 @@
 ON,%0A            
+ 
+       
 HIT_RATIO_MAX_DE
@@ -3181,19 +3181,16 @@
 CRIPTION
-);%0A
 %0A       
@@ -3194,113 +3194,37 @@
     
-final Sensor sensor = NamedCacheMetrics.hitRatioSensor(streamsMetrics, THREAD_ID, TASK_ID, STORE_NAME);%0A%0A
+        )%0A            );%0A    
     
@@ -3271,13 +3271,22 @@
 ));%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

Fix static mock usage in NamedCacheMetricsTest (#12322)