/*
 * Copyright 2013-2022 the original author or authors.
 *
 * Licensed 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
 *
 *      https://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.springframework.cloud.netflix.eureka.server;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.MyDataCenterInfo;
import com.netflix.discovery.shared.Application;
import com.netflix.eureka.EurekaServerContext;
import com.netflix.eureka.EurekaServerContextHolder;
import com.netflix.eureka.cluster.PeerEurekaNode;
import com.netflix.eureka.cluster.PeerEurekaNodes;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.ReflectionUtils;

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

class EurekaControllerTests {

	private ApplicationInfoManager infoManager;

	private ApplicationInfoManager original;

	@BeforeEach
	void setup() throws Exception {
		PeerEurekaNodes peerEurekaNodes = mock(PeerEurekaNodes.class);
		when(peerEurekaNodes.getPeerNodesView()).thenReturn(Collections.<PeerEurekaNode>emptyList());

		InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName("test")
				.setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn)).build();

		this.infoManager = mock(ApplicationInfoManager.class);
		this.original = ApplicationInfoManager.getInstance();
		setInstance(this.infoManager);
		when(this.infoManager.getInfo()).thenReturn(instanceInfo);

		Application myapp = new Application("myapp");
		myapp.addInstance(InstanceInfo.Builder.newBuilder().setAppName("myapp")
				.setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn)).setInstanceId("myapp:1").build());

		ArrayList<Application> applications = new ArrayList<>();
		applications.add(myapp);

		PeerAwareInstanceRegistry registry = mock(PeerAwareInstanceRegistry.class);
		when(registry.getSortedApplications()).thenReturn(applications);

		EurekaServerContext serverContext = mock(EurekaServerContext.class);
		EurekaServerContextHolder.initialize(serverContext);
		when(serverContext.getRegistry()).thenReturn(registry);
		when(serverContext.getPeerEurekaNodes()).thenReturn(peerEurekaNodes);
		when(serverContext.getApplicationInfoManager()).thenReturn(this.infoManager);

	}

	@AfterEach
	void teardown() throws Exception {
		setInstance(this.original);
	}

	static void setInstance(ApplicationInfoManager infoManager) throws IllegalAccessException {
		Field instance = ReflectionUtils.findField(ApplicationInfoManager.class, "instance");
		ReflectionUtils.makeAccessible(instance);
		instance.set(null, infoManager);
	}

	@Test
	void testStatus() {
		Map<String, Object> model = new HashMap<>();

		EurekaController controller = new EurekaController(infoManager, new EurekaProperties());

		controller.status(new MockHttpServletRequest("GET", "/"), model);

		assertThat((String) model.get("environment")).isEqualTo("test");
		assertThat((String) model.get("datacenter")).isEqualTo("default");

		Map<String, Object> app = getFirst(model, "apps");
		Map<String, Object> instanceInfo = getFirst(app, "instanceInfos");
		Map<String, Object> instance = getFirst(instanceInfo, "instances");

		assertThat((String) instance.get("id")).as("id was wrong").isEqualTo("myapp:1");
		assertThat(instance.get("url")).as("url was not null").isNull();
		assertThat((Boolean) instance.get("isHref")).as("isHref was wrong").isFalse();
	}

	@SuppressWarnings("unchecked")
	Map<String, Object> getFirst(Map<String, Object> model, String key) {
		List<Map<String, Object>> apps = (List<Map<String, Object>>) model.get(key);
		assertThat(apps).as(key + " was wrong size").hasSize(1);
		return apps.get(0);
	}

}
