| /******************************************************************************** |
| * Copyright (c) 2015-2019 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.odsadapter.search; |
| |
| import static org.assertj.core.api.Assertions.assertThat; |
| import static org.mockito.ArgumentMatchers.any; |
| import static org.mockito.Mockito.mock; |
| import static org.mockito.Mockito.when; |
| |
| import org.eclipse.mdm.api.base.adapter.Attribute; |
| import org.eclipse.mdm.api.base.adapter.EntityType; |
| import org.eclipse.mdm.api.base.model.ValueType; |
| import org.eclipse.mdm.api.base.query.Filter; |
| import org.junit.Test; |
| |
| public class ODSSearchServiceTest { |
| |
| @Test |
| public void testGetMergedFilter() { |
| EntityType testEntity = mockEntityType(); |
| ODSSearchService searchService = mockODSSearchService(testEntity); |
| |
| // No freetext query -> original filter is returned |
| assertThat(searchService.getMergedFilter(Filter.idOnly(testEntity, "1"), "")) |
| .isEqualTo(Filter.and().id(testEntity, "1")); |
| |
| // Freetext query with result -> conjuction of old filter and filter generated |
| // by freetext result |
| assertThat(searchService.getMergedFilter(Filter.idOnly(testEntity, "1"), "existing")) |
| .isEqualTo(Filter.and().id(testEntity, "1").merge(Filter.or().id(testEntity, "5"))); |
| |
| // Freetext query with no result -> empty filter is returned |
| assertThat(searchService.getMergedFilter(Filter.idOnly(testEntity, "1"), "not existing")) |
| .isEqualTo(Filter.or()); |
| |
| // No filter -> filter generated by freetext result is returned |
| assertThat(searchService.getMergedFilter(Filter.or(), "existing")).isEqualTo(Filter.or().id(testEntity, "5")); |
| } |
| |
| private ODSSearchService mockODSSearchService(EntityType testEntity) { |
| ODSSearchService searchService = mock(ODSSearchService.class); |
| when(searchService.getMergedFilter(any(), any())).thenCallRealMethod(); |
| |
| Filter f = Filter.or().id(testEntity, "5"); |
| when(searchService.getFilterForFreetextQuery("existing")).thenReturn(f); |
| when(searchService.getFilterForFreetextQuery("not existing")).thenReturn(null); |
| when(searchService.getFilterForFreetextQuery("")).thenReturn(Filter.or()); |
| return searchService; |
| } |
| |
| private EntityType mockEntityType() { |
| Attribute idAttribute = mock(Attribute.class); |
| when(idAttribute.getName()).thenReturn("Id"); |
| when(idAttribute.createValue("", "1")).thenReturn(ValueType.STRING.create("Id", "1")); |
| |
| EntityType testEntity = mock(EntityType.class); |
| when(testEntity.getName()).thenReturn("TestEntity"); |
| when(testEntity.getIDAttribute()).thenReturn(idAttribute); |
| when(idAttribute.getEntityType()).thenReturn(testEntity); |
| return testEntity; |
| } |
| } |