aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/cacher/injestion/parser/PayloadParserServiceTest.java
blob: ddac8e34c396d8d0cd17df1beaa3a1b4f8ca4661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed 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.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.cacher.injestion.parser;

import com.github.fakemongo.Fongo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.IMongodConfig;
import de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.aai.cacher.common.MongoHelperSingleton;
import org.onap.aai.cacher.injestion.parser.strategy.PayloadParserType;
import org.onap.aai.cacher.model.CacheEntry;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {InjestionTestComponent.class,PayloadParserServiceTest.class})
public class PayloadParserServiceTest {

	private static final String DB_NAME = PayloadParserServiceTest.class.getSimpleName();
	private static MongoDatabase mongoDb;
	private static DB db;
	private static MongodProcess mongod;
	private static MongoClient mongoC;

	@Autowired
	private PayloadParserService parserService;


	private JsonParser parser = new JsonParser();

	private String aaiGetAllServiceResponse =
			"{" +
					"	'service': [" +
					"		{" +
					"			'service-id': 'service-id-1:1'," +
					"			'service-description': 'A'," +
					"			'resource-version': '1'" +
					"		}," +
					"		{" +
					"			'service-id': 'service-id-2'," +
					"			'service-description': 'B'," +
					"			'resource-version': '2'" +
					"		}" +
					"	]" +
					"}";
	private JsonObject aaiGetAllServiceResponseJson = parser.parse(aaiGetAllServiceResponse).getAsJsonObject();

	@Bean
	public DB db() {
		return db;
	}

	@Bean
	public MongoDatabase mongoDatabase() {
		return mongoDb;
	}

	@Bean
	public MongoHelperSingleton mongoHelperSingleton(DB db, MongoDatabase mongoDb) {
		return new MongoHelperSingleton(db, mongoDb);
	}

	@BeforeClass
	public static void setup() throws IOException, InterruptedException {
		Fongo fongo = new Fongo(DB_NAME);
		mongoDb = fongo.getDatabase(DB_NAME);
		db = fongo.getDB(DB_NAME);
	}

	protected static void startEmbedded(int port) throws IOException {
		IMongodConfig mongoConfigConfig = new MongodConfigBuilder()
				.version(Version.Main.PRODUCTION)
				.net(new Net(port, Network.localhostIsIPv6()))
				.cmdOptions(new MongoCmdOptionsBuilder().verbose(true).build())
				.configServer(false)
				.build();

		MongodExecutable mongodExecutable = MongodStarter.getDefaultInstance().prepare(mongoConfigConfig);

		mongod = mongodExecutable.start();
	}


	private void print(List<CacheEntry> result) {
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		result.forEach(e -> System.out.println("Collection: " + e.getCollection() + "\nKey: " + e.getId() + "\n" + gson.toJson(e.getPayload())));
	}

	@Test
	public void testGetAllAAIResourceTest() throws JSONException {
		String expectedUri = "/service-design-and-creation/services/service/service-id-1%3A1";

		List<CacheEntry> result = parserService.doParse("service", aaiGetAllServiceResponseJson, "aai-resource-get-all");
		print(result);

		assertTrue(result.stream().map(cacheIdentifier -> cacheIdentifier.getId()).anyMatch(id -> id.equals(expectedUri)));
		JSONAssert.assertEquals(
				new JSONObject(aaiGetAllServiceResponseJson.getAsJsonArray("service").get(0).getAsJsonObject().toString()),
				new JSONObject(result.stream().filter(e -> e.getId().equals(expectedUri)).map(CacheEntry::getPayload).findFirst().get().toString()),
				false);

	}

	@Test
	public void testGetAllAAIResourceStringTest() throws JSONException {
		String expectedUri = "/service-design-and-creation/services/service/service-id-2";

		List<CacheEntry> result = parserService.doParse("service", aaiGetAllServiceResponse, PayloadParserType.AAI_RESOURCE_GET_ALL);
		print(result);

		assertTrue(result.stream().map(cacheIdentifier -> cacheIdentifier.getId()).anyMatch(id -> id.equals(expectedUri)));
		JSONAssert.assertEquals(
				new JSONObject(aaiGetAllServiceResponseJson.getAsJsonArray("service").get(1).getAsJsonObject().toString()),
				new JSONObject(result.stream().filter(e -> e.getId().equals(expectedUri)).map(CacheEntry::getPayload).findFirst().get().toString()),
				false);
	}

	@Test
	public void testNoneStrategyTest1() throws JSONException {
		String cacheKey = "service";
		List<CacheEntry> result = parserService.doParse(cacheKey, aaiGetAllServiceResponse);
		print(result);
		noneTests(cacheKey, result);
	}

	@Test
	public void testNoneStrategyTest2() throws JSONException {
		String cacheKey = "service";
		List<CacheEntry> result = parserService.doParse(cacheKey, aaiGetAllServiceResponseJson);
		print(result);
		noneTests(cacheKey, result);
	}

	@Test
	public void testNoneStrategyTest3() throws JSONException {
		String cacheKey = "service";
		List<CacheEntry> result = parserService.doParse(cacheKey, aaiGetAllServiceResponse, PayloadParserType.NONE);
		print(result);
		noneTests(cacheKey, result);
	}

	@Test
	public void testNoneStrategyTest4() throws JSONException {
		String cacheKey = "service";
		List<CacheEntry> result = parserService.doParse(cacheKey, aaiGetAllServiceResponse, "none");
		print(result);
		noneTests(cacheKey, result);
	}

	private void noneTests(String cacheKey, List<CacheEntry> result) throws JSONException {
		assertThat(result.size(), is(1));
		assertTrue(result.stream().map(cacheIdentifier -> cacheIdentifier.getId()).anyMatch(id -> id.equals(cacheKey)));
		JSONAssert.assertEquals(
				new JSONObject(aaiGetAllServiceResponse),
				new JSONObject(result.get(0).getPayload().toString()),
				false);
	}


}