com.linkedin.pinot.controller.utils.SegmentMetadataMockUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.linkedin.pinot.controller.utils.SegmentMetadataMockUtils.java

Source

/**
 * Copyright (C) 2014-2016 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * 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 com.linkedin.pinot.controller.utils;

import com.linkedin.pinot.common.metadata.segment.RealtimeSegmentZKMetadata;
import com.linkedin.pinot.common.segment.SegmentMetadata;
import com.linkedin.pinot.core.segment.index.ColumnMetadata;
import com.linkedin.pinot.core.segment.index.SegmentMetadataImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.math.IntRange;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;

public class SegmentMetadataMockUtils {
    private SegmentMetadataMockUtils() {
    }

    public static SegmentMetadata mockSegmentMetadata(String tableName, String segmentName, int numTotalDocs,
            String crc) {
        SegmentMetadata segmentMetadata = Mockito.mock(SegmentMetadata.class);
        Mockito.when(segmentMetadata.getTableName()).thenReturn(tableName);
        Mockito.when(segmentMetadata.getName()).thenReturn(segmentName);
        Mockito.when(segmentMetadata.getTotalDocs()).thenReturn(numTotalDocs);
        Mockito.when(segmentMetadata.getTotalRawDocs()).thenReturn(numTotalDocs);
        Mockito.when(segmentMetadata.getCrc()).thenReturn(crc);
        Mockito.when(segmentMetadata.getPushTime()).thenReturn(Long.MIN_VALUE);
        Mockito.when(segmentMetadata.getRefreshTime()).thenReturn(Long.MIN_VALUE);
        return segmentMetadata;
    }

    public static SegmentMetadata mockSegmentMetadata(String tableName) {
        String uniqueNumericString = Long.toString(System.nanoTime());
        return mockSegmentMetadata(tableName, tableName + uniqueNumericString, 0, uniqueNumericString);
    }

    public static SegmentMetadata mockSegmentMetadata(String tableName, String segmentName) {
        String uniqueNumericString = Long.toString(System.nanoTime());
        return mockSegmentMetadata(tableName, segmentName, 0, uniqueNumericString);
    }

    public static RealtimeSegmentZKMetadata mockRealtimeSegmentZKMetadata(String tableName, String segmentName,
            long numTotalDocs) {
        RealtimeSegmentZKMetadata realtimeSegmentZKMetadata = Mockito.mock(RealtimeSegmentZKMetadata.class);
        Mockito.when(realtimeSegmentZKMetadata.getTableName()).thenReturn(tableName);
        Mockito.when(realtimeSegmentZKMetadata.getSegmentName()).thenReturn(segmentName);
        Mockito.when(realtimeSegmentZKMetadata.getTotalRawDocs()).thenReturn(numTotalDocs);
        return realtimeSegmentZKMetadata;
    }

    public static SegmentMetadata mockSegmentMetadataWithPartitionInfo(String tableName, String segmentName,
            String columnName, int partitionNumber) {
        ColumnMetadata columnMetadata = mock(ColumnMetadata.class);
        List<IntRange> partitionRanges = new ArrayList<>();
        partitionRanges.add(new IntRange(partitionNumber));
        when(columnMetadata.getPartitionRanges()).thenReturn(partitionRanges);

        SegmentMetadataImpl segmentMetadata = mock(SegmentMetadataImpl.class);
        if (columnName != null) {
            when(segmentMetadata.getColumnMetadataFor(columnName)).thenReturn(columnMetadata);
        }
        when(segmentMetadata.getTableName()).thenReturn(tableName);
        when(segmentMetadata.getName()).thenReturn(segmentName);
        when(segmentMetadata.getCrc()).thenReturn("0");
        return segmentMetadata;
    }
}