package org.brapi.test.BrAPITestServer;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Optional;

import org.brapi.test.BrAPITestServer.model.entity.core.ListEntity;
import org.brapi.test.BrAPITestServer.repository.core.ListRepository;
import org.brapi.test.BrAPITestServer.service.core.ListService;
import org.brapi.test.BrAPITestServer.service.germ.PurdyNode;
import org.brapi.test.BrAPITestServer.service.germ.PurdyNotationUtil;
import org.junit.Test;


public class UnitTests {


	
	@Test
	public void testPurdyParser() {
		String purdy = "ABC/DEF//ABC/DEF/3/GHI/DEF/3/XYZ";
		
		PurdyNode root = PurdyNotationUtil.parsePurdy(purdy);
		
	}

	@Test
	public void testUpdateListItemsReturnsNonEmptyDataForEmptyListAndEmptyRequest() throws Exception {
		ListEntity entity = new ListEntity();
		entity.setId("empty-list");
		entity.setData(new ArrayList<>());

		ListService service = new ListService(fakeListRepository(entity), null);

		assertEquals(1, service.updateListItems("empty-list", new ArrayList<>()).getData().size());
		assertEquals(1, entity.getData().size());
	}

	private ListRepository fakeListRepository(ListEntity entity) {
		return (ListRepository) Proxy.newProxyInstance(ListRepository.class.getClassLoader(),
				new Class<?>[] { ListRepository.class }, (proxy, method, args) -> {
					if ("findById".equals(method.getName())) {
						return Optional.of(entity);
					}
					if ("save".equals(method.getName())) {
						return args[0];
					}
					throw new UnsupportedOperationException(method.getName());
				});
	}

	
}
