Skip to main content
summaryrefslogtreecommitdiffstats
blob: aea77da0b13d81037f0959380068c93c7ca2998b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/********************************************************************************
 * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 ********************************************************************************/

package org.eclipse.mdm.api.base;

import org.eclipse.mdm.api.base.model.TestStep;
import org.eclipse.mdm.api.base.query.DataAccessException;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.*;

public class BaseEntityManagerTest {

	@Test
	public void loadShouldReturnEntity() throws DataAccessException {
		
		@SuppressWarnings("unchecked")
		BaseEntityManager entityManager = mock(BaseEntityManager.class);
		TestStep mockedTestStep = mock(TestStep.class);
		
		when(entityManager.load(any(), anyString())).thenCallRealMethod();
		when(entityManager.load(TestStep.class, Arrays.asList("id1"))).thenReturn(Arrays.asList(mockedTestStep));
		
		assertThat(entityManager.load(TestStep.class, "id1"))
			.isEqualTo(mockedTestStep);
	}
	
	@Test
	public void loadNotExistingIdShouldThrowDataAccessException() throws DataAccessException {
		
		@SuppressWarnings("unchecked")
		BaseEntityManager entityManager = mock(BaseEntityManager.class);
		
		when(entityManager.load(any(), anyString())).thenCallRealMethod();
		when(entityManager.load(eq(TestStep.class), anyCollection())).thenReturn(Collections.<TestStep>emptyList());
		
		assertThatThrownBy(() -> entityManager.load(TestStep.class, "xyz"))
			.isInstanceOf(DataAccessException.class)
			.hasMessageContaining("Failed to load entity by instance ID.");
	}
	
	@Test
	public void loadNotUniqueIdShouldThrowDataAccessException() throws DataAccessException {
		
		@SuppressWarnings("unchecked")
		BaseEntityManager entityManager = mock(BaseEntityManager.class);
		TestStep mockedTestStep1 = mock(TestStep.class);
		TestStep mockedTestStep2 = mock(TestStep.class);

		when(entityManager.load(any(), anyString())).thenCallRealMethod();
		when(entityManager.load(TestStep.class, Arrays.asList("id1")))
			.thenReturn(Arrays.asList(mockedTestStep1, mockedTestStep2));
		
		assertThatThrownBy(() -> entityManager.load(TestStep.class, "id1"))
			.isInstanceOf(DataAccessException.class)
			.hasMessageContaining("Failed to load entity by instance ID.");
	}
}

Back to the top