/*******************************************************************************
 * Copyright (c) 2019 Eclipse RDF4J contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *******************************************************************************/
package org.eclipse.rdf4j.http.server.repository.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.repository.config.RepositoryConfig;
import org.eclipse.rdf4j.repository.config.RepositoryConfigUtil;
import org.eclipse.rdf4j.repository.manager.RepositoryManager;
import org.eclipse.rdf4j.repository.sail.config.SailRepositoryConfig;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.sail.memory.config.MemoryStoreConfig;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

public class ConfigControllerTest {

	@Test
	public void retrievesConfiguration() throws Exception {
		ConfigController controller = new ConfigController();

		String repositoryId = "test-config";

		final MockHttpServletRequest request = new MockHttpServletRequest();
		request.setMethod(HttpMethod.GET.name());
		request.addHeader("Accept", RDFFormat.NTRIPLES.getDefaultMIMEType());

		RepositoryConfig config = new RepositoryConfig(repositoryId, new SailRepositoryConfig(new MemoryStoreConfig()));
		RepositoryManager manager = mock(RepositoryManager.class);
		when(manager.getRepositoryConfig(repositoryId)).thenReturn(config);
		controller.setRepositoryManager(manager);

		// repository interceptor uses this attribute
		request.setAttribute("repositoryID", repositoryId);

		ModelAndView result = controller.handleRequest(request, new MockHttpServletResponse());

		verify(manager).getRepositoryConfig(repositoryId);
		assertThat(result.getModel().containsKey(ConfigView.CONFIG_DATA_KEY));

		Model resultData = (Model) result.getModel().get(ConfigView.CONFIG_DATA_KEY);
		RepositoryConfig resultConfig = RepositoryConfigUtil.getRepositoryConfig(resultData, repositoryId);
		assertThat(resultConfig).isNotNull();
	}

}

@@ -1389,167 +1389,186 @@
 %7B%0A%0A%09
-@Test%0A%09public void retrievesConfiguration() throws Exception %7B%0A%09%09ConfigController controller = new ConfigController();%0A%0A%09%09String repositoryId = %22test-config%22;%0A
+final String repositoryId = %22test-config%22;%0A%09final ConfigController controller = new ConfigController();%0A%0A%09@Test%0A%09public void getRequestRetrievesConfiguration() throws Exception %7B
 %0A%09%09f
@@ -2590,10 +2590,270 @@
 ();%0A%09%7D%0A%0A
+%09@Test%0A%09public void postRequestModifiesConfiguration() throws Exception %7B%0A%09%09final MockHttpServletRequest request = new MockHttpServletRequest();%0A%09%09request.setMethod(HttpMethod.POST.name());%0A%09%09request.setContentType(RDFFormat.NTRIPLES.getDefaultMIMEType());%0A%09%7D%0A
 %7D%0A

test for posting config modification [WIP]