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