/*
 * 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.apache.flink.runtime.io.network.partition.PartitionTestUtils.createPartition;
import static org.apache.flink.runtime.io.network.partition.PartitionTestUtils.verifyCreateSubpartitionViewThrowsException;
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());
	}

	/**
	 * Tests {@link ResultPartitionManager#createSubpartitionView(ResultPartitionID, int, BufferAvailabilityListener)}
	 * would throw a {@link PartitionNotFoundException} if this partition was already released before.
	 */
	@Test
	public void testCreateViewForReleasedPartition() throws Exception {
		final ResultPartitionManager partitionManager = new ResultPartitionManager();
		final ResultPartition partition = createPartition();

		partitionManager.registerResultPartition(partition);
		partitionManager.releasePartition(partition.getPartitionId(), null);

		verifyCreateSubpartitionViewThrowsException(partitionManager, partition.getPartitionId());
	}
}

@@ -902,38 +902,8 @@
 r;%0A%0A
-import org.hamcrest.Matchers;%0A
 impo
@@ -1145,88 +1145,8 @@
 ion;
-%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.junit.Assert.fail;
 %0A%0A/*
@@ -1682,383 +1682,120 @@
 n = 
-PartitionTestUtils.createPartition();%0A%09%09try %7B%0A%09%09%09partitionManager.createSubpartitionView(partition.getPartitionId(), 0, new NoOpBufferAvailablityListener());%0A%0A%09%09%09fail(%22Should throw PartitionNotFoundException for unregistered partition.%22);%0A%09%09%7D catch (PartitionNotFoundException notFound) %7B%0A%09%09%09assertThat(partition.getPartitionId(), Matchers.is(notFound.getPartitionId()));%0A%09%09%7D
+createPartition();%0A%0A%09%09verifyCreateSubpartitionViewThrowsException(partitionManager, partition.getPartitionId());
 %0A%09%7D%0A
@@ -2179,35 +2179,16 @@
 ition = 
-PartitionTestUtils.
 createPa

Refactor existing tests for reusing PartitionTestUtils