Project

General

Profile

« Previous | Next » 

Revision 328ec747

Added by Andreas Kohlbecker over 3 years ago

ref #9181 consistent drupal variable modification and restoring in the test base class

View differences:

src/main/java/eu/etaxonomy/dataportal/DrupalVars.java
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.dataportal;
10

  
11
/**
12
 * The drupal variables as defined in modules/cdm_dataportal/settings.php
13
 *
14
 * @author a.kohlbecker
15
 * @since Aug 11, 2020
16
 */
17
public class DrupalVars {
18

  
19
    public static final String CDM_DATAPORTAL_TAXONPAGE_TABS = "cdm_dataportal_taxonpage_tabs";
20

  
21

  
22
}
src/main/java/eu/etaxonomy/dataportal/junit/CdmDataPortalTestBase.java
3 3
 */
4 4
package eu.etaxonomy.dataportal.junit;
5 5

  
6
import java.io.IOException;
7
import java.util.HashMap;
8
import java.util.List;
9
import java.util.Map;
10

  
6 11
import org.apache.log4j.Logger;
12
import org.junit.After;
7 13
import org.junit.AfterClass;
8 14
import org.junit.Assert;
9 15
import org.junit.BeforeClass;
......
12 18

  
13 19
import eu.etaxonomy.dataportal.DataPortalContext;
14 20
import eu.etaxonomy.dataportal.DataPortalSite;
21
import eu.etaxonomy.dataportal.DrupalVars;
15 22
import eu.etaxonomy.dataportal.selenium.WebDriverFactory;
23
import eu.etaxonomy.drush.DrushExecuter;
16 24

  
17 25
/**
18 26
 * @author a.kohlbecker
......
27 35

  
28 36
	private DataPortalContext context;
29 37

  
38
    private Map<String,String> drupalVarsBeforeTest = new HashMap<>();
39

  
30 40
	public DataPortalContext getContext() {
31 41
		return context;
32 42
	}
......
36 46

  
37 47
	}
38 48

  
39
	@BeforeClass
49
	/**
50
     * Return the {@link DataPortalSite#getSiteUri()} of the currently active
51
     * context as String
52
     *
53
     * @return string representation of the DataPortal site URI
54
     */
55
    public String getSiteUrl() {
56
    	return context.getSiteUri().toString();
57
    }
58

  
59
    @BeforeClass
40 60
	public static void setUpDriver() {
41 61
		logger.debug("@BeforeClass: setUpDriver()");
42 62
		driver = WebDriverFactory.newWebDriver();
......
50 70
		}
51 71
	}
52 72

  
53
	/**
54
	 * Return the {@link DataPortalSite#getSiteUri()} of the currently active
55
	 * context as String
56
	 *
57
	 * @return string representation of the DataPortal base URI
58
	 */
59
	public String getBaseUrl() {
60
		return context.getSiteUri().toString();
61
	}
73
	@After
74
    public void resetToOriginalState() throws IOException, InterruptedException {
75
        restoreOriginalVars();
76
    }
77

  
78
    /**
79
     * Safely set a Drupal variable to a new value. Any changes to the Drupal
80
     * variables are reset after the test through {@link #resetToOriginalState()}.
81
     *
82
     * @param varKey The key of the Drupal variable to set. In {@link DrupalVars}
83
     * predefined variable key constants can be found.
84
     *
85
     * @param varValue The value to set
86
     *
87
     * @throws IOException
88
     * @throws InterruptedException
89
     */
90
    protected void setDrupalVar(String varKey, String varValue) throws IOException, InterruptedException {
91
        DrushExecuter dex = getContext().drushExecuter();
92
        List<String> result = dex.execute(DrushExecuter.variableGet, varKey);
93
        assertEquals(1, result.size());
94
        if(!drupalVarsBeforeTest.containsKey(varKey)) {
95
            // stored original values must not be replaced
96
            drupalVarsBeforeTest.put(varKey, result.get(0));
97
        }
98
        result = dex.execute(DrushExecuter.variableSet, varKey, varValue);
99
        assertEquals("success", result.get(1));
100
    }
101

  
102
    protected void restoreOriginalVars() throws IOException, InterruptedException {
103
        DrushExecuter dex = getContext().drushExecuter();
104
        boolean fail = false;
105
        for(String varKey : drupalVarsBeforeTest.keySet()) {
106
            try {
107
                List<String> result = dex.execute(DrushExecuter.variableSet, varKey, drupalVarsBeforeTest.get(varKey));
108
                assertEquals("success", result.get(1));
109
            } catch (Exception e) {
110
                logger.error("FATAL ERROR: Restoring the original drupal variable " + varKey + " = " + drupalVarsBeforeTest.get(varKey) + " failed.", e);
111
                fail = true;
112
            }
113
        }
114
        drupalVarsBeforeTest.clear();
115
        if(fail) {
116
            throw new IOException("Restoring a original drupal variable has previously failed. You may want to fix the site settings manually!");
117
        }
118
    }
62 119

  
63 120

  
64 121
}
src/test/java/eu/etaxonomy/dataportal/selenium/tests/reference/SpecimensTopDownViewTest.java
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.dataportal.selenium.tests.reference;
10

  
11
import java.io.IOException;
12
import java.util.UUID;
13

  
14
import org.junit.Before;
15

  
16
import eu.etaxonomy.dataportal.DrupalVars;
17
import eu.etaxonomy.dataportal.junit.CdmDataPortalTestBase;
18

  
19
/**
20
 * @author a.kohlbecker
21
 * @since Aug 11, 2020
22
 */
23
public class SpecimensTopDownViewTest extends CdmDataPortalTestBase {
24

  
25
    private static final UUID glenodinium_apiculatum_t = UUID.fromString("d245083e-3bda-435f-9bb3-bdc2249ff23c");
26

  
27
    @Before
28
    public void switchToView() throws IOException, InterruptedException {
29
        setDrupalVar(DrupalVars.CDM_DATAPORTAL_TAXONPAGE_TABS, "1");
30
    }
31

  
32
}

Also available in: Unified diff