Project

General

Profile

1 6440 rnahf
package edu.ucsb.nceas.metacat.dataone.hazelcast;
2 6420 rnahf
3 6440 rnahf
import static org.hamcrest.CoreMatchers.is;
4
import static org.junit.Assert.assertThat;
5
6
import java.io.File;
7 6420 rnahf
import java.util.Map;
8 6440 rnahf
import java.util.concurrent.Callable;
9 6420 rnahf
10 6424 rnahf
import org.dataone.service.types.v1.Identifier;
11 6440 rnahf
import org.junit.After;
12 6424 rnahf
import org.junit.Before;
13 6440 rnahf
import org.junit.Rule;
14 6420 rnahf
import org.junit.Test;
15 6440 rnahf
import org.junit.rules.ErrorCollector;
16 6420 rnahf
17
import com.hazelcast.core.Hazelcast;
18
import com.hazelcast.core.HazelcastInstance;
19
20
import edu.ucsb.nceas.MCTestCase;
21
22
23
public class HzObjectPathMapTest extends MCTestCase {
24 6440 rnahf
	public final static String CONFIG_PATH = "/Users/rnahf/" +
25
//			"projects/" +
26
			"software/workspace" +
27
			"nceas/metacat/test/edu/ucsb/nceas/metacat/dataone/hazelcast";
28
29
	/**
30
	 * Need to use the error collector to handle JUnit assertions
31
	 * and keep going.  This is because setting up multiple tests of
32
	 * Hazelcast is not straightforward.
33
	 * The check methods in this class use this errorCollector
34
	 * the check methods
35
	 */
36
	@Rule
37
    public ErrorCollector errorCollector = new ErrorCollector();
38 6420 rnahf
39
40 6424 rnahf
41 6440 rnahf
    protected void checkEquals(final String message, final String s1, final String s2)
42
    {
43
 //   	System.out.println("assertion: " + message);
44
    	errorCollector.checkSucceeds(new Callable<Object>()
45
        {
46
            public Object call() throws Exception
47
            {
48
                assertThat(message, s1, is(s2));
49
                return null;
50
            }
51
        });
52
    }
53
54
    /**
55
	 * performs the equivalent of the junit assertTrue method
56
	 * using the errorCollector to record the error and keep going
57
	 *
58
	 * @param message
59
	 * @param s1
60
	 * @param s2
61
	 */
62
    protected void checkTrue(final String message, final boolean b)
63
    {
64
//        System.out.println("assertion: " + message);
65
    	errorCollector.checkSucceeds(new Callable<Object>()
66
        {
67
            public Object call() throws Exception
68
            {
69
            	assertThat(message, true, is(b));
70
            	return null;
71
            }
72
        });
73
    }
74
75
76
77
	@After
78
    public void cleanup() throws Exception {
79
        Hazelcast.shutdownAll();
80
    }
81
82
83 6420 rnahf
	@Test
84 6440 rnahf
	public void testBehavior() {
85
		/**
86
		 * set up the two hazelcast instances (separate configurations)
87
		 * mapProvider is configured with the ObjectPathMap (MapLoader)
88
		 * mapUser is configured with a default config file (no MapLoader implementation)
89
		 */
90
		File configPath = new File(CONFIG_PATH + "hzObjectPathMap.provider.test.properties.xml");
91
		checkTrue("config file should exist", configPath.canRead());
92
		System.getProperties().setProperty("hazelcast.config", configPath.getAbsolutePath());
93
		HazelcastInstance mapProvider = Hazelcast.newHazelcastInstance(null);
94
95 6420 rnahf
96 6440 rnahf
		// setup and start the non-maploader member (d1_indexer)
97
		configPath = new File(CONFIG_PATH + "hzObjectPathMap.user.test.properties.xml");
98
		checkTrue("config file should exist", configPath.canRead());
99
		System.getProperties().setProperty("hazelcast.config", configPath.getAbsolutePath());
100
		HazelcastInstance mapUser = Hazelcast.newHazelcastInstance(null);
101 6424 rnahf
102 6440 rnahf
		// try to read from uninstantiated map
103
		Map<Identifier,String> userMap = mapUser.getMap("hzObjectPath");
104
		checkTrue("userMap should be empty at first", userMap.size() == 0);
105 6424 rnahf
106 6440 rnahf
		Map<Identifier,String> providerMap = mapProvider.getMap("hzObjectPath");
107
		checkTrue("providerMap should have keys", providerMap.size() > 0);
108
		checkTrue("userMap should have keys now", userMap.size() > 0);
109
110 6424 rnahf
111 6440 rnahf
		System.out.println("test Getting Preloaded Keys Via the UserMap");
112
113
		String pathValue = userMap.get(createIdentifier("testID.26"));
114
		System.out.println("pathValue: " + pathValue);
115
		checkEquals("userMap should contain a value for this",
116
				"/path/testID.26", pathValue);
117
118
119
		System.out.println("test Getting Unloaded Keys Via the UserMap");
120
		pathValue = userMap.get(createIdentifier("anNewKey"));
121
		System.out.println("pathValue: " + pathValue);
122
		checkEquals("userMap should contain a value for this",
123
				"/path/anNewKey", pathValue);
124
125
126
		System.out.println("test Entry Not Added When Key Not In Datastore");
127
128
		pathValue = userMap.get(createIdentifier("NO_CREATE_identifier"));
129
		System.out.println("pathValue: " + pathValue);
130
		checkEquals("providerInstance should return null if not found", null, pathValue);
131 6424 rnahf
132
133 6440 rnahf
	}
134 6420 rnahf
135 6440 rnahf
//
136
//		System.out.println(serverMap.get(createIdentifier("testID.35")));
137
//		System.out.println(serverMap.get(createIdentifier("aNewIdentifier")));
138
//
139
////		System.getProperties().setProperty("hazelcast.hzObjectPathRole","consumer");
140
//
141
//		System.out.println("client Instance");
142
//		System.out.println("1 " + clientMap.get(createIdentifier("testID.35")));
143
//		System.out.println("2 " + clientMap.get(createIdentifier("aNewIdentifier")));
144
//
145
//		String lookupMyPathPlease = "foo";
146
//		String pathValue = (String) clientMap.get(createIdentifier(lookupMyPathPlease));
147
//		System.out.println("remote retrieval of pathValue: " + pathValue);
148
//
149
//		pathValue = (String) serverMap.get(createIdentifier(lookupMyPathPlease));
150
//		System.out.println("server retrieval of pathValue: " + pathValue);
151
//
152
//		pathValue = (String) clientMap.get(createIdentifier(lookupMyPathPlease));
153
//		System.out.println("remote retrieval of pathValue again: " + pathValue);
154
//
155
//		//assertEquals("/path/" + lookupMyPathPlease, pathValue);
156
//	}
157 6420 rnahf
158 6440 rnahf
159 6424 rnahf
160
	private Identifier createIdentifier(String idValue) {
161
		Identifier id = new Identifier();
162
		id.setValue(idValue);
163
		return id;
164
	}
165
166 6420 rnahf
}