Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bddff4f2c6056be15f4b6cd65637f7f09bd728fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/******************************************************************************* 
 * Copyright (c) 2018 Red Hat, Inc. 
 * Distributed under license by Red Hat, Inc. All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 * 
 * Contributors: 
 * Red Hat, Inc. - initial API and implementation 
 ******************************************************************************/
package org.eclipse.linuxtools.docker.reddeer.requirements;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.linuxtools.docker.reddeer.ui.DockerExplorerView;
import org.eclipse.linuxtools.docker.reddeer.ui.resources.DockerConnection;
import org.eclipse.reddeer.common.logging.Logger;
import org.eclipse.reddeer.core.exception.CoreLayerException;
import org.eclipse.reddeer.junit.requirement.Requirement;
import org.eclipse.reddeer.swt.impl.tree.DefaultTree;
import org.eclipse.linuxtools.docker.reddeer.requirements.CleanDockerExplorerRequirement.CleanDockerExplorer;

/**
 * Requirement assuring that all Docker connections are removed from Docker Explorer.
 * @author Ondrej Dockal, odockal@redhat.com
 *
 */
public class CleanDockerExplorerRequirement implements Requirement<CleanDockerExplorer> {

	private CleanDockerExplorer cleanDocker;
	
	private static final Logger log = Logger.getLogger(CleanDockerExplorerRequirement.class);
	
	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.TYPE)
	public @interface CleanDockerExplorer {
		/**
		 * Decides if to remove all connections when {@link #cleanup()} is called.
		 * @return boolean, default is false
		 */
		boolean cleanup() default false;
	}

	@Override
	public void fulfill() {
		removeAllDockerConnections();
	}

	@Override
	public void setDeclaration(CleanDockerExplorer declaration) {
		this.cleanDocker = declaration;
	}

	@Override
	public CleanDockerExplorer getDeclaration() {
		return this.cleanDocker;
	}

	@Override
	public void cleanUp() {
		if (this.cleanDocker.cleanup()) {
			removeAllDockerConnections();
		}
	}
	
	/**
	 * Opens DockerExplorer view
	 */
	public void initializeExplorer() {
		new DockerExplorerView().open();
	}
	
	/**
	 * Obtains list of all docker connection found in Docker Explorer view
	 * @return {@code}ArrayList{@code} list of DockerConnection objects or empty list if there is not any
	 */
	public List<DockerConnection> getDockerConnections() {
		initializeExplorer();
		log.info("Getting all available Docker connections..."); 
		try {
			return new DefaultTree().getItems().stream()
					.map(x -> new DockerConnection(x))
					.collect(Collectors.toList()); 
		} catch (CoreLayerException coreExc) {
			// there is no item in docker explorer
		}
		return new ArrayList<>();
	}
	
	/**
	 * Removes all docker connections found
	 */
	public void removeAllDockerConnections() {
		List<DockerConnection> connections = getDockerConnections();
		if (!connections.isEmpty()) {
			connections.stream()
			.forEach(x -> {
				log.info("Removing: " + x.getName()); 
				x.removeConnection();
			});
		} else {
			log.info("There was no connection..."); 
		}		
	}

}

Back to the top