/*
 * 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.flink.runtime.io.network.partition;

import org.apache.flink.util.TestLogger;

import org.hamcrest.Matchers;
import org.junit.Test;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * Tests for {@link ResultPartitionManager}.
 */
public class ResultPartitionManagerTest extends TestLogger {

	/**
	 * Tests that {@link ResultPartitionManager#createSubpartitionView(ResultPartitionID, int, BufferAvailabilityListener)}
	 * would throw {@link PartitionNotFoundException} if this partition was not registered before.
	 */
	@Test
	public void testThrowPartitionNotFoundException() throws Exception {
		final ResultPartitionManager partitionManager = new ResultPartitionManager();
		final ResultPartition partition = PartitionTestUtils.createPartition();
		try {
			partitionManager.createSubpartitionView(partition.getPartitionId(), 0, new NoOpBufferAvailablityListener());

			fail("Should throw PartitionNotFoundException for unregistered partition.");
		} catch (PartitionNotFoundException notFound) {
			assertThat(partition.getPartitionId(), Matchers.is(notFound.getPartitionId()));
		}
	}

	/**
	 * Tests {@link ResultPartitionManager#createSubpartitionView(ResultPartitionID, int, BufferAvailabilityListener)}
	 * successful if this partition was already registered before.
	 */
	@Test
	public void testCreateViewForRegisteredPartition() throws Exception {
		final ResultPartitionManager partitionManager = new ResultPartitionManager();
		final ResultPartition partition = PartitionTestUtils.createPartition();

		partitionManager.registerResultPartition(partition);
		partitionManager.createSubpartitionView(partition.getPartitionId(), 0, new NoOpBufferAvailablityListener());
	}
}

Add new unit tests for ResultPartitionManager#createSubpartitionView