Project

General

Profile

1
package edu.ucsb.nceas.metacat.dataone.hazelcast;
2

    
3
import static org.hamcrest.CoreMatchers.is;
4
import static org.junit.Assert.assertThat;
5

    
6
import java.io.IOException;
7
import java.util.Map;
8
import java.util.concurrent.Callable;
9

    
10
import org.dataone.service.types.v1.Identifier;
11
import org.junit.After;
12
import org.junit.Rule;
13
import org.junit.Test;
14
import org.junit.rules.ErrorCollector;
15

    
16
import com.hazelcast.config.ClasspathXmlConfig;
17
import com.hazelcast.config.Config;
18
import com.hazelcast.config.UrlXmlConfig;
19
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

    
27
	/**
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

    
37
	
38
	
39
    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
	@Test
82
	public void testBehavior() throws IOException {
83
		/**
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
		Config config1 = new ClasspathXmlConfig("edu/ucsb/nceas/metacat/dataone/hazelcast/hzObjectPathMap.provider.test.properties.xml");
89
		HazelcastInstance mapProvider = Hazelcast.newHazelcastInstance(config1);
90
		
91
		// setup and start the non-maploader member (d1_indexer)
92
		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
		
96
		// 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
		
100
		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

    
105
		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
		
126
		
127
	}
128
	
129
//		
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
	
152
	
153
		
154
	private Identifier createIdentifier(String idValue) {
155
		Identifier id = new Identifier();
156
		id.setValue(idValue);
157
		return id;
158
	}
159

    
160
}
(2-2/5)