/*
 * 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.solr.client.solrj.embedded;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.schema.SchemaRequest;
import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.client.solrj.response.schema.SchemaResponse.FieldResponse;
import org.apache.solr.common.SolrException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestEmbeddedSolrServerSchemaAPI extends SolrTestCaseJ4 {

  private String fieldName = "VerificationTest";
  private static EmbeddedSolrServer server;
  private final Map<String, Object> fieldAttributes;
  {
    Map<String,Object> field = new LinkedHashMap<>();
    field.put("name", fieldName);
    field.put("type", "string");
    field.put("stored", false);
    field.put("indexed", true);
    field.put("multiValued", true);
    fieldAttributes = Collections.unmodifiableMap(field);
  }

  @BeforeClass
  public static void initClass() throws Exception {
    assertNull("no system props clash please", System.getProperty("managed.schema.mutable"));
    System.setProperty("managed.schema.mutable", ""+//true
    random().nextBoolean()
    );
    Path tmpHome = createTempDir("tmp-home");
    Path coreDir = tmpHome.resolve(DEFAULT_TEST_CORENAME);
    copyMinConf(coreDir.toFile(), null, "solrconfig-managed-schema.xml");
    initCore("solrconfig.xml" /*it's renamed to to*/, "schema.xml", tmpHome.toAbsolutePath().toString());
    
    server = new EmbeddedSolrServer(h.getCoreContainer(), DEFAULT_TEST_CORENAME);
  }

  @AfterClass
  public static void destroyClass() throws IOException {
    server.close(); // doubtful
    server = null;
    System.clearProperty("managed.schema.mutable");
  }

  @Before
  public void thereIsNoFieldYet() throws SolrServerException, IOException{
    try{
      FieldResponse process = new SchemaRequest.Field(fieldName)
                  .process(server);
      fail(""+process);
    }catch(SolrException e){
      assertTrue(e.getMessage().contains("No")
           && e.getMessage().contains("VerificationTest"));
    }
  }
  
  @Test
  public void testSchemaAddFieldAndVerifyExistence() throws Exception {
    assumeTrue("it needs to ammend schema", Boolean.getBoolean("managed.schema.mutable"));
    SchemaResponse.UpdateResponse addFieldResponse = new SchemaRequest.AddField(fieldAttributes).process(server);

    assertEquals(addFieldResponse.toString(), 0, addFieldResponse.getStatus());

    // This asserts that the field was actually created
    // this is due to the fact that the response gave OK but actually never created the field.
    Map<String,Object> foundFieldAttributes = new SchemaRequest.Field(fieldName).process(server).getField();
    assertEquals(fieldAttributes, foundFieldAttributes);

    assertEquals("removing " + fieldName, 0,
        new SchemaRequest.DeleteField(fieldName).process(server).getStatus());
  }

  @Test 
  public void testSchemaAddFieldAndFailOnImmutable() throws Exception {
    assumeFalse("it needs a readonly schema", Boolean.getBoolean("managed.schema.mutable"));

      SchemaRequest.AddField addFieldUpdateSchemaRequest = new SchemaRequest.AddField(fieldAttributes);
      SchemaResponse.UpdateResponse addFieldResponse = addFieldUpdateSchemaRequest.process(server);
      // wt hell???? assertFalse(addFieldResponse.toString(), addFieldResponse.getStatus()==0);
      assertTrue((""+addFieldResponse).contains("schema is not editable"));

  }

}

@@ -960,16 +960,39 @@
 ashMap;%0A
+import java.util.List;%0A
 import j
@@ -1005,16 +1005,16 @@
 il.Map;%0A
-
 %0Aimport 
@@ -1045,16 +1045,51 @@
 CaseJ4;%0A
+import org.apache.solr.api.ApiBag;%0A
 import o
@@ -1129,32 +1129,89 @@
 erverException;%0A
+import org.apache.solr.client.solrj.impl.HttpSolrClient;%0A
 import org.apach
@@ -1402,32 +1402,32 @@
 .FieldResponse;%0A
-
 import org.apach
@@ -1447,32 +1447,131 @@
 .SolrException;%0A
+import org.apache.solr.common.util.NamedList;%0Aimport org.apache.solr.common.util.SimpleOrderedMap;%0A
 import org.junit
@@ -4220,33 +4220,16 @@
 utable()
- throws Exception
  %7B%0A    a
@@ -4317,26 +4317,24 @@
 le%22));%0A%0A    
-  
 SchemaReques
@@ -4427,56 +4427,34 @@
     
-  SchemaResponse.UpdateResponse addField
+assertFailedSchema
 Response
  = a
@@ -4449,18 +4449,22 @@
 Response
- =
+(() -%3E
  addFiel
@@ -4503,186 +4503,379 @@
 ver)
-;
+,
 %0A      
-// wt hell???? assertFalse(addFieldResponse.toString(), addFieldResponse.getStatus()==0);%0A      assertTrue((%22%22+addFieldResponse).contains(%22schema is not editable%22
+  %22schema is not editable%22);%0A  %7D%0A%0A  private static void assertFailedSchemaResponse(ThrowingRunnable runnable, String expectedErrorMessage) %7B%0A    ApiBag.ExceptionWithErrObject e = expectThrows(ApiBag.ExceptionWithErrObject.class, runnable);%0A    String msg = e.getErrs().get(0).get(%22errorMessages%22).toString();%0A    assertTrue(msg.contains(expectedErrorMessage
 ));%0A
-%0A
   %7D%0A
-%0A
 %7D%0A

fix another test