Skip to content

Commit

Permalink
NIFI-5051 Cleaned up POM and added a simple unit test that uses a moc…
Browse files Browse the repository at this point in the history
…k client service.
  • Loading branch information
MikeThomsen committed Apr 27, 2018
1 parent c4895f5 commit 9e08cd6
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.8.2</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -138,16 +132,16 @@
<version>1.7.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-avro-record-utils</artifactId>
<version>1.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-schema-registry-service-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.nifi.elasticsearch.integration;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.nifi.elasticsearch.ElasticSearchClientService;
import org.apache.nifi.elasticsearch.ElasticSearchLookupService;
import org.apache.nifi.serialization.record.MapRecord;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class ElasticSearchLookupServiceTest {
ElasticSearchClientService mockClientService;
ElasticSearchLookupService lookupService;
TestRunner runner;

@Before
public void setup() throws Exception {
mockClientService = new TestElasticSearchClientService();
lookupService = new ElasticSearchLookupService();
runner = TestRunners.newTestRunner(TestControllerServiceProcessor.class);
runner.addControllerService("clientService", mockClientService);
runner.addControllerService("lookupService", lookupService);
runner.enableControllerService(mockClientService);
runner.setProperty(lookupService, ElasticSearchLookupService.CLIENT_SERVICE, "clientService");
runner.setProperty(lookupService, ElasticSearchLookupService.INDEX, "users");
runner.setProperty(TestControllerServiceProcessor.CLIENT_SERVICE, "clientService");
runner.setProperty(TestControllerServiceProcessor.LOOKUP_SERVICE, "lookupService");
runner.enableControllerService(lookupService);
}

@Test
public void simpleLookupTest() throws Exception {
Map<String, Object> coordinates = new HashMap<String,Object>(){{
ObjectMapper mapper = new ObjectMapper();
put("query", mapper.writeValueAsString(new HashMap<String, Object>(){{
put("match", new HashMap<String, Object>(){{
put("username", "john.smith");
}});
}}));
}};

Optional<MapRecord> result = lookupService.lookup(coordinates);

Assert.assertNotNull(result);
Assert.assertTrue(result.isPresent());
MapRecord record = result.get();
Assert.assertEquals("john.smith", record.getAsString("username"));
Assert.assertEquals("testing1234", record.getAsString("password"));
Assert.assertEquals("john.smith@test.com", record.getAsString("email"));
Assert.assertEquals("Software Engineer", record.getAsString("position"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.nifi.elasticsearch.integration;

import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.elasticsearch.ElasticSearchClientService;
import org.apache.nifi.elasticsearch.SearchResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TestElasticSearchClientService extends AbstractControllerService implements ElasticSearchClientService {
@Override
public SearchResponse search(String query, String index, String type) throws IOException {
List hits = new ArrayList<>();
hits.add(new HashMap<String, Object>() {{
put("_source", new HashMap<String, Object>(){{
put("username", "john.smith");
put("password", "testing1234");
put("email", "john.smith@test.com");
put("position", "Software Engineer");
}});
}});
return new SearchResponse(hits, null, 1, 100, false);
}

@Override
public String getTransitUrl(String index, String type) {
return "";
}
}

0 comments on commit 9e08cd6

Please sign in to comment.